From ee10d308c07f4164a24efb1d55ede62dcb04e7ae Mon Sep 17 00:00:00 2001 From: subnub Date: Thu, 10 Oct 2024 14:02:04 -0400 Subject: [PATCH] fixed trashing and restoring files/folders that were inside parent folders --- backend/db/mongoDB/fileDB.ts | 9 +++- backend/db/mongoDB/folderDB.ts | 15 ++++++ backend/express-routers/file-router.ts | 48 ++++++++++++++----- backend/middleware/files/files-middleware.ts | 39 ++++++++++++--- backend/services/file-service/file-service.ts | 19 +++++++- .../services/folder-service/folder-service.ts | 12 +++-- backend/utils/createQuery.ts | 2 +- 7 files changed, 118 insertions(+), 26 deletions(-) diff --git a/backend/db/mongoDB/fileDB.ts b/backend/db/mongoDB/fileDB.ts index 039c9c6..8c88f66 100644 --- a/backend/db/mongoDB/fileDB.ts +++ b/backend/db/mongoDB/fileDB.ts @@ -63,7 +63,12 @@ class DbUtil { return file; }; - trashFile = async (fileID: string, userID: string) => { + trashFile = async ( + fileID: string, + parent: string, + parentList: string, + userID: string + ) => { const result = await File.findByIdAndUpdate( { _id: new ObjectId(fileID), @@ -72,6 +77,8 @@ class DbUtil { { $set: { "metadata.trashed": true, + "metadata.parent": parent, + "metadata.parentList": parentList, }, } ); diff --git a/backend/db/mongoDB/folderDB.ts b/backend/db/mongoDB/folderDB.ts index ecf979f..98501bc 100644 --- a/backend/db/mongoDB/folderDB.ts +++ b/backend/db/mongoDB/folderDB.ts @@ -183,6 +183,21 @@ class DbUtil { return result; }; + trashFolder = async (folderID: string, userID: string) => { + const result = await Folder.findByIdAndUpdate( + { + _id: new ObjectId(folderID), + owner: userID, + }, + { + $set: { + trashed: true, + }, + } + ); + return result; + }; + getMoveFolderList = async ( userID: string, parent = "/", diff --git a/backend/express-routers/file-router.ts b/backend/express-routers/file-router.ts index 9deffa8..bdab520 100644 --- a/backend/express-routers/file-router.ts +++ b/backend/express-routers/file-router.ts @@ -10,6 +10,11 @@ import { getQuickListValidationRules, getSuggestedListValidationRules, getThumbnailValidationRules, + moveFileValidationRules, + renameFileValidationRules, + restoreFileValidationRules, + trashFileValidationRules, + trashMultiValidationRules, } from "../middleware/files/files-middleware"; const fileController = new FileController(); @@ -108,15 +113,40 @@ router.patch( fileController.makeOneTimePublic ); -router.patch("/file-service/rename", auth, fileController.renameFile); +router.patch( + "/file-service/rename", + auth, + renameFileValidationRules, + fileController.renameFile +); -router.patch("/file-service/move", auth, fileController.moveFile); +router.patch( + "/file-service/move", + auth, + moveFileValidationRules, + fileController.moveFile +); -router.patch("/file-service/trash", auth, fileController.trashFile); +router.patch( + "/file-service/trash", + auth, + trashFileValidationRules, + fileController.trashFile +); -router.patch("/file-service/trash-multi", auth, fileController.trashMulti); +router.patch( + "/file-service/trash-multi", + auth, + trashMultiValidationRules, + fileController.trashMulti +); -router.patch("/file-service/restore", auth, fileController.restoreFile); +router.patch( + "/file-service/restore", + auth, + restoreFileValidationRules, + fileController.restoreFile +); router.patch("/file-service/restore-multi", auth, fileController.restoreMulti); @@ -139,11 +169,3 @@ router.post( ); export default router; - -// NO longer needed left for reference - -//router.delete("/file-service/remove/token-video/:tempToken/:uuid", auth, fileController.removeTempToken); -//router.get("/file-service/stream-video/:id/:tempToken/:uuid", auth, fileController.streamVideo); -//router.get("/file-service/stream-video/:id", auth, fileController.streamVideo); -//router.get("/file-service/download/get-token", authFullUser, fileController.getDownloadToken); -//router.get("/file-service/download/get-token-video", auth, fileController.getDownloadTokenVideo); diff --git a/backend/middleware/files/files-middleware.ts b/backend/middleware/files/files-middleware.ts index 7c7733f..8335604 100644 --- a/backend/middleware/files/files-middleware.ts +++ b/backend/middleware/files/files-middleware.ts @@ -1,7 +1,9 @@ -import { param, query, validationResult } from "express-validator"; +import { body, param, query, validationResult } from "express-validator"; import { Request, Response, NextFunction } from "express"; import { middlewareValidationFunction } from "../utils/middleware-utils"; +// GET + export const getThumbnailValidationRules = [ param("id").isString().withMessage("ID must be a string"), middlewareValidationFunction, @@ -46,12 +48,6 @@ export const getListValidationRules = [ middlewareValidationFunction, ]; -export const moveFileValidationRules = [ - query("fileID").isString().withMessage("Parent must be a string"), - query("parentID").isString().withMessage("Parent must be a string"), - middlewareValidationFunction, -]; - export const getSuggestedListValidationRules = [ query("search") .optional() @@ -68,3 +64,32 @@ export const getSuggestedListValidationRules = [ .withMessage("Media Mode must be a boolean"), middlewareValidationFunction, ]; + +// PATCH + +export const renameFileValidationRules = [ + body("id").isString().withMessage("ID must be a string"), + body("title").isString().withMessage("Title must be a string"), + middlewareValidationFunction, +]; + +export const moveFileValidationRules = [ + body("id").isString().withMessage("FileID must be a string"), + body("parentID").isString().optional().withMessage("Parent must be a string"), + middlewareValidationFunction, +]; + +export const trashFileValidationRules = [ + body("id").isString().withMessage("ID must be a string"), + middlewareValidationFunction, +]; + +export const trashMultiValidationRules = [ + body("items").isArray().withMessage("Items must be an array"), + middlewareValidationFunction, +]; + +export const restoreFileValidationRules = [ + body("id").isString().withMessage("ID must be a string"), + middlewareValidationFunction, +]; diff --git a/backend/services/file-service/file-service.ts b/backend/services/file-service/file-service.ts index 053a313..d73e97a 100644 --- a/backend/services/file-service/file-service.ts +++ b/backend/services/file-service/file-service.ts @@ -210,7 +210,24 @@ class MongoFileService { }; trashFile = async (userID: string, fileID: string) => { - const trashedFile = await fileDB.trashFile(fileID, userID); + const file = await fileDB.getFileInfo(fileID, userID); + + if (!file) throw new NotFoundError("Trash File Not Found Error"); + + let parent = file.metadata.parent; + let parentList = file.metadata.parentList; + + if (file.metadata.parent !== "/") { + parent = "/"; + parentList = ["/"].toString(); + } + + const trashedFile = await fileDB.trashFile( + fileID, + parent, + parentList, + userID + ); if (!trashedFile) throw new NotFoundError("Trash File Not Found Error"); return trashedFile; }; diff --git a/backend/services/folder-service/folder-service.ts b/backend/services/folder-service/folder-service.ts index 7493e06..9198835 100644 --- a/backend/services/folder-service/folder-service.ts +++ b/backend/services/folder-service/folder-service.ts @@ -194,10 +194,16 @@ class FolderService { if (!folder) throw new NotFoundError("Trash Folder Not Found Error"); - folder.trashed = true; - await folder.save(); + const parentList = []; - const parentList = [...folder.parentList, folder._id!.toString()]; + if (folder.parent !== "/") { + await folderDB.moveFolder(folderID, userID, "/", ["/"]); + parentList.push("/", folder._id!.toString()); + } else { + parentList.push(...folder.parentList, folder._id!.toString()); + } + + await folderDB.trashFolder(folderID, userID); await folderDB.trashFoldersByParent(parentList, userID); diff --git a/backend/utils/createQuery.ts b/backend/utils/createQuery.ts index 37f29ac..4e40b3a 100644 --- a/backend/utils/createQuery.ts +++ b/backend/utils/createQuery.ts @@ -49,7 +49,7 @@ const createQuery = ({ query = { ...query, filename: { $gt: startAtName } }; } - if (trashMode) { + if (trashMode && parent === "/") { query = { ...query, "metadata.trashed": true }; } else { query = { ...query, "metadata.trashed": null };