-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (65 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require('dotenv').config()
const express = require('express')
const multer = require('multer')
const cors = require('cors')
const fs = require('fs');
//express variables
const app = express()
const port = process.env.PORT
const corsOptions = {
origin: process.env.TARGET_URL,
optionsSuccessStatus: 200
}
app.use(cors(corsOptions))
app.listen(port, () => {
console.log(`CDN active at http://localhost:${port}`)
})
app.use(express.static('public'), )
//CORS preflights
app.options('*', cors(corsOptions))
//multer settings
const imageStorage = multer.diskStorage({
destination: function (req, file, cb) {
const path = `public/uploads/${req.body.owner}`
fs.mkdirSync(path,{recursive: true})
cb(null, path)
},
filename: function (req, file, cb) {
cb(null, file.originalname)
},
})
const docsStorage = multer.diskStorage({
destination: function (req, file, cb) {
const path = `public/uploads/${req.body.owner}/${req.body.request}`
fs.mkdirSync(path,{recursive: true})
cb(null, path)
},
filename: function (req, file, cb) {
cb(null, file.originalname)
},
})
const uploadImage = multer({ storage: imageStorage })
const uploadDocs = multer({ storage: docsStorage })
const parseJson = express.json()
app.post('/image', uploadImage.single("profileImage"), (req,res,next)=>{
res.status(200).send({
url: req.file.path
})
})
app.post('/docs', uploadDocs.single("attachment"), (req,res,next)=>{
res.status(200).send({
url: req.file.path
})
})
// accepts application/json
app.post('/remove_docs', parseJson, (req, res) => {
const path = `public/uploads/${req.body.owner}/${req.body.request}/${req.body.fileName}`
fs.unlink(path, (err) => {
if (err) {
console.error(err)
res.status(500).send(err)
} else {
res.status(200).send('FILE DELETED')
}
})
})