mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 15:08:46 +00:00
/api/file/[id]/delete, /api/file/[id]/rename, /api/directory/[id]/delete, /api/directory/[id]/rename Endpoint 구현
This commit is contained in:
@@ -69,6 +69,47 @@ export const getDirectory = async (userId: number, directoryId: number) => {
|
||||
return res[0] ?? null;
|
||||
};
|
||||
|
||||
export const setDirectoryEncName = async (
|
||||
userId: number,
|
||||
directoryId: number,
|
||||
encName: string,
|
||||
encNameIv: string,
|
||||
) => {
|
||||
await db
|
||||
.update(directory)
|
||||
.set({ encName: { ciphertext: encName, iv: encNameIv } })
|
||||
.where(and(eq(directory.userId, userId), eq(directory.id, directoryId)))
|
||||
.execute();
|
||||
};
|
||||
|
||||
export const unregisterDirectory = async (userId: number, directoryId: number) => {
|
||||
return await db.transaction(async (tx) => {
|
||||
const getFilePaths = async (parentId: number) => {
|
||||
const files = await tx
|
||||
.select({ path: file.path })
|
||||
.from(file)
|
||||
.where(and(eq(file.userId, userId), eq(file.parentId, parentId)));
|
||||
return files.map(({ path }) => path);
|
||||
};
|
||||
const unregisterSubDirectoriesRecursively = async (directoryId: number): Promise<string[]> => {
|
||||
const subDirectories = await tx
|
||||
.select({ id: directory.id })
|
||||
.from(directory)
|
||||
.where(and(eq(directory.userId, userId), eq(directory.parentId, directoryId)));
|
||||
const subDirectoryFilePaths = await Promise.all(
|
||||
subDirectories.map(async ({ id }) => await unregisterSubDirectoriesRecursively(id)),
|
||||
);
|
||||
const filePaths = await getFilePaths(directoryId);
|
||||
|
||||
await tx.delete(file).where(eq(file.parentId, directoryId));
|
||||
await tx.delete(directory).where(eq(directory.id, directoryId));
|
||||
|
||||
return filePaths.concat(...subDirectoryFilePaths);
|
||||
};
|
||||
return await unregisterSubDirectoriesRecursively(directoryId);
|
||||
});
|
||||
};
|
||||
|
||||
export const registerNewFile = async (params: NewFileParams) => {
|
||||
await db.transaction(async (tx) => {
|
||||
const meks = await tx
|
||||
@@ -115,3 +156,25 @@ export const getFile = async (userId: number, fileId: number) => {
|
||||
.execute();
|
||||
return res[0] ?? null;
|
||||
};
|
||||
|
||||
export const setFileEncName = async (
|
||||
userId: number,
|
||||
fileId: number,
|
||||
encName: string,
|
||||
encNameIv: string,
|
||||
) => {
|
||||
await db
|
||||
.update(file)
|
||||
.set({ encName: { ciphertext: encName, iv: encNameIv } })
|
||||
.where(and(eq(file.userId, userId), eq(file.id, fileId)))
|
||||
.execute();
|
||||
};
|
||||
|
||||
export const unregisterFile = async (userId: number, fileId: number) => {
|
||||
const res = await db
|
||||
.delete(file)
|
||||
.where(and(eq(file.userId, userId), eq(file.id, fileId)))
|
||||
.returning({ path: file.path })
|
||||
.execute();
|
||||
return res[0]?.path ?? null;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user