diff --git a/src/app/routes/storage.ts b/src/app/routes/storage.ts index 58061197..9542e468 100644 --- a/src/app/routes/storage.ts +++ b/src/app/routes/storage.ts @@ -18,6 +18,7 @@ import { } from '../services/errors/FileWithNameAlreadyExistsError'; import { FolderAlreadyExistsError, FolderWithNameAlreadyExistsError } from '../services/errors/FolderWithNameAlreadyExistsError'; import * as resourceSharingMiddlewareBuilder from '../middleware/resource-sharing.middleware'; +import {validate } from 'uuid'; type AuthorizedRequest = Request & { user: UserAttributes }; interface Services { @@ -136,7 +137,7 @@ export class StorageController { } public async createFolder(req: Request, res: Response): Promise { - const { folderName, parentFolderId } = req.body; + const { folderName, parentFolderId, uuid: clientCreatedUuuid } = req.body; const { behalfUser: user } = req as any; if (Validator.isInvalidString(folderName)) { @@ -147,6 +148,10 @@ export class StorageController { throw createHttpError(400, 'Invalid parent folder id'); } + if (clientCreatedUuuid && !validate(clientCreatedUuuid)) { + throw createHttpError(400, 'Invalid uuid'); + } + const clientId = String(req.headers['internxt-client-id']); const parentFolder = await this.services.Folder.getById(parentFolderId); @@ -158,8 +163,9 @@ export class StorageController { if (parentFolder.userId !== user.id) { throw createHttpError(403, 'Parent folder does not belong to user'); } + - return this.services.Folder.Create(user, folderName, parentFolderId) + return this.services.Folder.Create(user, folderName, parentFolderId, null, clientCreatedUuuid) .then(async (result: FolderAttributes) => { res.status(201).json(result); const workspaceMembers = await this.services.User.findWorkspaceMembers(user.bridgeUser); diff --git a/src/app/services/folder.js b/src/app/services/folder.js index 2f712670..cf80eda6 100644 --- a/src/app/services/folder.js +++ b/src/app/services/folder.js @@ -56,7 +56,7 @@ module.exports = (Model, App) => { }; // Create folder entry, for desktop - const Create = async (user, folderName, parentFolderId, teamId = null) => { + const Create = async (user, folderName, parentFolderId, teamId = null, uuid = null) => { if (parentFolderId >= 2147483648) { throw Error('Invalid parent folder'); } @@ -117,7 +117,7 @@ module.exports = (Model, App) => { const folder = await user.createFolder({ name: cryptoFolderName, plain_name: folderName, - uuid: v4(), + uuid: uuid || v4(), bucket: null, parentId: parentFolderId || null, parentUuid: parentFolder.uuid,