-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
286 lines (251 loc) · 11.8 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const express = require('express');
const multer = require('multer');
const path = require('path');
const { exec } = require('child_process');
const fs = require('fs');
const archiver = require('archiver');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const app = express();
const port = 3000;
const convertedDir = path.join(__dirname, 'converted');
// Check if the 'converted' directory exists, and if not, create it
if (!fs.existsSync(convertedDir)) {
fs.mkdirSync(convertedDir, { recursive: true });
}
// Use Helmet and Ratelimit for enhanced security if the SECURITY environment variable is enabled
if (process.env.SECURITY === 'enabled') {
app.use(helmet());
const apiLimiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use('/api/', apiLimiter);
}
// Set up multer for file uploads with file type validation and size limit
const upload = multer({
dest: 'uploads/', // Directory to save uploaded files temporarily
limits: { fileSize: 1024 * 1024 * 1024 }, // 1GB file size limit
fileFilter: (req, file, cb) => {
const allowedMimeTypes = [
'video/mp4',
'video/avi',
'video/mkv',
'video/webm',
'video/quicktime',
'video/mpeg',
'application/octet-stream'
];
// Validate file type
if (allowedMimeTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('Invalid file type. Only video files are allowed.'));
}
}
});
// Set up EJS as the view engine
app.set('view engine', 'ejs');
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Render the home page
app.get('/', (req, res) => {
res.render('index');
});
// Function to sanitize and validate input values
const sanitizeInput = (input, type) => {
switch (type) {
case 'resolution':
const resolution = parseInt(input, 10);
if (isNaN(resolution) || resolution < 50 || resolution > 100 || resolution % 10 !== 0) {
throw new Error('Invalid resolution value. It must be a percentage between 50 and 100 in steps of 10.');
}
return resolution;
case 'fps':
const fps = parseInt(input, 10);
if (isNaN(fps) || fps < 15 || fps > 60) {
throw new Error('Invalid FPS value. It must be between 15 and 60.');
}
return fps;
case 'bitrate':
const bitrate = parseInt(input, 10);
if (isNaN(bitrate) || bitrate < 1000 || bitrate > 10000 || bitrate % 100 !== 0) {
throw new Error('Invalid bitrate value. It must be between 1000 and 10000 kbps in steps of 100.');
}
return `${bitrate}k`;
case 'format':
const allowedFormats = ['mp4', 'avi', 'mkv', 'webm', 'mov'];
if (!allowedFormats.includes(input)) {
throw new Error('Invalid output format. Allowed formats are mp4, avi, mkv, webm, mov.');
}
return input;
default:
throw new Error('Invalid input type.');
}
};
// Ensure the file path is within the 'uploads' or 'converted' directories for security
const safePath = (filePath) => {
const absolutePath = path.resolve(filePath);
const allowedPaths = [path.resolve(__dirname, 'uploads'), path.resolve(__dirname, 'converted')];
return allowedPaths.some(allowedPath => absolutePath.startsWith(allowedPath));
};
// Schedule file deletion after 1 hour
const scheduleFileDeletion = (filePath) => {
setTimeout(() => {
if (fs.existsSync(filePath)) {
fs.unlink(filePath, (err) => {
if (err) console.error(`Error deleting file after timeout: ${err.message}`);
else console.log(`File deleted after 1 hour: ${filePath}`);
});
}
}, 3600000); // 1 hour in milliseconds
};
// Handle video uploads and conversion
app.post('/upload', upload.array('videos'), (req, res) => {
try {
// Destructure and sanitize input values from the request body
const { format, resolution, fps, bitrate } = req.body;
const outputFormat = sanitizeInput(format, 'format');
const sanitizedResolution = sanitizeInput(resolution, 'resolution');
const sanitizedFps = sanitizeInput(fps, 'fps');
const sanitizedBitrate = sanitizeInput(bitrate, 'bitrate');
// Calculate scale factor for video resolution
const scaleFactor = sanitizedResolution / 100;
const scaleFilter = `iw*${scaleFactor}:ih*${scaleFactor}`;
// Create an array of promises to handle multiple file conversions
const conversionPromises = req.files.map(file => new Promise((resolve, reject) => {
const outputFilePath = path.join('converted', `${path.parse(file.filename).name}.${outputFormat}`);
// Ensure both input and output paths are safe
if (!safePath(file.path) || !safePath(outputFilePath)) {
reject(new Error('Unsafe file path detected.'));
return;
}
// Construct the ffmpeg command
const ffmpegCommand = `ffmpeg -i ${file.path} -vf "scale=${scaleFilter}" -r ${sanitizedFps} -b:v ${sanitizedBitrate} -preset fast ${outputFilePath}`;
console.log(`Executing command: ${ffmpegCommand}`);
// Execute the ffmpeg command for conversion
exec(ffmpegCommand, (error) => {
if (error) {
reject(new Error(`Error during conversion: ${error.message}`));
return;
}
// Delete the original uploaded file after conversion
fs.unlink(file.path, err => {
if (err) console.error(`Error deleting uploaded file: ${err.message}`);
else console.log(`Uploaded file deleted: ${file.path}`);
});
// Schedule the converted file for deletion after 1 hour
scheduleFileDeletion(outputFilePath);
resolve(outputFilePath);
});
}));
// Handle all conversion promises
Promise.all(conversionPromises)
.then(convertedFiles => {
// Handle single file conversion by sending the converted file to the client
if (convertedFiles.length === 1) {
const file = convertedFiles[0];
res.download(file, 'converted_video.' + outputFormat, err => {
if (err) console.error(`Error sending converted file: ${err.message}`);
fs.unlink(file, err => {
if (err) console.error(`Error deleting converted file: ${err.message}`);
});
});
} else {
// Handle multiple files by zipping them
const zipFilePath = path.join('converted', 'converted_videos.zip');
const output = fs.createWriteStream(zipFilePath);
const archive = archiver('zip', { zlib: { level: 9 } });
// Handle archiving errors
archive.on('error', err => {
console.error(`Archiving error: ${err.message}`);
res.status(500).send('An error occurred during archiving.');
});
// Send the zip file to the client after successful archiving
output.on('close', () => {
res.download(zipFilePath, 'converted_videos.zip', err => {
if (err) console.error(`Error sending zip file: ${err.message}`);
fs.unlink(zipFilePath, err => {
if (err) console.error(`Error deleting zip file: ${err.message}`);
});
// Clean up individual converted files after zipping
convertedFiles.forEach(file => {
fs.unlink(file, err => {
if (err) console.error(`Error deleting converted file: ${err.message}`);
});
});
});
});
// Add files to the archive
archive.pipe(output);
convertedFiles.forEach(file => archive.file(file, { name: path.basename(file) }));
archive.finalize(); // Finalize the archive
scheduleFileDeletion(zipFilePath); // Schedule deletion of the zip file
}
})
.catch(error => {
console.error(`Error in conversion process: ${error.message}`);
res.status(500).send('An error occurred during the conversion process.');
});
} catch (error) {
console.error(`Error in processing request: ${error.message}`);
res.status(400).send(error.message); // Return error to the client
}
});
// Handle API video upload and conversion
app.post('/api/upload', upload.single('video'), (req, res) => {
try {
// Verify API key for security
const apiKey = process.env.API_KEY;
if (apiKey && apiKey !== "none" && req.headers['authorization'] !== apiKey) {
return res.status(403).json({ error: 'Forbidden: Invalid API key' });
}
// Destructure and sanitize input values from the request body
const { format, resolution, fps, bitrate } = req.body;
const outputFormat = sanitizeInput(format, 'format');
const sanitizedResolution = sanitizeInput(resolution, 'resolution');
const sanitizedFps = sanitizeInput(fps, 'fps');
const sanitizedBitrate = sanitizeInput(`${bitrate}k`, 'bitrate');
// Calculate scale factor for video resolution
const scaleFactor = sanitizedResolution / 100;
const scaleFilter = `iw*${scaleFactor}:ih*${scaleFactor}`;
// Define output file path
const outputFilePath = path.join('converted', `${path.parse(req.file.filename).name}.${outputFormat}`);
// Ensure both input and output paths are safe
if (!safePath(req.file.path) || !safePath(outputFilePath)) {
throw new Error('Unsafe file path detected.');
}
// Construct the ffmpeg command
const ffmpegCommand = `ffmpeg -i ${req.file.path} -vf "scale=${scaleFilter}" -r ${sanitizedFps} -b:v ${sanitizedBitrate} -preset fast ${outputFilePath}`;
console.log(`Executing command: ${ffmpegCommand}`);
// Execute the ffmpeg command for conversion
exec(ffmpegCommand, (error) => {
if (error) {
return res.status(500).json({ error: 'Conversion error.' });
}
// Delete the original uploaded file after conversion
fs.unlink(req.file.path, err => {
if (err) console.error(`Error deleting uploaded file: ${err.message}`);
});
// Schedule the converted file for deletion after 1 hour
scheduleFileDeletion(outputFilePath);
res.download(outputFilePath, err => {
if (err) console.error(`Error sending converted file: ${err.message}`);
fs.unlink(outputFilePath, err => {
if (err) console.error(`Error deleting converted file: ${err.message}`);
});
});
});
} catch (error) {
console.error(`Error in API processing: ${error.message}`);
res.status(400).json({ error: error.message });
}
});
// Render the privacy policy page
app.get('/privacy', (req, res) => {
res.render('privacy');
});
// Start the server
app.listen(port, () => {
console.log(`ConvertZ is running at http://localhost:${port}`);
});