Initial Commit

This commit is contained in:
Jose 2020-08-22 15:35:42 -05:00
commit 1f37d91e8e
9 changed files with 3725 additions and 0 deletions

34
src/app/routes.ts Normal file
View file

@ -0,0 +1,34 @@
import { Application } from 'express'
import Image from './Models/img.model'
import { Multer } from 'multer'
import fs from 'fs'
import path from 'path'
export const use = (app: Application, uploader: Multer) => {
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "./Views/upload.html"))
})
app.get("/:slug", async (req, res) => {
const img = await Image.findById(req.params.slug)
if (img) {
res.contentType(img.contentType)
res.send(img.data)
} else {
res.status(404).end()
}
})
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()
res.redirect(`/${img._id}`)
// fs.unlinkSync(fp)
})
}