Initial commit

install-script
Filip Borum Poulsen 2 years ago
commit 1eb5a8eaed

1
.gitignore vendored

@ -0,0 +1 @@
node_modules

@ -0,0 +1,95 @@
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'
];
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', [video], {env: {DISPLAY: ':0'}});
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('/');
});

@ -0,0 +1,15 @@
{
"name": "video-player",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.3",
"express-fileupload": "^1.5.0"
}
}

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PI Video Player</title>
</head>
<body>
<form ref='uploadForm'
id='uploadForm'
action='/upload'
method='post'
encType="multipart/form-data">
<input type="file" name="file" />
<input type='submit' value='Upload!' />
</form>
<div id="videos"></div>
<script src="/main.js"></script>
</body>
</html>

@ -0,0 +1,18 @@
async function main() {
const response = await fetch('/videos');
const videos = await response.json();
const videoDiv = document.getElementById('videos');
videoDiv.innerHTML = '';
for (const video of videos) {
const button = document.createElement('button');
button.textContent = video.name;
button.addEventListener('click', () => {
fetch(`/play?video=${video.path}`, { method: 'POST' });
});
videoDiv.appendChild(button);
}
}
main();
Loading…
Cancel
Save