Skip to content

Commit

Permalink
feat(folder): check existence EP
Browse files Browse the repository at this point in the history
  • Loading branch information
sg-gs committed Sep 26, 2023
1 parent a9bb4b8 commit 66c579e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app/routes/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ export class StorageController {
});
}

public async checkFolderExistence(req: Request, res: Response): Promise<void> {
const { name, parentId } = req.body;
const { behalfUser: user } = req as any;

if (Validator.isInvalidString(name)) {
throw createHttpError(400, 'Folder name must be a valid string');
}

if (!parentId || parentId <= 0) {
throw createHttpError(400, 'Invalid parent folder id');
}

return this.services.Folder.CheckFolderExistence(user, name, parentId)
.then((result: { folder: FolderAttributes, exists: boolean }) => {
if (result.exists) {
res.status(200).json(result);
} else {
res.status(404).send();
}
})
.catch((err: Error) => {
this.logger.error(`Error checking folder existence for user ${user.id}: ${err}`);
res.status(500).send();
});
}

public async getTree(req: Request, res: Response): Promise<void> {
const { user } = req as PassportRequest;
const deleted = req.query?.trash === 'true';
Expand Down Expand Up @@ -794,6 +820,7 @@ export default (router: Router, service: any) => {
controller.createThumbnail.bind(controller)
);
router.post('/storage/folder', passportAuth, sharedAdapter, controller.createFolder.bind(controller));
router.post('/storage/folder/exists', passportAuth, sharedAdapter, controller.checkFolderExistence.bind(controller));
router.get('/storage/tree', passportAuth, controller.getTree.bind(controller));
router.get('/storage/tree/:folderId', passportAuth, controller.getTreeSpecific.bind(controller));
router.delete('/storage/folder/:id', passportAuth, sharedAdapter, controller.deleteFolder.bind(controller));
Expand Down
46 changes: 46 additions & 0 deletions src/app/services/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,51 @@ module.exports = (Model, App) => {
return folder;
};

const CheckFolderExistence = async (user, folderName, parentFolderId) => {
if (parentFolderId >= 2147483648) {
throw Error('Invalid parent folder');
}
const isGuest = user.email !== user.bridgeUser;

if (isGuest) {
const { bridgeUser } = user;

user = await Model.users.findOne({
where: { username: bridgeUser },
});
}

const parentFolder = await Model.folder.findOne({
id: { [Op.eq]: parentFolderId },
user_id: { [Op.eq]: user.id },
});

if (!parentFolder) {
throw Error('Parent folder is not yours');
}

if (folderName === '' || invalidName.test(folderName)) {
throw Error('Invalid folder name');
}

// Encrypt folder name, TODO: use versioning for encryption
const cryptoFolderName = App.services.Crypt.encryptName(folderName, parentFolderId);

const maybeExistentFolder = await Model.folder.findOne({
where: {
parentId: { [Op.eq]: parentFolderId },
name: { [Op.eq]: cryptoFolderName },
deleted: { [Op.eq]: false },
},
});

if (maybeExistentFolder) {
return { exists: true, folder: maybeExistentFolder };
}

return { exists: false, folder: null };
};

// Requires stored procedure
const DeleteOrphanFolders = async (userId) => {
const clear = await App.database.query('CALL clear_orphan_folders_by_user (:userId, :output)', {
Expand Down Expand Up @@ -772,6 +817,7 @@ module.exports = (Model, App) => {
Name: 'Folder',
getById,
Create,
CheckFolderExistence,
Delete,
GetChildren,
GetTree,
Expand Down

0 comments on commit 66c579e

Please sign in to comment.