mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-03 19:11:29 +03:00
Merge pull request #12 from Tzahi12345/serve-nodejs
Serve frontend app through nodejs
This commit is contained in:
@@ -2,6 +2,7 @@ var async = require('async');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var youtubedl = require('youtube-dl');
|
||||
var compression = require('compression');
|
||||
var https = require('https');
|
||||
var express = require("express");
|
||||
var bodyParser = require("body-parser");
|
||||
@@ -30,7 +31,7 @@ db.defaults(
|
||||
// config values
|
||||
var frontendUrl = null;
|
||||
var backendUrl = null;
|
||||
var backendPort = 17442;
|
||||
var backendPort = null;
|
||||
var usingEncryption = null;
|
||||
var basePath = null;
|
||||
var audioFolderPath = null;
|
||||
@@ -68,11 +69,6 @@ var descriptors = {};
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.get('/using-encryption', function(req, res) {
|
||||
res.send(usingEncryption);
|
||||
res.end("yes");
|
||||
});
|
||||
|
||||
// objects
|
||||
|
||||
function File(id, title, thumbnailURL, isAudio, duration) {
|
||||
@@ -89,7 +85,7 @@ function startServer() {
|
||||
if (usingEncryption)
|
||||
{
|
||||
https.createServer(options, app).listen(backendPort, function() {
|
||||
console.log('HTTPS: Anchor set on 17442');
|
||||
console.log('HTTPS: Started on PORT ' + backendPort);
|
||||
});
|
||||
}
|
||||
else
|
||||
@@ -125,11 +121,9 @@ async function loadConfig() {
|
||||
// get config library
|
||||
// config = require('config');
|
||||
|
||||
frontendUrl = !debugMode ? config_api.getConfigItem('ytdl_frontend_url') : 'http://localhost:4200';
|
||||
backendUrl = config_api.getConfigItem('ytdl_backend_url')
|
||||
backendPort = 17442;
|
||||
url = !debugMode ? config_api.getConfigItem('ytdl_url') : 'http://localhost:4200';
|
||||
backendPort = config_api.getConfigItem('ytdl_port');
|
||||
usingEncryption = config_api.getConfigItem('ytdl_use_encryption');
|
||||
basePath = config_api.getConfigItem('ytdl_base_path');
|
||||
audioFolderPath = config_api.getConfigItem('ytdl_audio_folder_path');
|
||||
videoFolderPath = config_api.getConfigItem('ytdl_video_folder_path');
|
||||
downloadOnlyMode = config_api.getConfigItem('ytdl_download_only_mode');
|
||||
@@ -153,7 +147,7 @@ async function loadConfig() {
|
||||
};
|
||||
}
|
||||
|
||||
url_domain = new URL(frontendUrl);
|
||||
url_domain = new URL(url);
|
||||
|
||||
// start the server here
|
||||
startServer();
|
||||
@@ -506,7 +500,21 @@ app.use(function(req, res, next) {
|
||||
next();
|
||||
});
|
||||
|
||||
app.post('/tomp3', function(req, res) {
|
||||
app.use(compression());
|
||||
|
||||
app.get('/api/config', function(req, res) {
|
||||
let config_file = config_api.getConfigFile();
|
||||
res.send({
|
||||
config_file: config_file,
|
||||
success: !!config_file
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/using-encryption', function(req, res) {
|
||||
res.send(usingEncryption);
|
||||
});
|
||||
|
||||
app.post('/api/tomp3', function(req, res) {
|
||||
var url = req.body.url;
|
||||
var date = Date.now();
|
||||
var audiopath = '%(title)s';
|
||||
@@ -594,7 +602,7 @@ app.post('/tomp3', function(req, res) {
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/tomp4', function(req, res) {
|
||||
app.post('/api/tomp4', function(req, res) {
|
||||
var url = req.body.url;
|
||||
var date = Date.now();
|
||||
var path = videoFolderPath;
|
||||
@@ -688,7 +696,7 @@ app.post('/tomp4', function(req, res) {
|
||||
});
|
||||
|
||||
// gets the status of the mp3 file that's being downloaded
|
||||
app.post('/fileStatusMp3', function(req, res) {
|
||||
app.post('/api/fileStatusMp3', function(req, res) {
|
||||
var name = decodeURI(req.body.name + "");
|
||||
var exists = "";
|
||||
var fullpath = audioFolderPath + name + ".mp3";
|
||||
@@ -710,7 +718,7 @@ app.post('/fileStatusMp3', function(req, res) {
|
||||
});
|
||||
|
||||
// gets the status of the mp4 file that's being downloaded
|
||||
app.post('/fileStatusMp4', function(req, res) {
|
||||
app.post('/api/fileStatusMp4', function(req, res) {
|
||||
var name = decodeURI(req.body.name);
|
||||
var exists = "";
|
||||
var fullpath = videoFolderPath + name + ".mp4";
|
||||
@@ -730,7 +738,7 @@ app.post('/fileStatusMp4', function(req, res) {
|
||||
});
|
||||
|
||||
// gets all download mp3s
|
||||
app.post('/getMp3s', function(req, res) {
|
||||
app.post('/api/getMp3s', function(req, res) {
|
||||
var mp3s = [];
|
||||
var playlists = db.get('playlists.audio').value();
|
||||
var files = recFindByExt(audioFolderPath, 'mp3'); // fs.readdirSync(audioFolderPath);
|
||||
@@ -762,7 +770,7 @@ app.post('/getMp3s', function(req, res) {
|
||||
});
|
||||
|
||||
// gets all download mp4s
|
||||
app.post('/getMp4s', function(req, res) {
|
||||
app.post('/api/getMp4s', function(req, res) {
|
||||
var mp4s = [];
|
||||
var playlists = db.get('playlists.video').value();
|
||||
var fullpath = videoFolderPath;
|
||||
@@ -794,7 +802,7 @@ app.post('/getMp4s', function(req, res) {
|
||||
res.end("yes");
|
||||
});
|
||||
|
||||
app.post('/createPlaylist', async (req, res) => {
|
||||
app.post('/api/createPlaylist', async (req, res) => {
|
||||
let playlistName = req.body.playlistName;
|
||||
let fileNames = req.body.fileNames;
|
||||
let type = req.body.type;
|
||||
@@ -817,7 +825,7 @@ app.post('/createPlaylist', async (req, res) => {
|
||||
})
|
||||
});
|
||||
|
||||
app.post('/updatePlaylist', async (req, res) => {
|
||||
app.post('/api/updatePlaylist', async (req, res) => {
|
||||
let playlistID = req.body.playlistID;
|
||||
let fileNames = req.body.fileNames;
|
||||
let type = req.body.type;
|
||||
@@ -843,7 +851,7 @@ app.post('/updatePlaylist', async (req, res) => {
|
||||
})
|
||||
});
|
||||
|
||||
app.post('/deletePlaylist', async (req, res) => {
|
||||
app.post('/api/deletePlaylist', async (req, res) => {
|
||||
let playlistID = req.body.playlistID;
|
||||
let type = req.body.type;
|
||||
|
||||
@@ -865,7 +873,7 @@ app.post('/deletePlaylist', async (req, res) => {
|
||||
});
|
||||
|
||||
// deletes mp3 file
|
||||
app.post('/deleteMp3', async (req, res) => {
|
||||
app.post('/api/deleteMp3', async (req, res) => {
|
||||
var name = req.body.name;
|
||||
var fullpath = audioFolderPath + name + ".mp3";
|
||||
var wasDeleted = false;
|
||||
@@ -885,7 +893,7 @@ app.post('/deleteMp3', async (req, res) => {
|
||||
});
|
||||
|
||||
// deletes mp4 file
|
||||
app.post('/deleteMp4', async (req, res) => {
|
||||
app.post('/api/deleteMp4', async (req, res) => {
|
||||
var name = req.body.name;
|
||||
var fullpath = videoFolderPath + name + ".mp4";
|
||||
var wasDeleted = false;
|
||||
@@ -904,7 +912,7 @@ app.post('/deleteMp4', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/downloadFile', async (req, res) => {
|
||||
app.post('/api/downloadFile', async (req, res) => {
|
||||
let fileNames = req.body.fileNames;
|
||||
let is_playlist = req.body.is_playlist;
|
||||
let type = req.body.type;
|
||||
@@ -927,7 +935,7 @@ app.post('/downloadFile', async (req, res) => {
|
||||
res.sendFile(file);
|
||||
});
|
||||
|
||||
app.post('/deleteFile', async (req, res) => {
|
||||
app.post('/api/deleteFile', async (req, res) => {
|
||||
let fileName = req.body.fileName;
|
||||
let type = req.body.type;
|
||||
if (type === 'audio') {
|
||||
@@ -938,7 +946,7 @@ app.post('/deleteFile', async (req, res) => {
|
||||
res.send()
|
||||
});
|
||||
|
||||
app.get('/video/:id', function(req , res){
|
||||
app.get('/api/video/:id', function(req , res){
|
||||
var head;
|
||||
let id = decodeURI(req.params.id);
|
||||
const path = "video/" + id + '.mp4';
|
||||
@@ -978,7 +986,7 @@ app.get('/video/:id', function(req , res){
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/audio/:id', function(req , res){
|
||||
app.get('/api/audio/:id', function(req , res){
|
||||
var head;
|
||||
let id = decodeURI(req.params.id);
|
||||
let path = "audio/" + id + '.mp3';
|
||||
@@ -1020,7 +1028,7 @@ app.get('/audio/:id', function(req , res){
|
||||
});
|
||||
|
||||
|
||||
app.post('/getVideoInfos', async (req, res) => {
|
||||
app.post('/api/getVideoInfos', async (req, res) => {
|
||||
let fileNames = req.body.fileNames;
|
||||
let urlMode = !!req.body.urlMode;
|
||||
let type = req.body.type;
|
||||
@@ -1039,3 +1047,22 @@ app.get('/audio/:id', function(req , res){
|
||||
success: !!result
|
||||
})
|
||||
});
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
//if the request is not html then move along
|
||||
var accept = req.accepts('html', 'json', 'xml');
|
||||
if (accept !== 'html') {
|
||||
return next();
|
||||
}
|
||||
|
||||
// if the request has a '.' assume that it's for a file, move along
|
||||
var ext = path.extname(req.path);
|
||||
if (ext !== '') {
|
||||
return next();
|
||||
}
|
||||
|
||||
fs.createReadStream('./public/index.html').pipe(res);
|
||||
|
||||
});
|
||||
|
||||
app.use(express.static('./public'));
|
||||
|
||||
Reference in New Issue
Block a user