forked from bansicloud/store
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
106 lines (84 loc) · 2.59 KB
/
app.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
const http = require('http');
const express = require('express');
const stringify = require('json-stringify-safe')
require('dotenv').config()
const multer = require('multer')
const path = require('path');
const { mkdir } = require('fs');
const cors = require('cors');
const { gitState, connectToGitHub, getStats, uploadFiles } = require('./repo');
const { sendMessage } = require('./telegram');
const app = express();
const server = http.createServer(app);
// CONSTANTS
const port = process.env.PORT || 4000;
const publicPath = path.join(__dirname, './client/build');
const FILES_LIMIT = 5;
const MAX_FILE_SIZE_MB = parseInt(process.env['MAX_FILE_SIZE_MB']) || 100;
app.use(cors());
app.use(express.static(publicPath));
// Custom function to handle uploads
var storage = multer.diskStorage({
destination: function (req, file, cb) {
mkdir(path.join(__dirname + '/uploads'), () => {
cb(null, path.join(__dirname + '/uploads'));
});
},
filename: function (req, file, cb) {
// Get extention of uploaded file
const extension = path.parse(file.originalname).ext;
console.log('received', file.originalname);
// Save file using format
cb(null, file.originalname);
}
});
// Applying custom upload handler
const upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/initialInfo', (req, res) => {
res.send({
FILES_LIMIT: FILES_LIMIT,
MAX_FILE_SIZE_MB: MAX_FILE_SIZE_MB
});
});
app.post('/stats', async (req, res) => {
getStats()
.then(stats => {
res.send(stringify(stats))
});
});
app.post('/upload', upload.array('somefiles', FILES_LIMIT), (req, res) => {
uploadFiles(req.files)
.then(links => {
res.send(links);
})
.catch(err => {
res.status(500).json(err.message);
});
});
app.post('/file', upload.array('file', FILES_LIMIT), (req, res) => {
console.log(req.files);
uploadFiles(req.files)
.then(links => {
res.send(links[0]);
})
.catch(err => {
res.status(500).json(err.message);
});
});
/**
* Server Initialization
*/
connectToGitHub()
.then(() => {
console.log(`[Server]: Working with ${gitState.blockLetter}${gitState.workingBlock}`);
server.listen(port, () => {
console.log(`[Server]: App is open on port ${port}`);
const _host = server.address().address;
const _port = server.address().port;
const msg = `📡 Started http://${_host === '::' ? 'localhost' : _host}:${_port} | Username: ${gitState.username} | Block: ${gitState.blockLetter}${gitState.workingBlock}`;
_host === '::' ? null : sendMessage(msg);
});
});