You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.0 KiB
JavaScript
105 lines
2.0 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const express = require('express');
|
|
const app = express();
|
|
const fileUpload = require('express-fileupload');
|
|
|
|
app.use(express.json());
|
|
app.use(fileUpload());
|
|
|
|
const port = 8080;
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running at http://localhost:${port}`);
|
|
});
|
|
|
|
app.use(express.static('public'));
|
|
|
|
let videoDirs = [
|
|
'./public/videos',
|
|
'/media/pi/USB',
|
|
'/video'
|
|
];
|
|
|
|
let fileEndings = [
|
|
'.mp4',
|
|
'.mkv',
|
|
'.avi',
|
|
'.mov',
|
|
'.flv',
|
|
'.wmv',
|
|
'.webm',
|
|
'.m4v',
|
|
'.mpg',
|
|
'.mpeg',
|
|
'.3gp',
|
|
'.3g2',
|
|
'.m2v',
|
|
'.m4v'
|
|
];
|
|
|
|
app.get('/videos', (req, res) => {
|
|
const list = [];
|
|
|
|
for (const dir of videoDirs) {
|
|
fs.readdirSync(dir, { recursive: true }).forEach(fileName => {
|
|
if (!fileEndings.includes(path.extname(fileName))) {
|
|
return;
|
|
}
|
|
const file = {
|
|
name: fileName,
|
|
path: path.resolve(dir, fileName)
|
|
};
|
|
list.push(file);
|
|
});
|
|
}
|
|
|
|
res.json(list);
|
|
});
|
|
|
|
let process = null;
|
|
|
|
app.post('/play', (req, res) => {
|
|
if (process) {
|
|
process.kill();
|
|
}
|
|
|
|
let video = req.query.video;
|
|
|
|
console.log(video);
|
|
|
|
if (video.startsWith('/media/pi/USB')) {
|
|
fs.copyFileSync(video, path.join('/tmp/', path.basename(video)));
|
|
video = path.join('/tmp/', path.basename(video))
|
|
}
|
|
|
|
process = require('child_process').spawn('ffplay', ['-fs', '-loop', '2147483647', video], { stdio: 'overlapped' });
|
|
|
|
process.stdout.on('data', (data) => {
|
|
console.log(`stdout: ${data}`);
|
|
});
|
|
|
|
process.stderr.on('data', (data) => {
|
|
console.error(`stderr: ${data}`);
|
|
});
|
|
|
|
res.json({ status: 'ok' });
|
|
});
|
|
|
|
app.post('/upload', function (req, res) {
|
|
|
|
if (!req.files || Object.keys(req.files).length === 0) {
|
|
return res.status(400).send('No files were uploaded.');
|
|
}
|
|
|
|
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
|
|
const uploadPath = path.join(__dirname, '/public/videos/');
|
|
|
|
req.files.file.mv(uploadPath + req.files.file.name, function (err) {
|
|
if (err) {
|
|
return res.status(500);
|
|
}
|
|
});
|
|
|
|
res.redirect('/');
|
|
}); |