Added finishing touches to mover

This commit is contained in:
subnub
2024-08-07 02:56:35 -04:00
parent 34323fe4fb
commit 2ec6d39eff
4 changed files with 56 additions and 107 deletions
+28 -1
View File
@@ -198,7 +198,7 @@ class DbUtil {
return file;
};
getFileListByParent = async (
getFileListByIncludedParent = async (
userID: string | mongoose.Types.ObjectId,
parentListString: string
) => {
@@ -215,6 +215,33 @@ class DbUtil {
return fileList;
};
getFileListByParent = async (userID: string, parent: string) => {
const fileList = await File.find({
owner: userID,
"metadata.parent": parent,
});
return fileList;
};
moveMultipleFiles = async (
userID: string | mongoose.Types.ObjectId,
currentParent: string,
newParent: string,
newParentList: string
) => {
await File.updateMany(
{ "metadata.owner": userID, "metadata.parent": currentParent },
{
$set: {
"metadata.parent": newParent,
"metadata.parentList": newParentList,
},
},
{ new: true }
);
};
}
export default DbUtil;
+5 -6
View File
@@ -196,9 +196,9 @@ class DbUtil {
const idQuery = [];
if (currentParent && currentParent !== "/") {
idQuery.push(currentParent);
}
// if (currentParent && currentParent !== "/") {
// idQuery.push(currentParent);
// }
if (folderID) {
query.parentList = { $ne: folderID };
@@ -215,9 +215,7 @@ class DbUtil {
query.name = new RegExp(search, "i");
}
console.log("query", query);
const result = await Folder.find(query);
const result = await Folder.find(query).sort({ createdAt: -1 });
return result;
};
@@ -228,6 +226,7 @@ class DbUtil {
parentList: {
$in: parent,
},
trashed: null,
});
return folderList;
+1 -1
View File
@@ -373,7 +373,7 @@ class StorageService {
});
await Folder.deleteMany({ owner: userID, _id: folderID });
const fileList = await dbUtilsFile.getFileListByParent(
const fileList = await dbUtilsFile.getFileListByIncludedParent(
userID,
parentList.toString()
);
+22 -99
View File
@@ -248,123 +248,46 @@ class FolderService {
};
moveFolder = async (userID: string, folderID: string, parentID: string) => {
console.log("parentID", parentID);
const foldersByIncludedParent =
await utilsFolder.getFolderListByIncludedParent(userID, folderID);
//const newParentList = [];
const startParentList = [];
if (parentID !== "/") {
const folderToMoveTo = await utilsFolder.getFolderInfo(parentID, userID);
if (!folderToMoveTo) {
throw new NotFoundError("Move Folder Not Found Error");
}
for (let i = 0; i < foldersByIncludedParent.length; i++) {
const currentFolder = foldersByIncludedParent[i];
const currentParentIndex = currentFolder.parentList.indexOf(folderID);
const newParentList = [
...folderToMoveTo.parentList,
folderToMoveTo._id.toString(),
...currentFolder.parentList.slice(currentParentIndex + 1),
];
console.log("new parent list", newParentList);
await utilsFolder.moveFolder(
folderID,
userID,
folderToMoveTo._id.toString(),
newParentList
);
}
startParentList.push(
...folderToMoveTo.parentList,
folderToMoveTo._id.toString()
);
} else {
// newParentList.push("/");
startParentList.push("/");
}
console.log("foldersByIncludedParent", foldersByIncludedParent);
for (let i = 0; i < foldersByIncludedParent.length; i++) {
const currentFolder = foldersByIncludedParent[i];
return;
let parentList = ["/"];
const currentParentIndex = currentFolder.parentList.indexOf(folderID);
// if (parentID.length !== 1) {
// const parentFile = await utilsFolder.getFolderInfo(parentID, userID);
const newParentList = [];
// if (!parentFile) throw new NotFoundError("Parent Folder Info Not Found");
// parentList = parentFile.parentList;
// parentList.push(parentID);
// }
const folder = await utilsFolder.moveFolder(
folderID,
userID,
parentID,
parentList
);
if (!folder) throw new NotFoundError("Move Folder Not Found");
const folderChilden = await utilsFolder.findAllFoldersByParent(
folderID.toString(),
userID
);
folderChilden.map(async (currentFolderChild) => {
let currentFolderChildParentList = currentFolderChild.parentList;
const indexOfFolderID = currentFolderChildParentList.indexOf(
folderID.toString()
newParentList.push(
...startParentList,
...currentFolder.parentList.slice(currentParentIndex + 1)
);
currentFolderChildParentList =
currentFolderChildParentList.splice(indexOfFolderID);
currentFolderChildParentList = [
...parentList,
...currentFolderChildParentList,
];
currentFolderChild.parentList = currentFolderChildParentList;
await currentFolderChild.save();
});
const fileChildren = await utilsFile.getFileListByParent(
userID,
folderID.toString()
);
fileChildren.map(async (currentFileChild) => {
let currentFileChildParentList: string | string[] =
currentFileChild.metadata.parentList;
currentFileChildParentList = currentFileChildParentList.split(",");
const indexOfFolderID = currentFileChildParentList.indexOf(
folderID.toString()
);
currentFileChildParentList =
currentFileChildParentList.splice(indexOfFolderID);
currentFileChildParentList = [
...parentList,
...currentFileChildParentList,
];
if (currentFileChild._id) {
await utilsFile.moveFile(
currentFileChild._id,
await Promise.all([
utilsFolder.moveFolder(folderID, userID, parentID, newParentList),
utilsFile.moveMultipleFiles(
userID,
currentFileChild.metadata.parent,
currentFileChildParentList.toString()
);
}
});
currentFolder._id.toString(),
parentID,
newParentList.toString()
),
]);
}
};
}