From 0189d292a8e7516ab3325f58604e2a0042550ee3 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Sun, 18 Oct 2020 02:20:06 -0400 Subject: [PATCH] Fixed bug that prevented categorized files from being deletes and simplified the two delete file API calls into one --- backend/app.js | 58 +++---------------- .../recent-videos/recent-videos.component.ts | 4 +- src/app/file-card/file-card.component.ts | 2 +- src/app/main/main.component.ts | 4 +- src/app/posts.services.ts | 8 +-- 5 files changed, 15 insertions(+), 61 deletions(-) diff --git a/backend/app.js b/backend/app.js index acbb45e..50af52e 100644 --- a/backend/app.js +++ b/backend/app.js @@ -2523,56 +2523,25 @@ app.post('/api/deletePlaylist', optionalJwt, async (req, res) => { }) }); -// deletes mp3 file -app.post('/api/deleteMp3', optionalJwt, async (req, res) => { - // var name = req.body.name; - var uid = req.body.uid; - var blacklistMode = req.body.blacklistMode; - - if (req.isAuthenticated()) { - let success = auth_api.deleteUserFile(req.user.uid, uid, 'audio', blacklistMode); - res.send(success); - return; - } - - var audio_obj = db.get('files.audio').find({uid: uid}).value(); - var name = audio_obj.id; - var fullpath = audioFolderPath + name + ".mp3"; - var wasDeleted = false; - if (fs.existsSync(fullpath)) - { - deleteAudioFile(name, null, blacklistMode); - db.get('files.audio').remove({uid: uid}).write(); - wasDeleted = true; - res.send(wasDeleted); - } else if (audio_obj) { - db.get('files.audio').remove({uid: uid}).write(); - wasDeleted = true; - res.send(wasDeleted); - } else { - wasDeleted = false; - res.send(wasDeleted); - } -}); - -// deletes mp4 file -app.post('/api/deleteMp4', optionalJwt, async (req, res) => { +// deletes non-subscription files +app.post('/api/deleteFile', optionalJwt, async (req, res) => { var uid = req.body.uid; + var type = req.body.type; var blacklistMode = req.body.blacklistMode; if (req.isAuthenticated()) { - let success = auth_api.deleteUserFile(req.user.uid, uid, 'video', blacklistMode); + let success = auth_api.deleteUserFile(req.user.uid, uid, type, blacklistMode); res.send(success); return; } - var video_obj = db.get('files.video').find({uid: uid}).value(); - var name = video_obj.id; - var fullpath = videoFolderPath + name + ".mp4"; + var file_obj = db.get(`files.${type}`).find({uid: uid}).value(); + var name = file_obj.id; + var fullpath = file_obj ? file_obj.path : null; var wasDeleted = false; if (fs.existsSync(fullpath)) { - wasDeleted = await deleteVideoFile(name, null, blacklistMode); + wasDeleted = type === 'audio' ? await deleteAudioFile(name, path.basename(fullpath), blacklistMode) : await deleteVideoFile(name, path.basename(fullpath), blacklistMode); db.get('files.video').remove({uid: uid}).write(); // wasDeleted = true; res.send(wasDeleted); @@ -2638,17 +2607,6 @@ app.post('/api/downloadFile', optionalJwt, async (req, res) => { }); }); -app.post('/api/deleteFile', async (req, res) => { - let fileName = req.body.fileName; - let type = req.body.type; - if (type === 'audio') { - deleteAudioFile(fileName); - } else if (type === 'video') { - deleteVideoFile(fileName); - } - res.send({}); -}); - app.post('/api/downloadArchive', async (req, res) => { let sub = req.body.sub; let archive_dir = sub.archive; diff --git a/src/app/components/recent-videos/recent-videos.component.ts b/src/app/components/recent-videos/recent-videos.component.ts index 8333f79..2eb7f36 100644 --- a/src/app/components/recent-videos/recent-videos.component.ts +++ b/src/app/components/recent-videos/recent-videos.component.ts @@ -210,7 +210,7 @@ export class RecentVideosComponent implements OnInit { if (!this.postsService.config.Extra.file_manager_enabled) { // tell server to delete the file once downloaded - this.postsService.deleteFile(name, false).subscribe(delRes => { + this.postsService.deleteFile(name, type).subscribe(delRes => { // reload mp4s this.getAllFiles(); }); @@ -233,7 +233,7 @@ export class RecentVideosComponent implements OnInit { } deleteNormalFile(file, index, blacklistMode = false) { - this.postsService.deleteFile(file.uid, file.isAudio, blacklistMode).subscribe(result => { + this.postsService.deleteFile(file.uid, file.isAudio ? 'audio' : 'video', blacklistMode).subscribe(result => { if (result) { this.postsService.openSnackBar('Delete success!', 'OK.'); this.files.splice(index, 1); diff --git a/src/app/file-card/file-card.component.ts b/src/app/file-card/file-card.component.ts index 280d131..68a8453 100644 --- a/src/app/file-card/file-card.component.ts +++ b/src/app/file-card/file-card.component.ts @@ -56,7 +56,7 @@ export class FileCardComponent implements OnInit { deleteFile(blacklistMode = false) { if (!this.playlist) { - this.postsService.deleteFile(this.uid, this.isAudio, blacklistMode).subscribe(result => { + this.postsService.deleteFile(this.uid, this.isAudio ? 'audio' : 'video', blacklistMode).subscribe(result => { if (result) { this.openSnackBar('Delete success!', 'OK.'); this.removeFile.emit(this.name); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index ef4f929..b50337a 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -740,7 +740,7 @@ export class MainComponent implements OnInit { if (!this.fileManagerEnabled) { // tell server to delete the file once downloaded - this.postsService.deleteFile(name, true).subscribe(delRes => { + this.postsService.deleteFile(name, 'video').subscribe(delRes => { // reload mp3s this.getMp3s(); }); @@ -757,7 +757,7 @@ export class MainComponent implements OnInit { if (!this.fileManagerEnabled) { // tell server to delete the file once downloaded - this.postsService.deleteFile(name, false).subscribe(delRes => { + this.postsService.deleteFile(name, 'audio').subscribe(delRes => { // reload mp4s this.getMp4s(); }); diff --git a/src/app/posts.services.ts b/src/app/posts.services.ts index f65af2e..8ee9117 100644 --- a/src/app/posts.services.ts +++ b/src/app/posts.services.ts @@ -199,12 +199,8 @@ export class PostsService implements CanActivate { return this.http.post(this.path + 'setConfig', {new_config_file: config}, this.httpOptions); } - deleteFile(uid: string, isAudio: boolean, blacklistMode = false) { - if (isAudio) { - return this.http.post(this.path + 'deleteMp3', {uid: uid, blacklistMode: blacklistMode}, this.httpOptions); - } else { - return this.http.post(this.path + 'deleteMp4', {uid: uid, blacklistMode: blacklistMode}, this.httpOptions); - } + deleteFile(uid: string, type: string, blacklistMode = false) { + return this.http.post(this.path + 'deleteFile', {uid: uid, type: type, blacklistMode: blacklistMode}, this.httpOptions); } getMp3s() {