-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox.js
128 lines (115 loc) · 3.7 KB
/
dropbox.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const basicAuth = require('express-basic-auth');
require('dotenv').config();
const app = express();
const port = 3000;
const users = { [process.env.KH_USER]: process.env.KH_PASSWORD };
app.use(basicAuth({
users: users,
challenge: true,
realm: 'KH Dropbox',
}));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadPath = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
}
cb(null, uploadPath);
},
filename: (req, file, cb) => {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const month = String(now.getMonth() + 1).padStart(2, '0');
const year = String(now.getFullYear()).slice(2);
const timestamp = `${hours}${minutes}${seconds}${day}${month}${year}`;
const originalname = file.originalname;
const ext = path.extname(originalname);
const baseName = path.basename(originalname, ext);
const uniqueFilename = `${baseName}-${timestamp}${ext}`;
cb(null, uniqueFilename);
},
});
const upload = multer({
storage: storage,
limits: {
fileSize: 200 * 1024 * 1024,
},
});
app.use(express.static(__dirname));
app.post('/upload', upload.array('files', 10), (req, res, next) => {
if (!req.files || req.files.length === 0) {
return res.send("No files uploaded");
}
res.send('Files uploaded successfully!');
const fileData = req.files.map((file) => ({
originalFilename: file.originalname,
filname: file.filename,
size: file.size,
}));
const postData = {
content: JSON.stringify(fileData),
username: "KH Dropbox"
};
axios.post(process.env.KH_WEBHOOK, postData);
}, (err, req, res, next) => {
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_FILE_SIZE') {
return res.status(413).send('File size too large (max 200MB)');
}
return res.status(400).send('Multer error: ' + err.message);
} else if (err) {
return res.status(500).send('An error occurred: ' + err.message);
}
next();
});
app.get('/download/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(__dirname, 'uploads', filename);
if (fs.existsSync(filePath)) {
res.download(filePath, filename, (err) => {
if (err) {
res.status(500).send('Error downloading file.');
}
});
} else {
res.status(404).send('File not found.');
}
});
app.get('/files', (req, res) => {
const uploadPath = path.join(__dirname, 'uploads');
fs.readdir(uploadPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return res.status(500).send('Error reading files.');
}
const filesWithStats = [];
let completed = 0;
files.forEach((file) => {
fs.stat(path.join(uploadPath, file), (statErr, stats) => {
if (!statErr) {
filesWithStats.push({ file, stats });
}
completed++;
if (completed === files.length) {
filesWithStats.sort((a, b) => a.stats.mtimeMs - b.stats.mtimeMs);
const sortedFiles = filesWithStats.map((fileStat) => fileStat.file);
res.json(sortedFiles);
}
});
});
if(files.length === 0){
res.json([]);
}
});
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});