파일/폴더 삭제 및 이름 변경 구현 완료

This commit is contained in:
static
2025-01-06 14:30:00 +09:00
parent bd0dd3343a
commit 71f12c942b
6 changed files with 75 additions and 27 deletions

View File

@@ -9,20 +9,30 @@ import {
decryptString,
} from "$lib/modules/crypto";
import type {
DirectoryRenameRequest,
DirectoryInfoResponse,
DirectoryCreateRequest,
FileRenameRequest,
FileUploadRequest,
} from "$lib/server/schemas";
import type { MasterKey } from "$lib/stores";
export { decryptFileMetadata } from "$lib/services/file";
export interface SelectedDirectoryEntry {
type: "directory" | "file";
id: number;
dataKey: CryptoKey;
name: string;
}
export const decryptDirectoryMetadata = async (
metadata: NonNullable<DirectoryInfoResponse["metadata"]>,
masterKey: CryptoKey,
) => {
const { dataKey } = await unwrapDataKey(metadata.dek, masterKey);
return {
dataKey,
name: await decryptString(metadata.name, metadata.nameIv, dataKey),
};
};
@@ -71,3 +81,26 @@ export const requestFileUpload = async (
xhr.open("POST", "/api/file/upload");
xhr.send(form);
};
export const requestDirectoryEntryRename = async (
entry: SelectedDirectoryEntry,
newName: string,
) => {
const newNameEncrypted = await encryptString(newName, entry.dataKey);
if (entry.type === "directory") {
await callPostApi<DirectoryRenameRequest>(`/api/directory/${entry.id}/rename`, {
name: newNameEncrypted.ciphertext,
nameIv: newNameEncrypted.iv,
});
} else {
await callPostApi<FileRenameRequest>(`/api/file/${entry.id}/rename`, {
name: newNameEncrypted.ciphertext,
nameIv: newNameEncrypted.iv,
});
}
};
export const requestDirectoryEntryDeletion = async (entry: SelectedDirectoryEntry) => {
await callPostApi(`/api/${entry.type}/${entry.id}/delete`);
};