From 6717a59422b67505419dbeac6befd7a1ea46b521 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Thu, 24 Sep 2020 02:26:58 -0400 Subject: [PATCH] Fixed bug that preventing playlists from being downloaded a zip --- backend/app.js | 16 +++++++++------- backend/authentication/auth.js | 1 + src/app/player/player.component.ts | 3 ++- src/app/posts.services.ts | 5 +++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/backend/app.js b/backend/app.js index 609a1b7..aba9e43 100644 --- a/backend/app.js +++ b/backend/app.js @@ -849,12 +849,13 @@ function getVideoFormatID(name) } } -async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvided = null) { +async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvided = null, user_uid = null) { return new Promise(async resolve => { let zipFolderPath = null; if (!fullPathProvided) { - zipFolderPath = path.join(__dirname, (type === 'audio') ? audioFolderPath : videoFolderPath); + zipFolderPath = path.join(type === 'audio' ? audioFolderPath : videoFolderPath); + if (user_uid) zipFolderPath = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, zipFolderPath); } else { zipFolderPath = path.join(__dirname, config_api.getConfigItem('ytdl_subscriptions_base_path')); } @@ -879,7 +880,7 @@ async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvid for (let i = 0; i < fileNames.length; i++) { let fileName = fileNames[i]; let fileNamePathRemoved = path.parse(fileName).base; - let file_path = !fullPathProvided ? zipFolderPath + fileName + ext : fileName; + let file_path = !fullPathProvided ? path.join(zipFolderPath, fileName + ext) : fileName; archive.file(file_path, {name: fileNamePathRemoved + ext}) } @@ -1793,9 +1794,9 @@ const optionalJwt = function (req, res, next) { const uuid = using_body ? req.body.uuid : req.query.uuid; const uid = using_body ? req.body.uid : req.query.uid; const type = using_body ? req.body.type : req.query.type; - const file = !req.query.id ? auth_api.getUserVideo(uuid, uid, type, true, req.body) : auth_api.getUserPlaylist(uuid, req.query.id, null, true); - const is_shared = file ? file['sharingEnabled'] : false; - if (is_shared) { + const playlist_id = using_body ? req.body.id : req.query.id; + const file = !playlist_id ? auth_api.getUserVideo(uuid, uid, type, true, req.body) : auth_api.getUserPlaylist(uuid, playlist_id, null, false); + if (file) { req.can_watch = true; return next(); } else { @@ -2548,7 +2549,8 @@ app.post('/api/downloadFile', optionalJwt, async (req, res) => { for (let i = 0; i < fileNames.length; i++) { fileNames[i] = decodeURIComponent(fileNames[i]); } - file = await createPlaylistZipFile(fileNames, type, outputName, fullPathProvided); + file = await createPlaylistZipFile(fileNames, type, outputName, fullPathProvided, req.body.uuid); + file = path.join(__dirname, file); } res.sendFile(file, function (err) { if (err) { diff --git a/backend/authentication/auth.js b/backend/authentication/auth.js index a64ca19..bd9da4a 100644 --- a/backend/authentication/auth.js +++ b/backend/authentication/auth.js @@ -283,6 +283,7 @@ exports.getUserVideos = function(user_uid, type) { } exports.getUserVideo = function(user_uid, file_uid, type, requireSharing = false) { + let file = null; if (!type) { file = users_db.get('users').find({uid: user_uid}).get(`files.audio`).find({uid: file_uid}).value(); if (!file) { diff --git a/src/app/player/player.component.ts b/src/app/player/player.component.ts index 6582cd7..af828b2 100644 --- a/src/app/player/player.component.ts +++ b/src/app/player/player.component.ts @@ -317,7 +317,8 @@ export class PlayerComponent implements OnInit, AfterViewInit, OnDestroy { const zipName = fileNames[0].split(' ')[0] + fileNames[1].split(' ')[0]; this.downloading = true; - this.postsService.downloadFileFromServer(fileNames, this.type, zipName).subscribe(res => { + this.postsService.downloadFileFromServer(fileNames, this.type, zipName, null, null, null, null, + !this.uuid ? this.postsService.user.uid : this.uuid, this.id).subscribe(res => { this.downloading = false; const blob: Blob = res; saveAs(blob, zipName + '.zip'); diff --git a/src/app/posts.services.ts b/src/app/posts.services.ts index 67e3bd1..416fd2d 100644 --- a/src/app/posts.services.ts +++ b/src/app/posts.services.ts @@ -223,7 +223,7 @@ export class PostsService implements CanActivate { } downloadFileFromServer(fileName, type, outputName = null, fullPathProvided = null, subscriptionName = null, subPlaylist = null, - uid = null, uuid = null) { + uid = null, uuid = null, id = null) { return this.http.post(this.path + 'downloadFile', {fileNames: fileName, type: type, zip_mode: Array.isArray(fileName), @@ -232,7 +232,8 @@ export class PostsService implements CanActivate { subscriptionName: subscriptionName, subPlaylist: subPlaylist, uuid: uuid, - uid: uid + uid: uid, + id: id }, {responseType: 'blob', params: this.httpOptions.params}); }