dv2ls/backend/src/app.ts
2021-06-21 17:23:00 -05:00

50 lines
No EOL
1.5 KiB
TypeScript

import * as bodyParser from 'body-parser'
import cors from 'cors'
import express from 'express'
import { existsSync, mkdirSync } from 'fs'
import moment from 'moment-timezone'
import multer from 'multer'
import path from 'path'
import { mw as requestIpMw } from 'request-ip'
import getImageHandler from './app/controllers/img/getImageHandler'
import putImageHandler from './app/controllers/img/putImageHandler'
import getTxtHandler from './app/controllers/txt/getTxtHandler'
import putTxtHandler from './app/controllers/txt/putTxtHandler'
import getUrlHandler from './app/controllers/url/getUrlHandler'
import putUrlHandler from './app/controllers/url/putUrlHandler'
const app = express()
app.use(cors())
app.use(requestIpMw())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
const uploadPath = '/tmp/ImgUploads'
if (!existsSync(uploadPath)) {
mkdirSync(uploadPath)
}
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadPath)
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${moment().toISOString()}`)
}
})
// 52428800 = 50mb
const uploader = multer({ storage, limits: { fileSize: 52428800 } })
// Image Uploader
app.put("/api/img", uploader.single("image"), putImageHandler)
app.get("/i/:slug", getImageHandler)
// Pastebin
app.put("/api/paste", putTxtHandler)
app.get("/p/:slug", getTxtHandler)
// URL Shortener -- LAST for a reason.
app.put("/api/url", putUrlHandler)
app.get("/:slug", getUrlHandler)
export default app