From c9d4b1035602cc3f837f9b6272636e43e00ac656 Mon Sep 17 00:00:00 2001 From: static Date: Fri, 26 Dec 2025 15:45:03 +0900 Subject: [PATCH] =?UTF-8?q?=EC=82=AC=EC=86=8C=ED=95=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/modules/file/upload.ts | 5 ++--- src/lib/modules/filesystem.ts | 13 +++++------- src/lib/services/auth.ts | 10 ++++------ src/lib/services/category.ts | 8 +++----- src/lib/services/file.ts | 8 +++----- src/lib/services/key.ts | 18 ++++++----------- .../auth/changePassword/service.ts | 6 ++---- src/routes/(fullscreen)/auth/login/service.ts | 6 ++---- src/routes/(fullscreen)/file/[id]/service.ts | 6 ++---- .../(fullscreen)/settings/thumbnail/+page.ts | 7 +++---- .../(main)/category/[[id]]/service.svelte.ts | 9 +++------ .../(main)/directory/[[id]]/service.svelte.ts | 20 +++++++------------ src/routes/(main)/menu/+page.ts | 7 +++---- src/trpc/client.ts | 2 +- src/trpc/routers/directory.ts | 12 ++++++----- src/trpc/routers/file.ts | 1 + 16 files changed, 54 insertions(+), 84 deletions(-) diff --git a/src/lib/modules/file/upload.ts b/src/lib/modules/file/upload.ts index 7c48503..31aabd8 100644 --- a/src/lib/modules/file/upload.ts +++ b/src/lib/modules/file/upload.ts @@ -23,15 +23,14 @@ import { type HmacSecret, type FileUploadStatus, } from "$lib/stores"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; const requestDuplicateFileScan = limitFunction( async (file: File, hmacSecret: HmacSecret, onDuplicate: () => Promise) => { - const trpc = useTRPC(); const fileBuffer = await file.arrayBuffer(); const fileSigned = encodeToBase64(await signMessageHmac(fileBuffer, hmacSecret.secret)); - const files = await trpc.file.listByHash.query({ + const files = await trpc().file.listByHash.query({ hskVersion: hmacSecret.version, contentHmac: fileSigned, }); diff --git a/src/lib/modules/filesystem.ts b/src/lib/modules/filesystem.ts index 8b9b203..40e8cd4 100644 --- a/src/lib/modules/filesystem.ts +++ b/src/lib/modules/filesystem.ts @@ -18,7 +18,7 @@ import { type CategoryId, } from "$lib/indexedDB"; import { unwrapDataKey, decryptString } from "$lib/modules/crypto"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export type DirectoryInfo = | { @@ -101,10 +101,9 @@ const fetchDirectoryInfoFromServer = async ( info: Writable, masterKey: CryptoKey, ) => { - const trpc = useTRPC(); let data; try { - data = await trpc.directory.get.query({ id }); + data = await trpc().directory.get.query({ id }); } catch (e) { if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { info.set(null); @@ -174,10 +173,9 @@ const fetchFileInfoFromServer = async ( info: Writable, masterKey: CryptoKey, ) => { - const trpc = useTRPC(); let metadata; try { - metadata = await trpc.file.get.query({ id }); + metadata = await trpc().file.get.query({ id }); } catch (e) { if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { info.set(null); @@ -270,10 +268,9 @@ const fetchCategoryInfoFromServer = async ( info: Writable, masterKey: CryptoKey, ) => { - const trpc = useTRPC(); let data; try { - data = await trpc.category.get.query({ id }); + data = await trpc().category.get.query({ id }); } catch (e) { if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { info.set(null); @@ -293,7 +290,7 @@ const fetchCategoryInfoFromServer = async ( let files; try { - files = await trpc.category.files.query({ id, recurse: true }); + files = await trpc().category.files.query({ id, recurse: true }); } catch { throw new Error("Failed to fetch category files"); } diff --git a/src/lib/services/auth.ts b/src/lib/services/auth.ts index 2131943..56d67eb 100644 --- a/src/lib/services/auth.ts +++ b/src/lib/services/auth.ts @@ -1,6 +1,6 @@ import { TRPCClientError } from "@trpc/client"; import { encodeToBase64, decryptChallenge, signMessageRSA } from "$lib/modules/crypto"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export const requestSessionUpgrade = async ( encryptKeyBase64: string, @@ -9,10 +9,9 @@ export const requestSessionUpgrade = async ( signKey: CryptoKey, force = false, ) => { - const trpc = useTRPC(); let id, challenge; try { - ({ id, challenge } = await trpc.auth.upgrade.mutate({ + ({ id, challenge } = await trpc().auth.upgrade.mutate({ encPubKey: encryptKeyBase64, sigPubKey: verifyKeyBase64, })); @@ -26,7 +25,7 @@ export const requestSessionUpgrade = async ( const answerSig = await signMessageRSA(answer, signKey); try { - await trpc.auth.verifyUpgrade.mutate({ + await trpc().auth.verifyUpgrade.mutate({ id, answerSig: encodeToBase64(answerSig), force, @@ -42,9 +41,8 @@ export const requestSessionUpgrade = async ( }; export const requestLogout = async () => { - const trpc = useTRPC(); try { - await trpc.auth.logout.mutate(); + await trpc().auth.logout.mutate(); return true; } catch { // TODO: Error Handling diff --git a/src/lib/services/category.ts b/src/lib/services/category.ts index c86c93b..7e0443f 100644 --- a/src/lib/services/category.ts +++ b/src/lib/services/category.ts @@ -1,18 +1,17 @@ import { generateDataKey, wrapDataKey, encryptString } from "$lib/modules/crypto"; import type { MasterKey } from "$lib/stores"; -import { useTRPC } from "$trpc/client"; +import { trpc } 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); try { - await trpc.category.create.mutate({ + await trpc().category.create.mutate({ parent: parentId, mekVersion: masterKey.version, dek: await wrapDataKey(dataKey, masterKey.key), @@ -28,10 +27,9 @@ export const requestCategoryCreation = async ( }; export const requestFileRemovalFromCategory = async (fileId: number, categoryId: number) => { - const trpc = useTRPC(); try { - await trpc.category.removeFile.mutate({ id: categoryId, file: fileId }); + await trpc().category.removeFile.mutate({ id: categoryId, file: fileId }); return true; } catch { // TODO: Error Handling diff --git a/src/lib/services/file.ts b/src/lib/services/file.ts index f428e97..7e93c7a 100644 --- a/src/lib/services/file.ts +++ b/src/lib/services/file.ts @@ -12,7 +12,7 @@ import { } from "$lib/modules/file"; import { getThumbnailUrl } from "$lib/modules/thumbnail"; import type { FileThumbnailUploadRequest } from "$lib/server/schemas"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export const requestFileDownload = async ( fileId: number, @@ -49,10 +49,9 @@ export const requestFileThumbnailDownload = async (fileId: number, dataKey?: Cry const cache = await getFileThumbnailCache(fileId); if (cache || !dataKey) return cache; - const trpc = useTRPC(); let thumbnailInfo; try { - thumbnailInfo = await trpc.file.thumbnail.query({ id: fileId }); + thumbnailInfo = await trpc().file.thumbnail.query({ id: fileId }); } catch { // TODO: Error Handling return null; @@ -70,10 +69,9 @@ export const requestFileThumbnailDownload = async (fileId: number, dataKey?: Cry }; export const requestDeletedFilesCleanup = async () => { - const trpc = useTRPC(); let liveFiles; try { - liveFiles = await trpc.file.list.query(); + liveFiles = await trpc().file.list.query(); } catch { // TODO: Error Handling return; diff --git a/src/lib/services/key.ts b/src/lib/services/key.ts index 6149829..fd89f74 100644 --- a/src/lib/services/key.ts +++ b/src/lib/services/key.ts @@ -11,7 +11,7 @@ import { } from "$lib/modules/crypto"; import { requestSessionUpgrade } from "$lib/services/auth"; import { masterKeyStore, type ClientKeys } from "$lib/stores"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export const requestClientRegistration = async ( encryptKeyBase64: string, @@ -19,16 +19,14 @@ export const requestClientRegistration = async ( verifyKeyBase64: string, signKey: CryptoKey, ) => { - const trpc = useTRPC(); - try { - const { id, challenge } = await trpc.client.register.mutate({ + const { id, challenge } = await trpc().client.register.mutate({ encPubKey: encryptKeyBase64, sigPubKey: verifyKeyBase64, }); const answer = await decryptChallenge(challenge, decryptKey); const answerSig = await signMessageRSA(answer, signKey); - await trpc.client.verify.mutate({ + await trpc().client.verify.mutate({ id, answerSig: encodeToBase64(answerSig), }); @@ -69,11 +67,9 @@ export const requestClientRegistrationAndSessionUpgrade = async ( }; export const requestMasterKeyDownload = async (decryptKey: CryptoKey, verifyKey: CryptoKey) => { - const trpc = useTRPC(); - let masterKeysWrapped; try { - masterKeysWrapped = await trpc.mek.list.query(); + masterKeysWrapped = await trpc().mek.list.query(); } catch { // TODO: Error Handling return false; @@ -110,10 +106,8 @@ export const requestInitialMasterKeyAndHmacSecretRegistration = async ( hmacSecretWrapped: string, signKey: CryptoKey, ) => { - const trpc = useTRPC(); - try { - await trpc.mek.registerInitial.mutate({ + await trpc().mek.registerInitial.mutate({ mek: masterKeyWrapped, mekSig: await signMasterKeyWrapped(masterKeyWrapped, 1, signKey), }); @@ -129,7 +123,7 @@ export const requestInitialMasterKeyAndHmacSecretRegistration = async ( } try { - await trpc.hsk.registerInitial.mutate({ + await trpc().hsk.registerInitial.mutate({ mekVersion: 1, hsk: hmacSecretWrapped, }); diff --git a/src/routes/(fullscreen)/auth/changePassword/service.ts b/src/routes/(fullscreen)/auth/changePassword/service.ts index 699ec7f..4673611 100644 --- a/src/routes/(fullscreen)/auth/changePassword/service.ts +++ b/src/routes/(fullscreen)/auth/changePassword/service.ts @@ -1,10 +1,8 @@ -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export const requestPasswordChange = async (oldPassword: string, newPassword: string) => { - const trpc = useTRPC(); - try { - await trpc.auth.changePassword.mutate({ oldPassword, newPassword }); + await trpc().auth.changePassword.mutate({ oldPassword, newPassword }); return true; } catch { // TODO: Error Handling diff --git a/src/routes/(fullscreen)/auth/login/service.ts b/src/routes/(fullscreen)/auth/login/service.ts index 0d57545..5c8219a 100644 --- a/src/routes/(fullscreen)/auth/login/service.ts +++ b/src/routes/(fullscreen)/auth/login/service.ts @@ -1,4 +1,4 @@ -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export { requestLogout } from "$lib/services/auth"; export { requestDeletedFilesCleanup } from "$lib/services/file"; @@ -8,10 +8,8 @@ export { } from "$lib/services/key"; export const requestLogin = async (email: string, password: string) => { - const trpc = useTRPC(); - try { - await trpc.auth.login.mutate({ email, password }); + await trpc().auth.login.mutate({ email, password }); return true; } catch { // TODO: Error Handling diff --git a/src/routes/(fullscreen)/file/[id]/service.ts b/src/routes/(fullscreen)/file/[id]/service.ts index 73ca7f1..09ec86f 100644 --- a/src/routes/(fullscreen)/file/[id]/service.ts +++ b/src/routes/(fullscreen)/file/[id]/service.ts @@ -1,7 +1,7 @@ import { encryptData } from "$lib/modules/crypto"; import { storeFileThumbnailCache } from "$lib/modules/file"; import { requestFileThumbnailUpload } from "$lib/services/file"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export { requestCategoryCreation, requestFileRemovalFromCategory } from "$lib/services/category"; export { requestFileDownload } from "$lib/services/file"; @@ -22,10 +22,8 @@ export const requestThumbnailUpload = async ( }; export const requestFileAdditionToCategory = async (fileId: number, categoryId: number) => { - const trpc = useTRPC(); - try { - await trpc.category.addFile.mutate({ id: categoryId, file: fileId }); + await trpc().category.addFile.mutate({ id: categoryId, file: fileId }); return true; } catch { // TODO: Error Handling diff --git a/src/routes/(fullscreen)/settings/thumbnail/+page.ts b/src/routes/(fullscreen)/settings/thumbnail/+page.ts index 3bfa322..f435b72 100644 --- a/src/routes/(fullscreen)/settings/thumbnail/+page.ts +++ b/src/routes/(fullscreen)/settings/thumbnail/+page.ts @@ -1,14 +1,13 @@ import { error } from "@sveltejs/kit"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; import type { PageLoad } from "./$types"; export const load: PageLoad = async ({ fetch }) => { - const trpc = useTRPC(fetch); - try { - const files = await trpc.file.listWithoutThumbnail.query(); + const files = await trpc(fetch).file.listWithoutThumbnail.query(); return { files }; } catch { + // TODO: Error Handling error(500, "Internal server error"); } }; diff --git a/src/routes/(main)/category/[[id]]/service.svelte.ts b/src/routes/(main)/category/[[id]]/service.svelte.ts index 824de8a..18f68fd 100644 --- a/src/routes/(main)/category/[[id]]/service.svelte.ts +++ b/src/routes/(main)/category/[[id]]/service.svelte.ts @@ -1,7 +1,7 @@ import { getContext, setContext } from "svelte"; import { encryptString } from "$lib/modules/crypto"; import type { SelectedCategory } from "$lib/components/molecules"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export { requestCategoryCreation, requestFileRemovalFromCategory } from "$lib/services/category"; @@ -17,11 +17,10 @@ export const useContext = () => { }; export const requestCategoryRename = async (category: SelectedCategory, newName: string) => { - const trpc = useTRPC(); const newNameEncrypted = await encryptString(newName, category.dataKey); try { - await trpc.category.rename.mutate({ + await trpc().category.rename.mutate({ id: category.id, dekVersion: category.dataKeyVersion, name: newNameEncrypted.ciphertext, @@ -35,10 +34,8 @@ export const requestCategoryRename = async (category: SelectedCategory, newName: }; export const requestCategoryDeletion = async (category: SelectedCategory) => { - const trpc = useTRPC(); - try { - await trpc.category.delete.mutate({ id: category.id }); + await trpc().category.delete.mutate({ id: category.id }); return true; } catch { // TODO: Error Handling diff --git a/src/routes/(main)/directory/[[id]]/service.svelte.ts b/src/routes/(main)/directory/[[id]]/service.svelte.ts index 72a8fdb..c94cc1e 100644 --- a/src/routes/(main)/directory/[[id]]/service.svelte.ts +++ b/src/routes/(main)/directory/[[id]]/service.svelte.ts @@ -9,7 +9,7 @@ import { uploadFile, } from "$lib/modules/file"; import { hmacSecretStore, type MasterKey, type HmacSecret } from "$lib/stores"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; export interface SelectedEntry { type: "directory" | "file"; @@ -33,11 +33,9 @@ export const useContext = () => { export const requestHmacSecretDownload = async (masterKey: CryptoKey) => { // TODO: MEK rotation - const trpc = useTRPC(); - let hmacSecretsWrapped; try { - hmacSecretsWrapped = await trpc.hsk.list.query(); + hmacSecretsWrapped = await trpc().hsk.list.query(); } catch { // TODO: Error Handling return false; @@ -61,12 +59,11 @@ export const requestDirectoryCreation = async ( parentId: "root" | number, masterKey: MasterKey, ) => { - const trpc = useTRPC(); const { dataKey, dataKeyVersion } = await generateDataKey(); const nameEncrypted = await encryptString(name, dataKey); try { - await trpc.directory.create.mutate({ + await trpc().directory.create.mutate({ parent: parentId, mekVersion: masterKey.version, dek: await wrapDataKey(dataKey, masterKey.key), @@ -100,19 +97,18 @@ export const requestFileUpload = async ( }; export const requestEntryRename = async (entry: SelectedEntry, newName: string) => { - const trpc = useTRPC(); const newNameEncrypted = await encryptString(newName, entry.dataKey); try { if (entry.type === "directory") { - await trpc.directory.rename.mutate({ + await trpc().directory.rename.mutate({ id: entry.id, dekVersion: entry.dataKeyVersion, name: newNameEncrypted.ciphertext, nameIv: newNameEncrypted.iv, }); } else { - await trpc.file.rename.mutate({ + await trpc().file.rename.mutate({ id: entry.id, dekVersion: entry.dataKeyVersion, name: newNameEncrypted.ciphertext, @@ -127,11 +123,9 @@ export const requestEntryRename = async (entry: SelectedEntry, newName: string) }; export const requestEntryDeletion = async (entry: SelectedEntry) => { - const trpc = useTRPC(); - try { if (entry.type === "directory") { - const { deletedFiles } = await trpc.directory.delete.mutate({ id: entry.id }); + const { deletedFiles } = await trpc().directory.delete.mutate({ id: entry.id }); await Promise.all( deletedFiles.flatMap((fileId) => [ deleteFileCache(fileId), @@ -139,7 +133,7 @@ export const requestEntryDeletion = async (entry: SelectedEntry) => { ]), ); } else { - await trpc.file.delete.mutate({ id: entry.id }); + await trpc().file.delete.mutate({ id: entry.id }); await Promise.all([deleteFileCache(entry.id), deleteFileThumbnailCache(entry.id)]); } return true; diff --git a/src/routes/(main)/menu/+page.ts b/src/routes/(main)/menu/+page.ts index ecd8f0b..e97e395 100644 --- a/src/routes/(main)/menu/+page.ts +++ b/src/routes/(main)/menu/+page.ts @@ -1,14 +1,13 @@ import { error } from "@sveltejs/kit"; -import { useTRPC } from "$trpc/client"; +import { trpc } from "$trpc/client"; import type { PageLoad } from "./$types"; export const load: PageLoad = async ({ fetch }) => { - const trpc = useTRPC(fetch); - try { - const { nickname } = await trpc.user.get.query(); + const { nickname } = await trpc(fetch).user.get.query(); return { nickname }; } catch { + // TODO: Error Handling error(500, "Internal server error"); } }; diff --git a/src/trpc/client.ts b/src/trpc/client.ts index cb1e8c5..a24cd5d 100644 --- a/src/trpc/client.ts +++ b/src/trpc/client.ts @@ -16,7 +16,7 @@ const createClient = (fetch: typeof globalThis.fetch) => let browserClient: ReturnType; -export const useTRPC = (fetch = globalThis.fetch) => { +export const trpc = (fetch = globalThis.fetch) => { const client = browserClient ?? createClient(fetch); if (browser) { browserClient ??= client; diff --git a/src/trpc/routers/directory.ts b/src/trpc/routers/directory.ts index bcd31c7..e060c23 100644 --- a/src/trpc/routers/directory.ts +++ b/src/trpc/routers/directory.ts @@ -108,11 +108,13 @@ const directoryRouter = router({ .mutation(async ({ ctx, input }) => { try { const files = await FileRepo.unregisterDirectory(ctx.session.userId, input.id); - files.forEach(({ path, thumbnailPath }) => { - safeUnlink(path); // Intended - safeUnlink(thumbnailPath); // Intended - }); - return { deletedFiles: files.map(({ id }) => id) }; + return { + deletedFiles: files.map((file) => { + safeUnlink(file.path); // Intended + safeUnlink(file.thumbnailPath); // Intended + return file.id; + }), + }; } catch (e) { if (e instanceof IntegrityError && e.message === "Directory not found") { throw new TRPCError({ code: "NOT_FOUND", message: "Invalid directory id" }); diff --git a/src/trpc/routers/file.ts b/src/trpc/routers/file.ts index 7c4e425..decbc76 100644 --- a/src/trpc/routers/file.ts +++ b/src/trpc/routers/file.ts @@ -115,6 +115,7 @@ const fileRouter = router({ if (!thumbnail) { throw new TRPCError({ code: "NOT_FOUND", message: "File or its thumbnail not found" }); } + return { updatedAt: thumbnail.updatedAt, contentIv: thumbnail.encContentIv }; }), });