From 1eb5a8eaed0a69631b04b8019b6784be053fa04e Mon Sep 17 00:00:00 2001 From: Filip Borum Poulsen Date: Mon, 18 Mar 2024 19:30:09 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + index.js | 95 ++++++++++++++++++++++++++++++++++++++++++ package.json | 15 +++++++ public/index.html | 20 +++++++++ public/main.js | 18 ++++++++ public/videos/.gitkeep | 0 6 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json create mode 100644 public/index.html create mode 100644 public/main.js create mode 100644 public/videos/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..3e75e10 --- /dev/null +++ b/index.js @@ -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('/'); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..7e4cfda --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..130871e --- /dev/null +++ b/public/index.html @@ -0,0 +1,20 @@ + + + + + + PI Video Player + + +
+ + +
+
+ + + \ No newline at end of file diff --git a/public/main.js b/public/main.js new file mode 100644 index 0000000..01a4bb4 --- /dev/null +++ b/public/main.js @@ -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(); \ No newline at end of file diff --git a/public/videos/.gitkeep b/public/videos/.gitkeep new file mode 100644 index 0000000..e69de29