-
Notifications
You must be signed in to change notification settings - Fork 17
/
routes.js
97 lines (84 loc) · 2.9 KB
/
routes.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
import { getFileList, statAsync } from './utils';
import { Router } from 'express';
import bodyparser from 'body-parser';
import { exec } from 'child_process';
import multer from 'multer';
import path from 'path';
import serverConfig from './config';
import url from 'url';
import webpackConfig from './../webpack.config.babel';
const urlencodedParser = bodyparser.urlencoded({ extended: true });
const isProduction = process.env.NODE_ENV === 'production';
const entryPoint = isProduction ? '/build/' : 'http://localhost:8080/';
// file upload configuration
const storage = multer.diskStorage({
destination: (request, file, callback) => {
callback(null, serverConfig.uploads);
},
filename: (request, file, callback) => {
let filename = file.originalname;
if (file.mimetype.match('video/.*'))
filename = `${filename}.mp4`;
console.info(`UPLOADING FILE... ${filename}`);
callback(null, filename);
}
});
const upload = multer({ storage });
// Routes
const router = new Router();
router.get('/', (req, res) => {
res.render('index', {
bundle: url.resolve(entryPoint, webpackConfig.output.filename),
title: 'PiZilla'
});
});
router.post('/upload', upload.any(), (req, res) => {
return res.status( 200 ).send(req.files);
});
router.get('/download', async (req, res) => {
if (!req.query.path) res.end();
else {
const filePath = path.resolve(req.query.path);
const isFile = await statAsync(filePath).isFile();
if (isFile) res.download(filePath);
}
});
router.get('/files', async (req, res) => {
let curDir = serverConfig.uploads;
const query = req.query.path || '';
if (query) curDir = path.resolve(query);
const files = await getFileList(curDir);
if (files === null)
res.json({ 'error': `Access denied: ${curDir}` }).end(403);
else
res.json(files);
});
router.get('/pifire', (_, res) => {
res.render('pifire');
});
router.post('/pifire', urlencodedParser, (req, res) => {
const response = {
selection_radio: req.body.group1,
url_input: req.body.url
};
const url = response.url_input;
const file = '/home/vinay/Desktop/PiZilla/uploads/';
let type = '';
if(response.selection_radio === 'music') {
type = '-f 140';
}
exec(`youtube-dl ${type} --no-check-certificate ` +
'-c --audio-quality 0 --restrict-filenames --no-warnings ' +
`--no-check-certificate -o ${file}'%(title)s.%(ext)s' ` +
`"${url}"`, (error) => {
if (error) console.error(error);
else exec(`youtube-dl ${type} --no-check-certificate ` +
'-c --recode-video mp4 --restrict-filenames' +
` --no-warnings -o ${file}'%(title)s.%(ext)s' "${url}"`,
(error) => {
if (error) console.error(error);
});
});
res.redirect('/pifire');
});
export default router;