--
This commit is contained in:
parent
3cd5d356cb
commit
25f973e2b3
37 changed files with 812 additions and 470 deletions
37
backend/src/app/controllers/img/putImageHandler.ts
Normal file
37
backend/src/app/controllers/img/putImageHandler.ts
Normal 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
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue