Don't upload duplicate images!
This commit is contained in:
parent
199ed6837a
commit
a476453d1c
1 changed files with 23 additions and 9 deletions
|
|
@ -13,8 +13,8 @@ export const use = (app: Application, uploader: Multer) => {
|
||||||
const img = await Image.findById(req.params.slug)
|
const img = await Image.findById(req.params.slug)
|
||||||
|
|
||||||
if (img) {
|
if (img) {
|
||||||
res.contentType(img.contentType)
|
res.contentType(img.img.contentType)
|
||||||
res.send(img.data)
|
res.send(img.img.data)
|
||||||
} else {
|
} else {
|
||||||
res.status(404).end()
|
res.status(404).end()
|
||||||
}
|
}
|
||||||
|
|
@ -22,13 +22,27 @@ export const use = (app: Application, uploader: Multer) => {
|
||||||
|
|
||||||
app.post("/", uploader.single('image'), async (req, res) => {
|
app.post("/", uploader.single('image'), async (req, res) => {
|
||||||
const fp = path.join(__dirname + '/Uploads/' + req.file.filename)
|
const fp = path.join(__dirname + '/Uploads/' + req.file.filename)
|
||||||
const img = new Image({
|
const data = fs.readFileSync(fp)
|
||||||
data: fs.readFileSync(fp),
|
fs.unlinkSync(fp)
|
||||||
contentType: req.file.mimetype
|
|
||||||
})
|
|
||||||
await img.save()
|
|
||||||
|
|
||||||
res.redirect(`/${img._id}`)
|
const images = await Image.find()
|
||||||
// fs.unlinkSync(fp)
|
|
||||||
|
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}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue