diff --git a/Public API v1.yaml b/Public API v1.yaml index 0ca76da..10c5e3e 100644 --- a/Public API v1.yaml +++ b/Public API v1.yaml @@ -482,6 +482,26 @@ paths: description: OK security: - Auth query parameter: [] + /api/deleteAllFiles: + post: + tags: + - files + summary: Delete all downloaded files + operationId: post-api-deleteAllFiles + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/DeleteMp3Mp4Request' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/DeleteAllFilesResponse' + security: + - Auth query parameter: [] /api/downloadArchive: post: tags: @@ -1765,6 +1785,15 @@ components: type: boolean error: type: string + DeleteAllFilesResponse: + type: object + properties: + file_count: + type: number + description: Number of files found matching search parameters + delete_count: + type: number + description: Number of files removed DeleteSubscriptionFileRequest: required: - file diff --git a/backend/app.js b/backend/app.js index 2882663..88b81bc 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1434,6 +1434,46 @@ app.post('/api/deleteFile', optionalJwt, async (req, res) => { res.send(wasDeleted); }); +app.post('/api/deleteAllFiles', optionalJwt, async (req, res) => { + const blacklistMode = false; + const uuid = req.isAuthenticated() ? req.user.uid : null; + + let files = null; + let text_search = req.body.text_search; + let file_type_filter = req.body.file_type_filter; + + const filter_obj = {user_uid: uuid}; + const regex = true; + if (text_search) { + if (regex) { + filter_obj['title'] = {$regex: `.*${text_search}.*`, $options: 'i'}; + } else { + filter_obj['$text'] = { $search: utils.createEdgeNGrams(text_search) }; + } + } + + if (file_type_filter === 'audio_only') filter_obj['isAudio'] = true; + else if (file_type_filter === 'video_only') filter_obj['isAudio'] = false; + + files = await db_api.getRecords('files', filter_obj); + + let file_count = await db_api.getRecords('files', filter_obj, true); + let delete_count = 0; + + for (let i = 0; i < files.length; i++) { + let wasDeleted = false; + wasDeleted = await db_api.deleteFile(files[i].uid, uuid, blacklistMode); + if (wasDeleted) { + delete_count++; + } + } + + res.send({ + file_count: file_count, + delete_count: delete_count + }); +}); + app.post('/api/downloadFileFromServer', optionalJwt, async (req, res) => { let uid = req.body.uid; let uuid = req.body.uuid;