Merge pull request #141 from web-connect/feature/remove_whitespace

Removing extra white spaces
This commit is contained in:
Tzahi12345
2020-06-27 12:43:44 -04:00
committed by GitHub
7 changed files with 107 additions and 107 deletions

View File

@@ -25,7 +25,7 @@ const fetch = require('node-fetch');
var URL = require('url').URL; var URL = require('url').URL;
const shortid = require('shortid') const shortid = require('shortid')
const url_api = require('url'); const url_api = require('url');
var config_api = require('./config.js'); var config_api = require('./config.js');
var subscriptions_api = require('./subscriptions') var subscriptions_api = require('./subscriptions')
const CONSTS = require('./consts') const CONSTS = require('./consts')
const { spawn } = require('child_process') const { spawn } = require('child_process')
@@ -65,7 +65,7 @@ const logger = winston.createLogger({
defaultMeta: {}, defaultMeta: {},
transports: [ transports: [
// //
// - Write to all logs with level `info` and below to `combined.log` // - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`. // - Write all logs error (and below) to `error.log`.
// //
new winston.transports.File({ filename: 'appdata/logs/error.log', level: 'error' }), new winston.transports.File({ filename: 'appdata/logs/error.log', level: 'error' }),
@@ -83,7 +83,7 @@ subscriptions_api.initialize(db, users_db, logger, db_api);
// Set some defaults // Set some defaults
db.defaults( db.defaults(
{ {
playlists: { playlists: {
audio: [], audio: [],
video: [] video: []
@@ -100,7 +100,7 @@ db.defaults(
}).write(); }).write();
users_db.defaults( users_db.defaults(
{ {
users: [], users: [],
roles: { roles: {
"admin": { "admin": {
@@ -272,7 +272,7 @@ async function startServer() {
async function restartServer() { async function restartServer() {
const restartProcess = () => { const restartProcess = () => {
spawn('node', ['app.js'], { spawn('node', ['app.js'], {
detached: true, detached: true,
stdio: 'inherit' stdio: 'inherit'
}).unref() }).unref()
process.exit() process.exit()
@@ -292,7 +292,7 @@ async function updateServer(tag) {
return false; return false;
} }
} }
return new Promise(async resolve => { return new Promise(async resolve => {
// backup current dir // backup current dir
updaterStatus = { updaterStatus = {
@@ -423,7 +423,7 @@ async function downloadReleaseZip(tag) {
await fetchFile(latest_zip_link, output_path, 'update ' + tag); await fetchFile(latest_zip_link, output_path, 'update ' + tag);
resolve(true); resolve(true);
}); });
} }
async function installDependencies() { async function installDependencies() {
@@ -432,7 +432,7 @@ async function installDependencies() {
child_process.execSync('npm install',{stdio:[0,1,2]}); child_process.execSync('npm install',{stdio:[0,1,2]});
resolve(true); resolve(true);
}); });
} }
async function backupServerLite() { async function backupServerLite() {
@@ -445,12 +445,12 @@ async function backupServerLite() {
gzip: true, gzip: true,
zlib: { level: 9 } // Sets the compression level. zlib: { level: 9 } // Sets the compression level.
}); });
archive.on('error', function(err) { archive.on('error', function(err) {
logger.error(err); logger.error(err);
resolve(false); resolve(false);
}); });
// pipe archive data to the output file // pipe archive data to the output file
archive.pipe(output); archive.pipe(output);
@@ -558,7 +558,7 @@ async function loadConfig() {
resolve(true); resolve(true);
}); });
} }
function loadConfigValues() { function loadConfigValues() {
@@ -613,9 +613,9 @@ function calculateSubcriptionRetrievalDelay(subscriptions_amount) {
return subinterval_in_ms; return subinterval_in_ms;
} }
async function watchSubscriptions() { async function watchSubscriptions() {
let subscriptions = null; let subscriptions = null;
const multiUserMode = config_api.getConfigItem('ytdl_multi_user_mode'); const multiUserMode = config_api.getConfigItem('ytdl_multi_user_mode');
if (multiUserMode) { if (multiUserMode) {
subscriptions = []; subscriptions = [];
@@ -674,7 +674,7 @@ function getEnvConfigItems() {
config_items.push(config_item); config_items.push(config_item);
} }
} }
return config_items; return config_items;
} }
@@ -720,7 +720,7 @@ function getMp4s(relative_path = true) {
var file_path = file.substring(videoFolderPath.length, file.length); var file_path = file.substring(videoFolderPath.length, file.length);
var stats = fs.statSync(file); var stats = fs.statSync(file);
var id = file_path.substring(0, file_path.length-4); var id = file_path.substring(0, file_path.length-4);
var jsonobj = utils.getJSONMp4(id, videoFolderPath); var jsonobj = utils.getJSONMp4(id, videoFolderPath);
if (!jsonobj) continue; if (!jsonobj) continue;
@@ -763,7 +763,7 @@ function getFileSizeMp3(name)
var obj = JSON.parse(fs.readFileSync(jsonPath, 'utf8')); var obj = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
else else
var obj = 0; var obj = 0;
return obj.filesize; return obj.filesize;
} }
@@ -783,7 +783,7 @@ function getFileSizeMp4(name)
} }
} }
} }
return filesize; return filesize;
} }
@@ -845,12 +845,12 @@ async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvid
gzip: true, gzip: true,
zlib: { level: 9 } // Sets the compression level. zlib: { level: 9 } // Sets the compression level.
}); });
archive.on('error', function(err) { archive.on('error', function(err) {
logger.error(err); logger.error(err);
throw err; throw err;
}); });
// pipe archive data to the output file // pipe archive data to the output file
archive.pipe(output); archive.pipe(output);
@@ -867,9 +867,9 @@ async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvid
setTimeout(function() { setTimeout(function() {
resolve(path.join(zipFolderPath,outputName + '.zip')); resolve(path.join(zipFolderPath,outputName + '.zip'));
}, 100); }, 100);
}); });
} }
@@ -902,7 +902,7 @@ async function deleteAudioFile(name, blacklistMode = false) {
} catch(e) { } catch(e) {
} }
} }
let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive'); let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive');
if (useYoutubeDLArchive) { if (useYoutubeDLArchive) {
@@ -937,7 +937,7 @@ async function deleteAudioFile(name, blacklistMode = false) {
// TODO: tell user that the file didn't exist // TODO: tell user that the file didn't exist
resolve(true); resolve(true);
} }
}); });
} }
@@ -960,7 +960,7 @@ async function deleteVideoFile(name, customPath = null, blacklistMode = false) {
} catch(e) { } catch(e) {
} }
} }
let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive'); let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive');
if (useYoutubeDLArchive) { if (useYoutubeDLArchive) {
@@ -995,16 +995,16 @@ async function deleteVideoFile(name, customPath = null, blacklistMode = false) {
// TODO: tell user that the file didn't exist // TODO: tell user that the file didn't exist
resolve(true); resolve(true);
} }
}); });
} }
function recFindByExt(base,ext,files,result) function recFindByExt(base,ext,files,result)
{ {
files = files || fs.readdirSync(base) files = files || fs.readdirSync(base)
result = result || [] result = result || []
files.forEach( files.forEach(
function (file) { function (file) {
var newbase = path.join(base,file) var newbase = path.join(base,file)
if ( fs.statSync(newbase).isDirectory() ) if ( fs.statSync(newbase).isDirectory() )
@@ -1016,7 +1016,7 @@ function recFindByExt(base,ext,files,result)
if ( file.substr(-1*(ext.length+1)) == '.' + ext ) if ( file.substr(-1*(ext.length+1)) == '.' + ext )
{ {
result.push(newbase) result.push(newbase)
} }
} }
} }
) )
@@ -1155,7 +1155,7 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
} }
options.customFileFolderPath = fileFolderPath; options.customFileFolderPath = fileFolderPath;
} }
const downloadConfig = await generateArgs(url, type, options); const downloadConfig = await generateArgs(url, type, options);
// adds download to download helper // adds download to download helper
@@ -1210,11 +1210,11 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
var modified_file_name = output_json ? output_json['title'] : null; var modified_file_name = output_json ? output_json['title'] : null;
if (!output_json) { if (!output_json) {
continue; continue;
} }
// get filepath with no extension // get filepath with no extension
const filepath_no_extension = removeFileExtension(output_json['_filename']); const filepath_no_extension = removeFileExtension(output_json['_filename']);
var full_file_path = filepath_no_extension + ext; var full_file_path = filepath_no_extension + ext;
var file_name = filepath_no_extension.substring(fileFolderPath.length, filepath_no_extension.length); var file_name = filepath_no_extension.substring(fileFolderPath.length, filepath_no_extension.length);
@@ -1254,7 +1254,7 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
download['complete'] = true; download['complete'] = true;
download['fileNames'] = is_playlist ? file_names : [full_file_path] download['fileNames'] = is_playlist ? file_names : [full_file_path]
updateDownloads(); updateDownloads();
var videopathEncoded = encodeURIComponent(file_names[0]); var videopathEncoded = encodeURIComponent(file_names[0]);
resolve({ resolve({
@@ -1334,7 +1334,7 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
'use strict' 'use strict'
logger.info('file ' + info._filename + ' already downloaded.') logger.info('file ' + info._filename + ' already downloaded.')
}) })
let download_pos = 0; let download_pos = 0;
video.on('data', function data(chunk) { video.on('data', function data(chunk) {
download_pos += chunk.length download_pos += chunk.length
@@ -1359,7 +1359,7 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
// filename fix // filename fix
video_info['_filename'] = removeFileExtension(video_info['_filename']) + '.mp3'; video_info['_filename'] = removeFileExtension(video_info['_filename']) + '.mp3';
// ID3 tagging // ID3 tagging
let tags = { let tags = {
title: video_info['title'], title: video_info['title'],
artist: video_info['artist'] ? video_info['artist'] : video_info['uploader'] artist: video_info['artist'] ? video_info['artist'] : video_info['uploader']
@@ -1403,7 +1403,7 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
resolve(false); resolve(false);
}); });
}); });
} }
async function generateArgs(url, type, options) { async function generateArgs(url, type, options) {
@@ -1475,7 +1475,7 @@ async function generateArgs(url, type, options) {
logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.'); logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.');
} }
} }
if (!useDefaultDownloadingAgent && customDownloadingAgent) { if (!useDefaultDownloadingAgent && customDownloadingAgent) {
downloadConfig.splice(0, 0, '--external-downloader', customDownloadingAgent); downloadConfig.splice(0, 0, '--external-downloader', customDownloadingAgent);
} }
@@ -1484,9 +1484,9 @@ async function generateArgs(url, type, options) {
if (useYoutubeDLArchive) { if (useYoutubeDLArchive) {
const archive_folder = options.user ? path.join(fileFolderPath, 'archives') : archivePath; const archive_folder = options.user ? path.join(fileFolderPath, 'archives') : archivePath;
const archive_path = path.join(archive_folder, `archive_${type}.txt`); const archive_path = path.join(archive_folder, `archive_${type}.txt`);
fs.ensureDirSync(archive_folder); fs.ensureDirSync(archive_folder);
// create archive file if it doesn't exist // create archive file if it doesn't exist
if (!fs.existsSync(archive_path)) { if (!fs.existsSync(archive_path)) {
fs.closeSync(fs.openSync(archive_path, 'w')); fs.closeSync(fs.openSync(archive_path, 'w'));
@@ -1671,7 +1671,7 @@ async function autoUpdateYoutubeDL() {
resolve(false); resolve(false);
return false; return false;
} }
const latest_update_version = json[0]['name']; const latest_update_version = json[0]['name'];
if (current_version !== latest_update_version) { if (current_version !== latest_update_version) {
let binary_path = 'node_modules/youtube-dl/bin'; let binary_path = 'node_modules/youtube-dl/bin';
// versions different, download new update // versions different, download new update
@@ -1757,7 +1757,7 @@ app.use(function(req, res, next) {
} }
}); });
app.use(function(req, res, next) { app.use(function(req, res, next) {
if (!req.path.includes('/api/')) { if (!req.path.includes('/api/')) {
next(); next();
} else if (req.query.apiKey === admin_token) { } else if (req.query.apiKey === admin_token) {
@@ -1776,8 +1776,8 @@ app.use(compression());
const optionalJwt = function (req, res, next) { const optionalJwt = function (req, res, next) {
const multiUserMode = config_api.getConfigItem('ytdl_multi_user_mode'); const multiUserMode = config_api.getConfigItem('ytdl_multi_user_mode');
if (multiUserMode && ((req.body && req.body.uuid) || (req.query && req.query.uuid)) && (req.path.includes('/api/getFile') || if (multiUserMode && ((req.body && req.body.uuid) || (req.query && req.query.uuid)) && (req.path.includes('/api/getFile') ||
req.path.includes('/api/audio') || req.path.includes('/api/audio') ||
req.path.includes('/api/video') || req.path.includes('/api/video') ||
req.path.includes('/api/downloadFile'))) { req.path.includes('/api/downloadFile'))) {
// check if shared video // check if shared video
@@ -1823,7 +1823,7 @@ app.post('/api/setConfig', function(req, res) {
logger.error('Tried to save invalid config file!') logger.error('Tried to save invalid config file!')
res.sendStatus(400); res.sendStatus(400);
} }
}); });
app.get('/api/using-encryption', function(req, res) { app.get('/api/using-encryption', function(req, res) {
@@ -1837,7 +1837,7 @@ app.get('/api/logs', function(req, res) {
logs = fs.readFileSync(logs_path, 'utf8'); logs = fs.readFileSync(logs_path, 'utf8');
else else
logger.error(`Failed to find logs file at the expected location: ${logs_path}`) logger.error(`Failed to find logs file at the expected location: ${logs_path}`)
console.log(logs) console.log(logs)
res.send({ res.send({
logs: logs, logs: logs,
@@ -1871,7 +1871,7 @@ app.post('/api/tomp3', optionalJwt, async function(req, res) {
} else { } else {
res.sendStatus(500); res.sendStatus(500);
} }
res.end("yes"); res.end("yes");
}); });
@@ -1887,7 +1887,7 @@ app.post('/api/tomp4', optionalJwt, async function(req, res) {
ui_uid: req.body.ui_uid, ui_uid: req.body.ui_uid,
user: req.isAuthenticated() ? req.user.uid : null user: req.isAuthenticated() ? req.user.uid : null
} }
const safeDownloadOverride = config_api.getConfigItem('ytdl_safe_download_override'); const safeDownloadOverride = config_api.getConfigItem('ytdl_safe_download_override');
const is_playlist = url.includes('playlist'); const is_playlist = url.includes('playlist');
@@ -1901,7 +1901,7 @@ app.post('/api/tomp4', optionalJwt, async function(req, res) {
} else { } else {
res.sendStatus(500); res.sendStatus(500);
} }
res.end("yes"); res.end("yes");
}); });
@@ -2005,10 +2005,10 @@ app.post('/api/getFile', optionalJwt, function (req, res) {
type = 'audio'; type = 'audio';
} }
} }
if (!file && type) file = db.get(`files.${type}`).find({uid: uid}).value(); if (!file && type) file = db.get(`files.${type}`).find({uid: uid}).value();
} }
if (file) { if (file) {
res.send({ res.send({
@@ -2051,12 +2051,12 @@ app.post('/api/enableSharing', optionalJwt, function(req, res) {
.write(); .write();
} else if (type === 'subscription') { } else if (type === 'subscription') {
// TODO: Implement. Main blocker right now is subscription videos are not stored in the DB, they are searched for every // TODO: Implement. Main blocker right now is subscription videos are not stored in the DB, they are searched for every
// time they are requested from the subscription directory. // time they are requested from the subscription directory.
} else { } else {
// error // error
success = false; success = false;
} }
} catch(err) { } catch(err) {
success = false; success = false;
} }
@@ -2094,12 +2094,12 @@ app.post('/api/disableSharing', optionalJwt, function(req, res) {
.write(); .write();
} else if (type === 'subscription') { } else if (type === 'subscription') {
// TODO: Implement. Main blocker right now is subscription videos are not stored in the DB, they are searched for every // TODO: Implement. Main blocker right now is subscription videos are not stored in the DB, they are searched for every
// time they are requested from the subscription directory. // time they are requested from the subscription directory.
} else { } else {
// error // error
success = false; success = false;
} }
} catch(err) { } catch(err) {
success = false; success = false;
} }
@@ -2214,7 +2214,7 @@ app.post('/api/getSubscription', optionalJwt, async (req, res) => {
base_path = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions'); base_path = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions');
else else
base_path = config_api.getConfigItem('ytdl_subscriptions_base_path'); base_path = config_api.getConfigItem('ytdl_subscriptions_base_path');
let appended_base_path = path.join(base_path, (subscription.isPlaylist ? 'playlists' : 'channels'), subscription.name, '/'); let appended_base_path = path.join(base_path, (subscription.isPlaylist ? 'playlists' : 'channels'), subscription.name, '/');
let files; let files;
try { try {
@@ -2229,12 +2229,12 @@ app.post('/api/getSubscription', optionalJwt, async (req, res) => {
let file = files[i]; let file = files[i];
var file_path = file.substring(appended_base_path.length, file.length); var file_path = file.substring(appended_base_path.length, file.length);
var stats = fs.statSync(file); var stats = fs.statSync(file);
var id = file_path.substring(0, file_path.length-4); var id = file_path.substring(0, file_path.length-4);
var jsonobj = utils.getJSONMp4(id, appended_base_path); var jsonobj = utils.getJSONMp4(id, appended_base_path);
if (!jsonobj) continue; if (!jsonobj) continue;
var title = jsonobj.title; var title = jsonobj.title;
var thumbnail = jsonobj.thumbnail; var thumbnail = jsonobj.thumbnail;
var duration = jsonobj.duration; var duration = jsonobj.duration;
var url = jsonobj.webpage_url; var url = jsonobj.webpage_url;
@@ -2242,13 +2242,13 @@ app.post('/api/getSubscription', optionalJwt, async (req, res) => {
var upload_date = jsonobj.upload_date; var upload_date = jsonobj.upload_date;
upload_date = `${upload_date.substring(0, 4)}-${upload_date.substring(4, 6)}-${upload_date.substring(6, 8)}`; upload_date = `${upload_date.substring(0, 4)}-${upload_date.substring(4, 6)}-${upload_date.substring(6, 8)}`;
var size = stats.size; var size = stats.size;
var isaudio = false; var isaudio = false;
var file_obj = new utils.File(id, title, thumbnail, isaudio, duration, url, uploader, size, file, upload_date); var file_obj = new utils.File(id, title, thumbnail, isaudio, duration, url, uploader, size, file, upload_date);
parsed_files.push(file_obj); parsed_files.push(file_obj);
} }
} }
res.send({ res.send({
subscription: subscription, subscription: subscription,
@@ -2315,8 +2315,8 @@ app.post('/api/createPlaylist', optionalJwt, async (req, res) => {
.push(new_playlist) .push(new_playlist)
.write(); .write();
} }
res.send({ res.send({
new_playlist: new_playlist, new_playlist: new_playlist,
success: !!new_playlist // always going to be true success: !!new_playlist // always going to be true
@@ -2343,14 +2343,14 @@ app.post('/api/getPlaylist', optionalJwt, async (req, res) => {
type = 'audio'; type = 'audio';
} }
} }
if (!playlist) playlist = db.get(`playlists.${type}`).find({id: playlistID}).value(); if (!playlist) playlist = db.get(`playlists.${type}`).find({id: playlistID}).value();
} }
res.send({ res.send({
playlist: playlist, playlist: playlist,
type: type, type: type,
success: !!playlist success: !!playlist
}); });
}); });
@@ -2374,7 +2374,7 @@ app.post('/api/updatePlaylist', optionalJwt, async (req, res) => {
} catch(e) { } catch(e) {
logger.error(`Failed to find playlist with ID ${playlistID}`); logger.error(`Failed to find playlist with ID ${playlistID}`);
} }
res.send({ res.send({
success: success success: success
}) })
@@ -2500,7 +2500,7 @@ app.post('/api/downloadFile', optionalJwt, async (req, res) => {
basePath = path.join(usersFileFolder, req.user.uid, 'subscriptions'); basePath = path.join(usersFileFolder, req.user.uid, 'subscriptions');
else else
basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); basePath = config_api.getConfigItem('ytdl_subscriptions_base_path');
file = path.join(__dirname, basePath, (subscriptionPlaylist === 'true' ? 'playlists' : 'channels'), subscriptionName, fileNames + ext); file = path.join(__dirname, basePath, (subscriptionPlaylist === 'true' ? 'playlists' : 'channels'), subscriptionName, fileNames + ext);
} }
} else { } else {
@@ -2514,9 +2514,9 @@ app.post('/api/downloadFile', optionalJwt, async (req, res) => {
logger.error(err); logger.error(err);
} else if (fullPathProvided) { } else if (fullPathProvided) {
try { try {
fs.unlinkSync(file); fs.unlinkSync(file);
} catch(e) { } catch(e) {
logger.error("Failed to remove file", file); logger.error("Failed to remove file", file);
} }
} }
}); });
@@ -2536,7 +2536,7 @@ app.post('/api/deleteFile', async (req, res) => {
app.post('/api/downloadArchive', async (req, res) => { app.post('/api/downloadArchive', async (req, res) => {
let sub = req.body.sub; let sub = req.body.sub;
let archive_dir = sub.archive; let archive_dir = sub.archive;
let full_archive_path = path.join(archive_dir, 'archive.txt'); let full_archive_path = path.join(archive_dir, 'archive.txt');
if (fs.existsSync(full_archive_path)) { if (fs.existsSync(full_archive_path)) {
@@ -2581,7 +2581,7 @@ app.get('/api/updaterStatus', async (req, res) => {
app.post('/api/updateServer', async (req, res) => { app.post('/api/updateServer', async (req, res) => {
let tag = req.body.tag; let tag = req.body.tag;
updateServer(tag); updateServer(tag);
res.send({ res.send({
@@ -2623,7 +2623,7 @@ app.post('/api/checkPin', async (req, res) => {
let stored_pin = db.get('pin_md5').value(); let stored_pin = db.get('pin_md5').value();
let successful = false; let successful = false;
if (input_pin_md5 === stored_pin) { if (input_pin_md5 === stored_pin) {
successful = true; successful = true;
} }
@@ -2660,7 +2660,7 @@ app.get('/api/video/:id', optionalJwt, function(req , res){
let basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); let basePath = config_api.getConfigItem('ytdl_subscriptions_base_path');
const isPlaylist = optionalParams['subPlaylist']; const isPlaylist = optionalParams['subPlaylist'];
basePath += (isPlaylist === 'true' ? 'playlists/' : 'channels/'); basePath += (isPlaylist === 'true' ? 'playlists/' : 'channels/');
file_path = basePath + optionalParams['subName'] + '/' + id + '.mp4'; file_path = basePath + optionalParams['subName'] + '/' + id + '.mp4';
} }
const stat = fs.statSync(file_path) const stat = fs.statSync(file_path)
const fileSize = stat.size const fileSize = stat.size
@@ -2668,7 +2668,7 @@ app.get('/api/video/:id', optionalJwt, function(req , res){
if (range) { if (range) {
const parts = range.replace(/bytes=/, "").split("-") const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10) const start = parseInt(parts[0], 10)
const end = parts[1] const end = parts[1]
? parseInt(parts[1], 10) ? parseInt(parts[1], 10)
: fileSize-1 : fileSize-1
const chunksize = (end-start)+1 const chunksize = (end-start)+1
@@ -2716,7 +2716,7 @@ app.get('/api/audio/:id', optionalJwt, function(req , res){
let basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); let basePath = config_api.getConfigItem('ytdl_subscriptions_base_path');
const isPlaylist = optionalParams['subPlaylist']; const isPlaylist = optionalParams['subPlaylist'];
basePath += (isPlaylist === 'true' ? 'playlists/' : 'channels/'); basePath += (isPlaylist === 'true' ? 'playlists/' : 'channels/');
file_path = basePath + optionalParams['subName'] + '/' + id + '.mp3'; file_path = basePath + optionalParams['subName'] + '/' + id + '.mp3';
} }
file_path = file_path.replace(/\"/g, '\''); file_path = file_path.replace(/\"/g, '\'');
const stat = fs.statSync(file_path) const stat = fs.statSync(file_path)
@@ -2725,7 +2725,7 @@ app.get('/api/audio/:id', optionalJwt, function(req , res){
if (range) { if (range) {
const parts = range.replace(/bytes=/, "").split("-") const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10) const start = parseInt(parts[0], 10)
const end = parts[1] const end = parts[1]
? parseInt(parts[1], 10) ? parseInt(parts[1], 10)
: fileSize-1 : fileSize-1
const chunksize = (end-start)+1 const chunksize = (end-start)+1

View File

@@ -69,7 +69,7 @@ exports.passport = require('passport');
exports.passport.serializeUser(function(user, done) { exports.passport.serializeUser(function(user, done) {
done(null, user); done(null, user);
}); });
exports.passport.deserializeUser(function(user, done) { exports.passport.deserializeUser(function(user, done) {
done(null, user); done(null, user);
}); });
@@ -87,7 +87,7 @@ exports.registerUser = function(req, res) {
logger.error(`Registration failed for user ${userid}. Registration is disabled.`); logger.error(`Registration failed for user ${userid}. Registration is disabled.`);
return; return;
} }
bcrypt.hash(plaintextPassword, saltRounds) bcrypt.hash(plaintextPassword, saltRounds)
.then(function(hash) { .then(function(hash) {
let new_user = { let new_user = {
@@ -127,7 +127,7 @@ exports.registerUser = function(req, res) {
} }
}) })
.then(function(result) { .then(function(result) {
}) })
.catch(function(err) { .catch(function(err) {
logger.error(err); logger.error(err);
@@ -146,7 +146,7 @@ exports.registerUser = function(req, res) {
/************************************************* /*************************************************
* This gets called when passport.authenticate() * This gets called when passport.authenticate()
* gets called. * gets called.
* *
* This checks that the credentials are valid. * This checks that the credentials are valid.
* If so, passes the user info to the next middleware. * If so, passes the user info to the next middleware.
************************************************/ ************************************************/
@@ -181,12 +181,12 @@ exports.passport.use(new LocalStrategy({
* This is a wrapper for auth.passport.authenticate(). * This is a wrapper for auth.passport.authenticate().
* We use this to change WWW-Authenticate header so * We use this to change WWW-Authenticate header so
* the browser doesn't pop-up challenge dialog box by default. * the browser doesn't pop-up challenge dialog box by default.
* Browser's will pop-up up dialog when status is 401 and * Browser's will pop-up up dialog when status is 401 and
* "WWW-Authenticate:Basic..." * "WWW-Authenticate:Basic..."
*************************************************************/ *************************************************************/
/* /*
exports.authenticateViaPassport = function(req, res, next) { exports.authenticateViaPassport = function(req, res, next) {
exports.passport.authenticate('basic',{session:false}, exports.passport.authenticate('basic',{session:false},
function(err, user, info) { function(err, user, info) {
if(!user){ if(!user){
res.set('WWW-Authenticate', 'x'+info); // change to xBasic res.set('WWW-Authenticate', 'x'+info); // change to xBasic
@@ -212,7 +212,7 @@ exports.generateJWT = function(req, res, next) {
, user: req.user.uid , user: req.user.uid
}; };
req.token = jwt.sign(payload, SERVER_SECRET); req.token = jwt.sign(payload, SERVER_SECRET);
next(); next();
} }
exports.returnAuthResponse = function(req, res) { exports.returnAuthResponse = function(req, res) {
@@ -221,11 +221,11 @@ exports.returnAuthResponse = function(req, res) {
token: req.token, token: req.token,
permissions: exports.userPermissions(req.user.uid), permissions: exports.userPermissions(req.user.uid),
available_permissions: consts['AVAILABLE_PERMISSIONS'] available_permissions: consts['AVAILABLE_PERMISSIONS']
}); });
} }
/*************************************** /***************************************
* Authorization: middleware that checks the * Authorization: middleware that checks the
* JWT token for validity before allowing * JWT token for validity before allowing
* the user to access anything. * the user to access anything.
* *
@@ -392,7 +392,7 @@ exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = fals
} catch(e) { } catch(e) {
} }
} }
const full_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext); const full_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext);
users_db.get('users').find({uid: user_uid}).get(`files.${type}`) users_db.get('users').find({uid: user_uid}).get(`files.${type}`)
@@ -454,7 +454,7 @@ exports.changeSharingMode = function(user_uid, file_uid, type, is_playlist, enab
file_db_obj.assign({sharingEnabled: enabled}).write(); file_db_obj.assign({sharingEnabled: enabled}).write();
} }
} }
return success; return success;
} }
@@ -470,7 +470,7 @@ exports.userHasPermission = function(user_uid, permission) {
const user_has_explicit_permission = user_obj['permissions'].includes(permission); const user_has_explicit_permission = user_obj['permissions'].includes(permission);
const permission_in_overrides = user_obj['permission_overrides'].includes(permission); const permission_in_overrides = user_obj['permission_overrides'].includes(permission);
// check if user has a negative/positive override // check if user has a negative/positive override
if (user_has_explicit_permission && permission_in_overrides) { if (user_has_explicit_permission && permission_in_overrides) {
// positive override // positive override
@@ -505,7 +505,7 @@ exports.userPermissions = function(user_uid) {
const user_has_explicit_permission = user_obj['permissions'].includes(permission); const user_has_explicit_permission = user_obj['permissions'].includes(permission);
const permission_in_overrides = user_obj['permission_overrides'].includes(permission); const permission_in_overrides = user_obj['permission_overrides'].includes(permission);
// check if user has a negative/positive override // check if user has a negative/positive override
if (user_has_explicit_permission && permission_in_overrides) { if (user_has_explicit_permission && permission_in_overrides) {
// positive override // positive override
@@ -537,4 +537,4 @@ function getToken(queryParams) {
} else { } else {
return null; return null;
} }
}; };

View File

@@ -119,8 +119,8 @@ function setConfigItem(key, value) {
let parent_parent_single_key = parent_path_arr[parent_path_arr.length-1]; let parent_parent_single_key = parent_path_arr[parent_path_arr.length-1];
parent_parent_object[parent_parent_single_key] = {}; parent_parent_object[parent_parent_single_key] = {};
parent_object = Object.byString(config_json, parent_path); parent_object = Object.byString(config_json, parent_path);
} }
if (value === 'false' || value === 'true') { if (value === 'false' || value === 'true') {
parent_object[element_name] = (value === 'true'); parent_object[element_name] = (value === 'true');
} else { } else {
@@ -146,7 +146,7 @@ function setConfigItems(items) {
let item_path = CONFIG_ITEMS[key]['path']; let item_path = CONFIG_ITEMS[key]['path'];
let item_parent_path = getParentPath(item_path); let item_parent_path = getParentPath(item_path);
let item_element_name = getElementNameInConfig(item_path); let item_element_name = getElementNameInConfig(item_path);
let item_parent_object = Object.byString(config_json, item_parent_path); let item_parent_object = Object.byString(config_json, item_parent_path);
item_parent_object[item_element_name] = value; item_parent_object[item_element_name] = value;
} }

View File

@@ -175,4 +175,4 @@ module.exports = {
CONFIG_ITEMS: CONFIG_ITEMS, CONFIG_ITEMS: CONFIG_ITEMS,
AVAILABLE_PERMISSIONS: AVAILABLE_PERMISSIONS, AVAILABLE_PERMISSIONS: AVAILABLE_PERMISSIONS,
CURRENT_VERSION: 'v4.0' CURRENT_VERSION: 'v4.0'
} }

View File

@@ -7,7 +7,7 @@ const config_api = require('./config');
var logger = null; var logger = null;
var db = null; var db = null;
var users_db = null; var users_db = null;
function setDB(input_db, input_users_db) { db = input_db; users_db = input_users_db } function setDB(input_db, input_users_db) { db = input_db; users_db = input_users_db }
function setLogger(input_logger) { logger = input_logger; } function setLogger(input_logger) { logger = input_logger; }
function initialize(input_db, input_users_db, input_logger) { function initialize(input_db, input_users_db, input_logger) {
@@ -100,4 +100,4 @@ function getAppendedBasePathSub(sub, base_path) {
module.exports = { module.exports = {
initialize: initialize, initialize: initialize,
registerFileDB: registerFileDB registerFileDB: registerFileDB
} }

View File

@@ -15,7 +15,7 @@ var db = null;
var users_db = null; var users_db = null;
var db_api = null; var db_api = null;
function setDB(input_db, input_users_db, input_db_api) { db = input_db; users_db = input_users_db; db_api = input_db_api } function setDB(input_db, input_users_db, input_db_api) { db = input_db; users_db = input_users_db; db_api = input_db_api }
function setLogger(input_logger) { logger = input_logger; } function setLogger(input_logger) { logger = input_logger; }
function initialize(input_db, input_users_db, input_logger, input_db_api) { function initialize(input_db, input_users_db, input_logger, input_db_api) {
@@ -57,7 +57,7 @@ async function subscribe(sub, user_uid = null) {
sub_db = db.get('subscriptions').find({id: sub.id}); sub_db = db.get('subscriptions').find({id: sub.id});
} }
let success = await getSubscriptionInfo(sub, user_uid); let success = await getSubscriptionInfo(sub, user_uid);
if (success) { if (success) {
sub = sub_db.value(); sub = sub_db.value();
getVideosForSub(sub, user_uid); getVideosForSub(sub, user_uid);
@@ -69,7 +69,7 @@ async function subscribe(sub, user_uid = null) {
result_obj.sub = sub; result_obj.sub = sub;
resolve(result_obj); resolve(result_obj);
}); });
} }
async function getSubscriptionInfo(sub, user_uid = null) { async function getSubscriptionInfo(sub, user_uid = null) {
@@ -80,7 +80,7 @@ async function getSubscriptionInfo(sub, user_uid = null) {
basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); basePath = config_api.getConfigItem('ytdl_subscriptions_base_path');
return new Promise(resolve => { return new Promise(resolve => {
// get videos // get videos
let downloadConfig = ['--dump-json', '--playlist-end', '1'] let downloadConfig = ['--dump-json', '--playlist-end', '1']
youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) {
if (debugMode) { if (debugMode) {
@@ -229,7 +229,7 @@ async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null,
// TODO: tell user that the file didn't exist // TODO: tell user that the file didn't exist
resolve(true); resolve(true);
} }
}); });
} }
@@ -275,7 +275,7 @@ async function getVideosForSub(sub, user_uid = null) {
} }
let downloadConfig = ['-o', fullOutput, '-ciw', '--write-info-json', '--print-json']; let downloadConfig = ['-o', fullOutput, '-ciw', '--write-info-json', '--print-json'];
let qualityPath = null; let qualityPath = null;
if (sub.type && sub.type === 'audio') { if (sub.type && sub.type === 'audio') {
qualityPath = ['-f', 'bestaudio'] qualityPath = ['-f', 'bestaudio']
@@ -326,7 +326,7 @@ async function getVideosForSub(sub, user_uid = null) {
} }
} }
// get videos // get videos
logger.verbose('Subscription: getting videos for subscription ' + sub.name); logger.verbose('Subscription: getting videos for subscription ' + sub.name);
youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) {
logger.verbose('Subscription: finished check for ' + sub.name); logger.verbose('Subscription: finished check for ' + sub.name);
@@ -370,7 +370,7 @@ async function getVideosForSub(sub, user_uid = null) {
handleOutputJSON(sub, sub_db, output_json, multiUserMode, reset_videos); handleOutputJSON(sub, sub_db, output_json, multiUserMode, reset_videos);
// TODO: Potentially store downloaded files in db? // TODO: Potentially store downloaded files in db?
} }
resolve(true); resolve(true);
} }
@@ -407,7 +407,7 @@ function getAllSubscriptions(user_uid = null) {
function getSubscription(subID, user_uid = null) { function getSubscription(subID, user_uid = null) {
if (user_uid) if (user_uid)
return users_db.get('users').find({uid: user_uid}).get('subscriptions').find({id: subID}).value(); return users_db.get('users').find({uid: user_uid}).get('subscriptions').find({id: subID}).value();
else else
return db.get('subscriptions').find({id: subID}).value(); return db.get('subscriptions').find({id: subID}).value();
} }
@@ -454,7 +454,7 @@ function removeIDFromArchive(archive_path, id) {
for (let index=0; index<dataArray.length; index++) { for (let index=0; index<dataArray.length; index++) {
if (dataArray[index].includes(searchKeyword)) { // check if a line contains the id keyword if (dataArray[index].includes(searchKeyword)) { // check if a line contains the id keyword
lastIndex = index; // found a line includes a id keyword lastIndex = index; // found a line includes a id keyword
break; break;
} }
} }

View File

@@ -51,7 +51,7 @@ function getJSONMp3(name, customPath, openReadPerms = false) {
} }
else else
obj = 0; obj = 0;
return obj; return obj;
} }
@@ -75,4 +75,4 @@ module.exports = {
getJSONMp4: getJSONMp4, getJSONMp4: getJSONMp4,
getTrueFileName: getTrueFileName, getTrueFileName: getTrueFileName,
File: File File: File
} }