This repository has been archived by the owner on May 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.js
387 lines (358 loc) · 16.2 KB
/
server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"use strict";
const path = require("path");
const express = require("express");
const favicon = require("serve-favicon");
const upload = require("express-fileupload");
const videoData = require("./backend/scripts/data-videos");
const deleteData = require("./backend/scripts/delete-data");
const stream = require("./backend/scripts/stream-video-image");
const userSettings = require("./backend/scripts/user-settings");
const availableVideos = require("./backend/scripts/available-videos");
const ffmpegUploadVideo = require("./backend/scripts/ffmpeg-upload-video");
const ffmpegDownloadVideo = require("./backend/scripts/ffmpeg-download-video");
const ffmpegDownloadStream = require("./backend/scripts/ffmpeg-download-stream");
const currentDownloadVideos = require("./backend/scripts/current-download-videos");
const ffmpegUnfinishedVideo = require("./backend/scripts/ffmpeg-unfinished-videos");
const youtubedlDownloadVideo = require("./backend/scripts/youtubedl-download-video");
const ffmpegDownloadResponse = require("./backend/scripts/ffmpeg-download-response");
const ffmpegDownloadtrimedVideo = require("./backend/scripts/ffmpeg-download-trimed-video");
const app = express();
app.use(upload({
limits: { fileSize: 1024 * 1024 * 1024 * 2 },
}));
// show website icon on tab
app.use(favicon(path.join(__dirname, "client", "images", "favicon", "favicon.ico")));
// take uploaded video file and downloads it
app.post("/uploadVideoFile", uploadVideoFile);
function uploadVideoFile(req, res){
ffmpegUploadVideo.uploadVideoFile(req, res);
}
// converts url link to video link
app.post("/getVideoLinkFromUrl", express.json(), videoLinkFromUrl);
async function videoLinkFromUrl(req, res){
const getVideoLinkFromUrl = await youtubedlDownloadVideo.getVideoLinkFromUrl(req.body.url);
if (getVideoLinkFromUrl.message == "initializing") {
const checkDownloadResponse = setInterval(function(){
const getDownloadResponse = ffmpegDownloadResponse.getDownloadResponse([getVideoLinkFromUrl.fileName]);
if (getDownloadResponse !== undefined) {
if (getDownloadResponse.message !== undefined) {
if (getDownloadResponse.message !== "initializing") {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
if (typeof getDownloadResponse["message"]["video_url"] === "string" && typeof getDownloadResponse["message"]["video_file_format"] === "string") {
res.json(getDownloadResponse.message);
} else {
res.json("failed-get-video-url-from-provided-url");
}
}
} else {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json("failed-get-video-url-from-provided-url");
}
} else {
clearInterval(checkDownloadResponse);
res.json("failed-get-video-url-from-provided-url");
}
}, 50);
} else {
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getVideoLinkFromUrl.fileName);
if (getVideoLinkFromUrl.message !== undefined) {
if (typeof getVideoLinkFromUrl["message"]["video_url"] === "string" && typeof getVideoLinkFromUrl["message"]["video_file_format"] === "string") {
res.json(getVideoLinkFromUrl.message);
} else {
res.json("failed-get-video-url-from-provided-url");
}
} else {
res.json("failed-get-video-url-from-provided-url");
}
}
}
// update video player volume settings
app.post("/updateVideoPlayerVolume", express.json(), updateVideoPlayerVolume);
function updateVideoPlayerVolume(req, res) {
const updatedVolume = userSettings.updateVideoPlayerVolume(req.body.updatedVideoPlayerVolume, req.body.updatedVideoPlayerMuted);
res.json(updatedVolume);
}
// get video player settings
app.get("/getVideoPlayerSettings", getVideoPlayerSettings);
function getVideoPlayerSettings(req, res) {
res.json(userSettings.getUserSettings(["videoPlayer"]));
}
// get download confirmation status
app.get("/getDownloadConfirmation", getDownloadConfirmation);
function getDownloadConfirmation(req, res) {
const userConfirmationSettings = userSettings.getUserSettings(["download", "confirmation"]);
if (userConfirmationSettings !== "invalid array path" && userConfirmationSettings !== undefined) {
res.json(userConfirmationSettings);
} else {
res.json("userConfirmationSettings unavailable");
}
}
// update download confirmation by specified id & bool
app.post("/updateDownloadConfirmation", express.json(), updateDownloadConfirmation);
function updateDownloadConfirmation(req, res) {
if (typeof req.body.updateBool == "boolean") {
const checkIfValidID = userSettings.getUserSettings(["download", "confirmation", req.body.updateID]);
if (checkIfValidID !== "invalid array path" && checkIfValidID !== undefined) {
userSettings.updateUserSettingsData(["download", "confirmation", req.body.updateID], req.body.updateBool);
res.json("updated bool");
} else {
res.json("invalid id");
}
} else {
res.json("invalid bool");
}
}
// get video thumbnail by video id and thumbnail number header
app.get("/thumbnail/:videoID/:thumbnailID", streamThumbnailById);
async function streamThumbnailById(req, res){
const streamThumbnail = await stream.streamThumbnail(req.params.videoID, req.params.thumbnailID, res);
try {
if (streamThumbnail.status == 200) {
streamThumbnail.message;
} else {
res.status(streamThumbnail.status).redirect(streamThumbnail.redirect);
}
} catch (error) {
res.status(404).redirect("/");
}
}
// delete video permently by video id header
app.post("/delete-video-data-permanently", express.json(), deleteAllVideoData);
function deleteAllVideoData(req, res){
const checkBeforeDataDeletion = deleteData.checkIfCompressedVideoIsDownloadingBeforeVideoDataDeletion(req.body.id, req.body.folderIDPath);
if (checkBeforeDataDeletion.message == "initializing") {
const checkDownloadResponse = setInterval(function(){
const getDownloadResponse = ffmpegDownloadResponse.getDownloadResponse([checkBeforeDataDeletion.id]);
if (getDownloadResponse !== undefined) {
if (getDownloadResponse.message !== undefined) {
if (getDownloadResponse.message !== "initializing") {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.id);
res.json(getDownloadResponse.message);
}
} else {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.id);
res.json("response-not-found");
}
} else {
clearInterval(checkDownloadResponse);
res.json("response-not-found");
}
}, 50);
} else {
if (checkBeforeDataDeletion.message !== undefined) {
res.json(checkBeforeDataDeletion.message);
} else {
res.json(checkBeforeDataDeletion);
}
}
}
// stream original video by video id header
app.get("/video/:id", streamOriginalVideoById);
async function streamOriginalVideoById(req, res){
const streamVideo = await stream.streamVideo(req.params.id, false, req, res);
try {
if (streamVideo.status == 200 || streamVideo.status == 206) {
streamVideo.message;
} else if (streamVideo.status == 416) {
res.status(streamVideo.status).send(streamVideo.message);
} else {
res.status(streamVideo.status).redirect(streamVideo.redirect);
}
} catch (error) {
res.status(404).redirect("/");
}
}
// stream compressed video by video id header
app.get("/compressed/:id", streamCompressedVideoById);
async function streamCompressedVideoById(req, res){
const streamVideo = await stream.streamVideo(req.params.id, true, req, res);
try {
if (streamVideo.status == 200 || streamVideo.status == 206) {
streamVideo.message;
} else if (streamVideo.status == 416) {
res.status(streamVideo.status).send(streamVideo.message);
} else {
res.status(streamVideo.status).redirect(streamVideo.redirect);
}
} catch (error) {
res.status(404).redirect("/");
}
}
// create Folder at availableVideos
app.post("/createFolder", express.json(), createFolder);
function createFolder(req, res){
res.json(availableVideos.createFolder(req.body.folderIDPath, req.body.folderTitle));
}
// input selected element id out of folder element at availableVideos
app.post("/inputSelectedIDOutOfFolderID", express.json(), inputSelectedIDOutOfFolderID);
async function inputSelectedIDOutOfFolderID(req, res){
res.json(await availableVideos.inputSelectedIDOutOfFolderID(req.body.selectedID, req.body.folderID, req.body.folderIDPath));
}
// input selected element into folder element at availableVideos
app.post("/inputSelectedIDIntoFolderID", express.json(), inputSelectedIDIntoFolderID);
async function inputSelectedIDIntoFolderID(req, res){
res.json(await availableVideos.inputSelectedIDIntoFolderID(req.body.selectedID, req.body.folderID, req.body.folderIDPath));
}
// move selected id data to before target id data at available video details
app.post("/moveSelectedIdBeforeTargetIdAtAvailableVideoDetails", express.json(), moveSelectedIdBeforeTargetIdAtAvailableVideoDetails);
async function moveSelectedIdBeforeTargetIdAtAvailableVideoDetails(req, res){
res.json(await availableVideos.moveSelectedIdBeforeTargetIdAtAvailableVideoDetails(req.body.selectedID, req.body.targetID, req.body.folderIDPath));
}
// move selected id data to after target id data at available video details
app.post("/moveSelectedIdAfterTargetIdAtAvailableVideoDetails", express.json(), moveSelectedIdAfterTargetIdAtAvailableVideoDetails);
async function moveSelectedIdAfterTargetIdAtAvailableVideoDetails(req, res){
res.json(await availableVideos.moveSelectedIdAfterTargetIdAtAvailableVideoDetails(req.body.selectedID, req.body.targetID, req.body.folderIDPath));
}
// change title of video
app.post("/changeVideoTitle", express.json(), changeVideoTitle);
async function changeVideoTitle(req, res){
res.json(await availableVideos.changeTitle(req.body.videoID, req.body.newVideoTitle, req.body.folderIDPath));
}
// get video data for specified video by id header
app.get("/video-data/:id", findVideosByID);
function findVideosByID(req, res){
res.json(videoData.findVideosByID(req.params.id));
}
// get all available video data header
app.get("/all-available-video-data", getAllAvailableVideos);
function getAllAvailableVideos(req, res){
res.json(availableVideos.getAvailableVideos());
}
// download live video stream header
app.post("/downloadVideoStream", express.json(), downloadVideoStream);
async function downloadVideoStream(req, res){
const downloadVideoStream = await ffmpegDownloadStream.downloadVideoStream(req.body.videoSrc, req.body.videoType);
if (downloadVideoStream.message == "initializing") {
const checkDownloadResponse = setInterval(function(){
const getDownloadResponse = ffmpegDownloadResponse.getDownloadResponse([downloadVideoStream.fileName]);
if (getDownloadResponse !== undefined) {
if (getDownloadResponse.message !== undefined) {
if (getDownloadResponse.message !== "initializing") {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json(getDownloadResponse.message);
}
} else {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json("response-not-found");
}
} else {
clearInterval(checkDownloadResponse);
res.json("response-not-found");
}
}, 50);
} else {
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(downloadVideoStream.fileName);
if (downloadVideoStream.message !== undefined) {
res.json(downloadVideoStream.message);
} else {
res.json(downloadVideoStream);
}
}
}
// download video header
app.post("/downloadVideo", express.json(), downloadVideo);
async function downloadVideo(req, res){
const downloadVideo = await ffmpegDownloadVideo.downloadVideo(req.body.videoSrc, req.body.videoType);
if (downloadVideo.message == "initializing") {
const checkDownloadResponse = setInterval(function(){
const getDownloadResponse = ffmpegDownloadResponse.getDownloadResponse([downloadVideo.fileName]);
if (getDownloadResponse !== undefined) {
if (getDownloadResponse.message !== undefined) {
if (getDownloadResponse.message !== "initializing") {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json(getDownloadResponse.message);
}
} else {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json("response-not-found");
}
} else {
clearInterval(checkDownloadResponse);
res.json("response-not-found");
}
}, 50);
} else {
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(downloadVideo.fileName);
if (downloadVideo.message !== undefined) {
res.json(downloadVideo.message);
} else {
res.json(downloadVideo);
}
}
}
// download video from specified section header
app.post("/trimVideo", express.json(), trimVideo);
async function trimVideo(req, res){
const downloadVideo = await ffmpegDownloadtrimedVideo.trimVideo(req.body.videoSrc, req.body.videoType, req.body.newStartTime, req.body.newEndTime);
if (downloadVideo.message == "initializing") {
const checkDownloadResponse = setInterval(function(){
const getDownloadResponse = ffmpegDownloadResponse.getDownloadResponse([downloadVideo.fileName]);
if (getDownloadResponse !== undefined) {
if (getDownloadResponse.message !== undefined) {
if (getDownloadResponse.message !== "initializing") {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json(getDownloadResponse.message);
}
} else {
clearInterval(checkDownloadResponse);
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(getDownloadResponse.fileName);
res.json("response-not-found");
}
} else {
clearInterval(checkDownloadResponse);
res.json("response-not-found");
}
}, 50);
} else {
ffmpegDownloadResponse.deleteSpecifiedDownloadResponse(downloadVideo.fileName);
if (downloadVideo.message !== undefined) {
res.json(downloadVideo.message);
} else {
res.json(downloadVideo);
}
}
}
// stop downloading live video stream header
app.post("/stopDownloadVideoStream", express.json(), stopDownloadVideoStream);
async function stopDownloadVideoStream(req, res){
res.json(await ffmpegDownloadStream.stopDownloadVideoStream(req.body.id));
}
// load path name /saved/videos with index.html page
app.all("/saved/videos", savedVideos);
function savedVideos(req, res){
res.sendFile(path.join(__dirname, "client", "index.html"));
}
//show current downloads
app.get("/current-video-downloads", getCurrentDownloads);
function getCurrentDownloads(req, res){
res.json(currentDownloadVideos.getCurrentDownloads());
}
// complete download unfinnished video by specified video id header
app.post("/complete-unfinnished-video-download", express.json(), completeUnfinnishedVideoDownload);
function completeUnfinnishedVideoDownload(req, res){
res.json(ffmpegUnfinishedVideo.completeUnfinnishedVideoDownload(req.body.id));
}
// adds html as extensions, dont need to write index.html
app.use(express.static("client", { extensions: ["html"] }));
// if page not found redirect to home page
app.get("*", function(req, res){
res.status(404).redirect("/");
});
// application runs on port 8080
app.set("port", (process.env.PORT || 8080));
app.listen(app.get("port"), function() {
console.log("Server running at:" + app.get("port"));
// cheack for available unfinished video downloads
ffmpegUnfinishedVideo.cheackForAvailabeUnFinishedVideoDownloads();
});
// export website url
exports.url = `http://localhost:${app.get("port")}`;