mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 16:16:55 +00:00
/api/category, /api/directory, /api/file 아래의 대부분의 Endpoint들을 tRPC로 마이그레이션
This commit is contained in:
@@ -1,31 +1,40 @@
|
||||
import { callPostApi } from "$lib/hooks";
|
||||
import { generateDataKey, wrapDataKey, encryptString } from "$lib/modules/crypto";
|
||||
import type { CategoryCreateRequest, CategoryFileRemoveRequest } from "$lib/server/schemas";
|
||||
import type { MasterKey } from "$lib/stores";
|
||||
import { useTRPC } from "$trpc/client";
|
||||
|
||||
export const requestCategoryCreation = async (
|
||||
name: string,
|
||||
parentId: "root" | number,
|
||||
masterKey: MasterKey,
|
||||
) => {
|
||||
const trpc = useTRPC();
|
||||
const { dataKey, dataKeyVersion } = await generateDataKey();
|
||||
const nameEncrypted = await encryptString(name, dataKey);
|
||||
|
||||
const res = await callPostApi<CategoryCreateRequest>("/api/category/create", {
|
||||
parent: parentId,
|
||||
mekVersion: masterKey.version,
|
||||
dek: await wrapDataKey(dataKey, masterKey.key),
|
||||
dekVersion: dataKeyVersion.toISOString(),
|
||||
name: nameEncrypted.ciphertext,
|
||||
nameIv: nameEncrypted.iv,
|
||||
});
|
||||
return res.ok;
|
||||
try {
|
||||
await trpc.category.create.mutate({
|
||||
parent: parentId,
|
||||
mekVersion: masterKey.version,
|
||||
dek: await wrapDataKey(dataKey, masterKey.key),
|
||||
dekVersion: dataKeyVersion,
|
||||
name: nameEncrypted.ciphertext,
|
||||
nameIv: nameEncrypted.iv,
|
||||
});
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const requestFileRemovalFromCategory = async (fileId: number, categoryId: number) => {
|
||||
const res = await callPostApi<CategoryFileRemoveRequest>(
|
||||
`/api/category/${categoryId}/file/remove`,
|
||||
{ file: fileId },
|
||||
);
|
||||
return res.ok;
|
||||
const trpc = useTRPC();
|
||||
|
||||
try {
|
||||
await trpc.category.removeFile.mutate({ id: categoryId, file: fileId });
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,11 +11,8 @@ import {
|
||||
downloadFile,
|
||||
} from "$lib/modules/file";
|
||||
import { getThumbnailUrl } from "$lib/modules/thumbnail";
|
||||
import type {
|
||||
FileThumbnailInfoResponse,
|
||||
FileThumbnailUploadRequest,
|
||||
FileListResponse,
|
||||
} from "$lib/server/schemas";
|
||||
import type { FileThumbnailUploadRequest } from "$lib/server/schemas";
|
||||
import { useTRPC } from "$trpc/client";
|
||||
|
||||
export const requestFileDownload = async (
|
||||
fileId: number,
|
||||
@@ -52,12 +49,17 @@ export const requestFileThumbnailDownload = async (fileId: number, dataKey?: Cry
|
||||
const cache = await getFileThumbnailCache(fileId);
|
||||
if (cache || !dataKey) return cache;
|
||||
|
||||
let res = await callGetApi(`/api/file/${fileId}/thumbnail`);
|
||||
if (!res.ok) return null;
|
||||
const trpc = useTRPC();
|
||||
let thumbnailInfo;
|
||||
try {
|
||||
thumbnailInfo = await trpc.file.thumbnail.query({ id: fileId });
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return null;
|
||||
}
|
||||
const { contentIv: thumbnailEncryptedIv } = thumbnailInfo;
|
||||
|
||||
const { contentIv: thumbnailEncryptedIv }: FileThumbnailInfoResponse = await res.json();
|
||||
|
||||
res = await callGetApi(`/api/file/${fileId}/thumbnail/download`);
|
||||
const res = await callGetApi(`/api/file/${fileId}/thumbnail/download`);
|
||||
if (!res.ok) return null;
|
||||
|
||||
const thumbnailEncrypted = await res.arrayBuffer();
|
||||
@@ -68,10 +70,15 @@ export const requestFileThumbnailDownload = async (fileId: number, dataKey?: Cry
|
||||
};
|
||||
|
||||
export const requestDeletedFilesCleanup = async () => {
|
||||
const res = await callGetApi("/api/file/list");
|
||||
if (!res.ok) return;
|
||||
const trpc = useTRPC();
|
||||
let liveFiles;
|
||||
try {
|
||||
liveFiles = await trpc.file.list.query();
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return;
|
||||
}
|
||||
|
||||
const { files: liveFiles }: FileListResponse = await res.json();
|
||||
const liveFilesSet = new Set(liveFiles);
|
||||
const maybeCachedFiles = await getAllFileInfos();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user