From a476453d1c6db5f848bd3257aec464741e889302 Mon Sep 17 00:00:00 2001 From: Jose Date: Sat, 22 Aug 2020 21:13:15 -0500 Subject: [PATCH] Don't upload duplicate images! --- src/app/routes.ts | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/app/routes.ts b/src/app/routes.ts index 5b1857c..d622954 100644 --- a/src/app/routes.ts +++ b/src/app/routes.ts @@ -13,8 +13,8 @@ export const use = (app: Application, uploader: Multer) => { const img = await Image.findById(req.params.slug) if (img) { - res.contentType(img.contentType) - res.send(img.data) + res.contentType(img.img.contentType) + res.send(img.img.data) } else { res.status(404).end() } @@ -22,13 +22,27 @@ export const use = (app: Application, uploader: Multer) => { app.post("/", uploader.single('image'), async (req, res) => { const fp = path.join(__dirname + '/Uploads/' + req.file.filename) - const img = new Image({ - data: fs.readFileSync(fp), - contentType: req.file.mimetype - }) - await img.save() + const data = fs.readFileSync(fp) + fs.unlinkSync(fp) - res.redirect(`/${img._id}`) - // fs.unlinkSync(fp) + const images = await Image.find() + + for (const img of images) { + if (img.img.data.compare(data) === 0) { + /* If some other IP uploads, we want to add them too */ + img.uploaders.push(req.clientIp as string) + await img.save() + return res.redirect(`/${img._id}`) + } + } + + const img = new Image() + + img.img.contentType = req.file.mimetype + img.img.data = data + img.uploaders = req.clientIp ? [req.clientIp as string] : [] + + await img.save() + return res.redirect(`/${img._id}`) }) } \ No newline at end of file