Skip to content

Commit

Permalink
refactor(folder): improve search speed to prevent duplicates when cre…
Browse files Browse the repository at this point in the history
…ating a folder
  • Loading branch information
sg-gs committed Jan 17, 2024
1 parent ea612f1 commit 7a5701e
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/app/services/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ module.exports = (Model, App) => {
throw Error('Invalid folder name');
}

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

const exists = await Model.folder.findOne({
where: {
parentId: { [Op.eq]: parentFolderId },
name: { [Op.eq]: cryptoFolderName },
plain_name: { [Op.eq]: folderName },
deleted: { [Op.eq]: false },
},
});
Expand All @@ -112,8 +109,7 @@ module.exports = (Model, App) => {
throw new FolderAlreadyExistsError('Folder with the same name already exists');
}

// Since we upload everything in the same bucket, this line is no longer needed
// const bucket = await App.services.Inxt.CreateBucket(user.email, user.userId, user.mnemonic, cryptoFolderName)
const cryptoFolderName = App.services.Crypt.encryptName(folderName, parentFolderId);
const folder = await user.createFolder({
name: cryptoFolderName,
plain_name: folderName,
Expand Down Expand Up @@ -142,8 +138,10 @@ module.exports = (Model, App) => {
}

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

if (!parentFolder) {
Expand All @@ -154,22 +152,16 @@ module.exports = (Model, App) => {
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({
const folder = await Model.folder.findOne({
where: {
parentId: { [Op.eq]: parentFolderId },
name: { [Op.eq]: cryptoFolderName },
plain_name: { [Op.eq]: folderName },
deleted: { [Op.eq]: false },
},
});
const folderExists = !!folder;

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

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

// Requires stored procedure
Expand Down

0 comments on commit 7a5701e

Please sign in to comment.