This commit is contained in:
oitsjustjose 2020-10-16 01:31:59 -05:00
commit 25f973e2b3
37 changed files with 812 additions and 470 deletions

View file

@ -0,0 +1,37 @@
import { Request, Response } from 'express'
import Images from '../../db/img.schema'
import fs from 'fs'
import path from 'path'
export default async (req: Request, res: Response) => {
try {
const data = fs.readFileSync(req.file.path)
fs.unlinkSync(req.file.path)
const images = await Images.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.status(200).send(img._id);
}
}
const img = new Images()
img.img.contentType = req.file.mimetype
img.img.data = data
img.uploaders = req.clientIp ? [req.clientIp as string] : []
await img.save()
return res.status(200).send(img._id);
} catch (ex) {
console.error(ex)
return res.status(500).send({
error: ex
})
}
}