Skip to content

Commit

Permalink
Merge pull request #35 from JSPaste/dev
Browse files Browse the repository at this point in the history
merge dev into stable
  • Loading branch information
tnfAngel authored Jan 29, 2024
2 parents cbd978e + 963304b commit 0771131
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
34 changes: 21 additions & 13 deletions src/classes/DocumentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ export class DocumentHandler {

doc.rawFileData = buffer;

await DocumentManager.write(basePath + key, doc);
const edited = await DocumentManager.write(basePath + key, doc)
.then(() => true)
.catch(() => false);

return { message: 'File updated successfully' };
return { edited };
}

static async handleExists({ errorSender, key }: { errorSender: ErrorSender; key: string }) {
Expand Down Expand Up @@ -216,32 +218,36 @@ export class DocumentHandler {
// Make the document permanent if the value exceeds 5 years
if ((lifetime ?? 0) > 157_784_760) lifetime = 0;

const expirationTimestamp =
const parsedExpirationTimestamp =
(lifetime ?? defaultDocumentLifetime) * 1000 > 0
? Date.now() + (lifetime ?? defaultDocumentLifetime) * 1000
: undefined;

const expirationTimestamp =
typeof parsedExpirationTimestamp === 'number'
? BigInt(parsedExpirationTimestamp)
: undefined;

const newDoc: DocumentDataStruct = {
rawFileData: buffer,
secret,
expirationTimestamp:
typeof expirationTimestamp === 'number' ? BigInt(expirationTimestamp) : undefined,
expirationTimestamp,
password
};

const selectedKey = await createKey();
const key = await createKey();

await DocumentManager.write(basePath + selectedKey, newDoc);
await DocumentManager.write(basePath + key, newDoc);

switch (version) {
case APIVersions.v1:
return { key: selectedKey, secret };
return { key, secret };
case APIVersions.v2:
return {
key: selectedKey,
key,
secret,
url: viewDocumentPath + selectedKey,
expirationTimestamp
url: viewDocumentPath + key,
expirationTimestamp: parsedExpirationTimestamp
};
}
}
Expand Down Expand Up @@ -283,8 +289,10 @@ export class DocumentHandler {
});

// FIXME: Use bun
await unlink(basePath + key);
const removed = await unlink(basePath + key)
.then(() => true)
.catch(() => false);

return { message: 'File removed successfully' };
return { removed };
}
}
11 changes: 7 additions & 4 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ export class Server {
})
)
.use(errorSenderPlugin)
.onError(({ errorSender, set, code, error }) => {
.onError(({ errorSender, path, set, code, error }) => {
switch (code) {
case 'NOT_FOUND':
// for some reason, error sender is not available on this code.
set.status = 404;
return 'Not found, please try again later of check our documentation.';
if (path === '/404') return 'Not found';

// Redirect to the frontend 404 page
set.redirect = '/404';

return;

case 'VALIDATION':
return errorSender.sendError(400, {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/v1/remove.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export default new Elysia({
response: {
200: t.Object(
{
message: t.String({
description: 'A message saying that the deletion was successful'
removed: t.Boolean({
description: 'A boolean indicating if the deletion was successful'
})
},
{ description: 'A response object with a message' }
{ description: 'A response object with a boolean' }
),
400: ErrorSender.errorType(),
403: ErrorSender.errorType(),
Expand Down
6 changes: 3 additions & 3 deletions src/routes/v2/edit.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default new Elysia({
response: {
200: t.Object(
{
message: t.String({
description: 'A message saying that the update was successful'
edited: t.Boolean({
description: 'A boolean indicating if the edit was successful'
})
},
{ description: 'A response object with a message' }
{ description: 'A response object with a boolean' }
),
400: ErrorSender.errorType(),
403: ErrorSender.errorType(),
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v2/exists.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DocumentHandler } from '../../classes/DocumentHandler.ts';
import { errorSenderPlugin } from '../../plugins/errorSender.ts';

export default new Elysia({
name: 'routes:v2:documents:exits'
name: 'routes:v2:documents:exists'
})
.use(errorSenderPlugin)
.get(
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v2/publish.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default new Elysia({
),
lifetime: t.Optional(
t.Numeric({
description: `Number in seconds that the document will exist before it is automatically deleted. Set to 0 to make the document permanent. If nothing is set, the default period is: ${defaultDocumentLifetime}`,
description: `Number in seconds that the document will exist before it is automatically removed. Set to 0 to make the document permanent. If nothing is set, the default period is: ${defaultDocumentLifetime}`,
examples: [60, 0]
})
)
Expand Down
6 changes: 3 additions & 3 deletions src/routes/v2/remove.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export default new Elysia({
response: {
200: t.Object(
{
message: t.String({
description: 'A message saying that the deletion was successful'
removed: t.Boolean({
description: 'A boolean indicating if the deletion was successful'
})
},
{ description: 'A response object with a message' }
{ description: 'A response object with a boolean' }
),
400: ErrorSender.errorType(),
403: ErrorSender.errorType(),
Expand Down

0 comments on commit 0771131

Please sign in to comment.