From cc99001697d9eac7c096b87ce632c473ca87f9a9 Mon Sep 17 00:00:00 2001 From: Koichi MATSUMOTO Date: Sat, 31 Oct 2020 14:24:29 +0900 Subject: [PATCH 1/3] fix image file path bug and add deletion of image file entity fix image file path bug and add deletion of image file entity. --- src/controllers/ImagesController.ts | 8 ++++++++ src/index.ts | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/controllers/ImagesController.ts b/src/controllers/ImagesController.ts index 1c76b144..82bd6bf7 100644 --- a/src/controllers/ImagesController.ts +++ b/src/controllers/ImagesController.ts @@ -1,3 +1,5 @@ +import fs from 'fs'; +import { join } from 'path'; import { FastifyReply, FastifyRequest, FastifyInstance } from 'fastify'; import { Controller, @@ -57,6 +59,12 @@ export class ImagesController { id: req.params.id }); + const dir = config.uploader.directory ? config.uploader.directory : 'uploads'; + const path = join(dir.charAt(0) == '/' ? dir : join(process.cwd(), dir), image.file); + fs.unlink(path, (err) => { + if (err) throw new Error('No image file'); + }); + Console.logger(Image).info(`image ${image.id} was deleted`); if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE)) WebhookHelper.sendWebhook(this.webhooks.upload.content, { diff --git a/src/index.ts b/src/index.ts index f2a303c0..1e517520 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,9 @@ if (!config) { process.exit(0); } +const dir = config.uploader.directory ? config.uploader.directory : 'uploads';; +const path = dir.charAt(0) == '/' ? dir : join(process.cwd(), dir); + const server = fastify({}); const app = next({ dev, @@ -114,7 +117,7 @@ server.register(fastifyCookies, { }); server.register(fastifyStatic, { - root: join(process.cwd(), config.uploader.directory), + root: root: path, prefix: config.uploader.route }); From 13406a2f67513aa736ddb112a1516f1c266b1387 Mon Sep 17 00:00:00 2001 From: Koichi MATSUMOTO Date: Sat, 31 Oct 2020 14:32:15 +0900 Subject: [PATCH 2/3] remove unnecessary word remove unnecessary word --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 1e517520..55d0a960 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,7 +117,7 @@ server.register(fastifyCookies, { }); server.register(fastifyStatic, { - root: root: path, + root: path, prefix: config.uploader.route }); From 278aa66e0029355448d3625bf70a50c75357ea20 Mon Sep 17 00:00:00 2001 From: dicedtomato <35403473+dicedtomatoreal@users.noreply.github.com> Date: Sat, 31 Oct 2020 17:28:28 -0700 Subject: [PATCH 3/3] Update ImagesController.ts --- src/controllers/ImagesController.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/controllers/ImagesController.ts b/src/controllers/ImagesController.ts index 82bd6bf7..bf209b2e 100644 --- a/src/controllers/ImagesController.ts +++ b/src/controllers/ImagesController.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { unlinkSync } from 'fs'; import { join } from 'path'; import { FastifyReply, FastifyRequest, FastifyInstance } from 'fastify'; import { @@ -61,18 +61,22 @@ export class ImagesController { const dir = config.uploader.directory ? config.uploader.directory : 'uploads'; const path = join(dir.charAt(0) == '/' ? dir : join(process.cwd(), dir), image.file); - fs.unlink(path, (err) => { - if (err) throw new Error('No image file'); - }); + + try { + unlinkSync(path); + + Console.logger(Image).info(`image ${image.id} was deleted`); + if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE)) + WebhookHelper.sendWebhook(this.webhooks.upload.content, { + image, + host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.uploader.route}/` + }); - Console.logger(Image).info(`image ${image.id} was deleted`); - if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE)) - WebhookHelper.sendWebhook(this.webhooks.upload.content, { - image, - host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.uploader.route}/` - }); - - return reply.send(image); + return reply.send(image); + } catch (e) { + Console.logger(Image).error(`image ${image.id} could not be deleted...`); + return reply.status(401).send({ error: 'Could not delete image.' }) + } } @GET('/recent')