-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.js
executable file
·95 lines (78 loc) · 3.14 KB
/
backend.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
const express = require('express');
const fs = require('fs');
const multer = require('multer');
const thumbsupply = require('thumbsupply');
const process = require('process');
const path = require('path');
const getDuration = require('get-video-duration');
const app = express();
var storage = multer.diskStorage({
destination: process.cwd() + '/uploads',
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
if (false === fs.existsSync('public/img/miniatures/uploads/')) {
fs.mkdirSync('public/img/miniatures/uploads/');
}
// Step 1: parse data.js
const videosData = require('./data.js');
function addVideo(videoMetadata)
{
// TODO: checker les valeurs de videoMetadata
videosData.films.push(videoMetadata);
}
// Step 2: respond to data.js request
app.get('/data.js', function(req, res) {
res.set('Content-Type', 'application/javascript');
res.send('var data = ' + JSON.stringify(videosData));
});
// Step3: allow users to upload videos
function formatTime(time) {
if (time < 1) return '00:00';
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time / 60) % 60); // modulo 60 is useful if the time is longer than one hour
const seconds = Math.floor(time % 60);
let formattedTime = "";
if (hours !== 0) {
formattedTime += hours + ':';
}
formattedTime += minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
return formattedTime;
}
app.post('/upload', upload.single('video'), function(req, res) {
getDuration(req.file.path)
.then((duration) => {
const formattedDuration = formatTime(Math.floor(duration));
thumbsupply.generateThumbnail(req.file.path, {size: thumbsupply.ThumbSize.LARGE, timestamp: "10%"})
.then(thumb => {
const thumbPath = 'public/img/miniatures/uploads/' + path.basename(req.file.path) + '.png';
fs.renameSync(thumb, thumbPath);
addVideo({
src: 'uploads/' + req.file.filename,
img: 'uploads/' + path.basename(req.file.path) + '.png',
title: req.body.title,
duration: formattedDuration,
author: req.body.author,
author_url: 'http://localhost:3000/',
description: req.body.description,
year: parseInt(req.body.year),
audio_language: 'N/A',
sub_language: 'N/A',
rating: 0,
category: req.body.category,
});
res.redirect('/');
})
})
});
// Step 4: specify where static files are located and start server
app.use(express.static('public'));
app.use('/videos/uploads', express.static('uploads', {
setHeaders(res) {
// HOTFIX: Chrome requires the video mime to be set in the headers
res.set('Content-Type', 'video/mp4');
}
}));
app.listen(3000);