-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
117 lines (96 loc) · 2.7 KB
/
server.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
const express = require('express')
const multer = require('multer')
const cors = require('cors')
const knex = require('knex')
const fs = require('fs')
const dotenv = require('dotenv').config()
const cron = require('node-cron')
const postgres = knex({
client: 'pg',
connection: {
host : process.env.DB_HOST,
user : process.env.DB_USER,
password : process.env.DB_PASS,
database : process.env.DB_NAME,
}
})
let response;
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, process.env.STORAGE_LOC)
},
filename: async function (req, file, cb) {
day = Math.floor(Date.now() / 86400000)
extensionarray = file.originalname.split(".")
extension = extensionarray[extensionarray.length - 1]
do {
random_int = Math.floor(Math.random() * Math.pow(36, process.env.NAME_LENGTH))
b36_int = random_int.toString(36)
response = b36_int + '.' + extension
match = await postgres('files').where('filename',response)
} while(match.length != 0)
postgres('files').insert({
filename: response,
date: day,
}).then()
cb(null, response)
}
})
const upload = multer({
storage: storage,
limits:{fileSize:536870912 } // 500 mb
})
const app = express();
app.use(cors());
app.use(express.static(process.env.STORAGE_LOC));
// deletion schedule
cron.schedule('0 0 * * *',() => {
const currentDate = Math.floor(Date.now() / 86400000);
postgres('files')
.select('filename')
.from('files')
.where('date', '<', currentDate)
.then(
fs.unlink('./storage/' + entry.filename, () => {})
)
postgres('files')
.delete()
.where('date', '<', currentDate)
.then()
})
app.get('/', (req, res) => {
res.send("API is up and running")
})
app.post('/uploadtext', upload.none(), async (req, res, next) => {
day = Math.floor(Date.now() / 86400000)
do {
random_int = Math.floor(Math.random() * Math.pow(36, process.env.NAME_LENGTH))
b36_int = random_int.toString(36)
response = b36_int + '.txt'
match = await postgres('files').where('filename',response)
} while(match.length != 0)
postgres('files').insert({
filename: response,
date: day,
})
fs.appendFile( process.env.STORAGE_LOC + "/" + response, req.body.chosenFile, () => {})
const resObject = {link: response}
res.json(resObject)
})
app.post('/upload', upload.single('chosenFile'), (req, res, next) => {
/*const filename = function (req, file, cb) {
const time = Date.now() % 86400000
const day = Math.floor(Date.now() / 86400000)
const b36time = time.toString(36)
response = b36time + '.txt'
postgres('files').insert({
filename: response,
timemillis: time,
date: day,
}).then()
return response
}*/
const resObject = {link: response}
res.json(resObject)
})
app.listen(process.env.PORT);