Merge pull request #12 from Tzahi12345/serve-nodejs

Serve frontend app through nodejs
This commit is contained in:
Tzahi12345
2020-03-01 00:50:20 -05:00
committed by GitHub
15 changed files with 128 additions and 113 deletions

View File

@@ -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'));

View File

@@ -108,5 +108,6 @@ module.exports = {
getConfigItem: getConfigItem,
setConfigItem: setConfigItem,
setConfigItems: setConfigItems,
getConfigFile: getConfigFile,
CONFIG_ITEMS: CONFIG_ITEMS
}

View File

@@ -1,8 +1,8 @@
{
"YoutubeDLMaterial": {
"Host": {
"frontendurl": "http://example.com",
"backendurl": "http://example.com:17442/"
"url": "http://example.com",
"port": "17442"
},
"Encryption": {
"use-encryption": false,
@@ -10,7 +10,6 @@
"key-file-path": "/etc/letsencrypt/live/example.com/privkey.pem"
},
"Downloader": {
"path-base": "http://example.com:17442/",
"path-audio": "audio/",
"path-video": "video/"
},

View File

@@ -1,16 +1,15 @@
{
"YoutubeDLMaterial": {
"Host": {
"frontendurl": "https://example.com",
"backendurl": "https://example.com:17442/"
},
"Host": {
"url": "https://example.com",
"port": "17442"
},
"Encryption": {
"use-encryption": true,
"cert-file-path": "/etc/letsencrypt/live/example.com/fullchain.pem",
"key-file-path": "/etc/letsencrypt/live/example.com/privkey.pem"
},
"Downloader": {
"path-base": "https://example.com:17442/",
"path-audio": "audio/",
"path-video": "video/"
},

View File

@@ -2,13 +2,13 @@ var config = require('config');
let CONFIG_ITEMS = {
// Host
'ytdl_frontend_url': {
'key': 'ytdl_frontend_url',
'path': 'YoutubeDLMaterial.Host.frontendurl'
'ytdl_url': {
'key': 'ytdl_url',
'path': 'YoutubeDLMaterial.Host.url'
},
'ytdl_backend_url': {
'key': 'ytdl_backend_url',
'path': 'YoutubeDLMaterial.Host.backendurl'
'ytdl_port': {
'key': 'ytdl_port',
'path': 'YoutubeDLMaterial.Host.port'
},
// Encryption
@@ -26,10 +26,6 @@ let CONFIG_ITEMS = {
},
// Downloader
'ytdl_base_path': {
'key': 'ytdl_base_path',
'path': 'YoutubeDLMaterial.Downloader.path-base'
},
'ytdl_audio_folder_path': {
'key': 'ytdl_audio_folder_path',
'path': 'YoutubeDLMaterial.Downloader.path-audio'

View File

@@ -20,6 +20,7 @@
"dependencies": {
"archiver": "^3.1.1",
"async": "^3.1.0",
"compression": "^1.7.4",
"config": "^3.2.3",
"exe": "^1.0.2",
"express": "^4.17.1",