-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
211 lines (165 loc) · 5.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
require('./mongo')()
const express = require('express');
const { storage } = require('./storage/storage');
const multer = require('multer');
const replaceUrlExtension = require("./functions/replaceVideoWithMPD")
const axios = require("axios");
const fileFilter = (req, file, cb) => {
const requestURL = req.baseUrl
if (requestURL.startsWith("/upload")) {
return cb(null, true);
}
const allowedFileTypes = ['image/jpeg', 'image/png', 'image/jpg', 'image/gif', 'video/mp4', 'video/webm', 'video/ogg'];
if (allowedFileTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error({ success: false, message: "Unsupported file format only: jpeg, jpg, png, gifs and videos are allowed", code: 400 }));
}
};
const uploadPlanelix = multer({ storage: storage,limits: {
fieldSize: 31457280
},
fileFilter: fileFilter });
const upload = multer({ storage: storage });
const cors = require('cors');
const images = require('./database/images')
const planelixFiles = require("./database/planelixAttachments")
const logger = require('morgan')
const authorizer = require('./middleware/authorizer')
const uuid = require("uuid")
const app = express();
app.use(logger('dev'))
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/authorize", authorizer, async (req, res) => {
return res.status(200).json({ success: true })
})
app.post('/upload', authorizer, upload.single('image'), async (req, res) => {
const id = uuid.v4()
if (!req.file) {
return res.json({ error: "Please provide a valid image" })
}
const path = req.file.path;
if (!path) {
return res.json({ error: "Please provide a valid image" })
}
await images.create({
id: id,
url: path
})
return res.json({ success: true, url: `${id}` })
})
app.post("/upload/planelix", uploadPlanelix.single("image"), async (req, res) => {
try {
let isVideo = false;
if (!req.file) {
return res.status(400).json({ success: false, message: "Please provide a valid attachment", code: 400 })
}
const path = req.file.path;
if (!req.file.path) {
return res.status(400).json({ success: false, message: "Please provide a valid attachment", code: 400 })
}
const fileType = req.file.mimetype;
if (fileType.startsWith("video/")) {
isVideo = true;
}
if (isVideo) {
const id = uuid.v4();
const mpdPath = replaceUrlExtension(path);
await planelixFiles.create({
_id: id,
type: req.file.mimetype,
url: path,
mpd_url: mpdPath,
})
return res.status(200).json({ success: true, message: "uploaded", mpd: mpdPath, raw_url: req.file.path, id: id, type: req.file.mimetype, code: 200 })
} else {
const id = uuid.v4();
await planelixFiles.create({
_id: id,
type: req.file.mimetype,
url: path,
mpd_url: null
})
return res.status(200).json({
success: true, message: "uploaded", mpd: null,
raw_url: req.file.path,
id: id,
type: req.file.mimetype, code: 200
})
}
} catch (e) {
console.error(e)
return res.status(500).json({ success: false, message: "internal server error or file upload not supported", code: 500 })
}
})
app.get("/planelix/:id", async (req, res) => {
const id = req.params.id;
const checkIfExist = await planelixFiles.findOne({ _id: id })
if (!checkIfExist) {
return res.sendStatus(404)
}
let url = checkIfExist.url;
const imageResponse = await axios.get(url, {
responseType: 'arraybuffer'
})
const imageType = imageResponse.headers['content-type'];
res.set('Content-Type', imageType);
res.set("Cache-Control", "public, max-age=604800");
const expiryDate = new Date(Date.now() + 604800000).toUTCString();
res.set("Expires", expiryDate);
res.send(imageResponse.data);
})
app.get('/status', async (req, res) => {
res.sendStatus(200)
})
app.get('/', async (req, res) => {
res.sendStatus(403)
});
app.get("/proxy", async(req, res) => {
try {
const {url} = req.query;
if(!url) {
return res.status(400).json({success:false, message: 'Invalid url', code: 400})
}
if(!url.startsWith("http://") && !url.startsWith("https://")) {
return res.status(400).json({success:false, message: 'Invalid url', code: 400})
}
const response = await axios.get(url, {
responseType: 'arraybuffer',
})
const filetype = response.headers['content-type'];
res.set('Content-Type', filetype);
res.send(response.data)
} catch(e) {
console.error(e)
return res.status(500).json({success:false, message: 'Internal server error', code: 500})
}
})
app.get('/:id', async (req, res) => {
const id = req.params.id;
const image = await images.findOne({ id: id })
if (!image) {
return res.status(404).send()
}
const url = image.url;
const imageResponse = await axios.get(url, {
responseType: 'arraybuffer'
})
const imageType = imageResponse.headers['content-type'];
res.set('Content-Type', imageType);
res.set("Cache-Control", "public, max-age=604800");
const expiryDate = new Date(Date.now() + 604800000).toUTCString();
res.set("Expires", expiryDate);
res.send(imageResponse.data);
})
app.get("*", async (req, res) => {
res.status(404).send()
})
app.listen(3000, () => {
console.log('server started');
});