mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 08:06:56 +00:00
파일 관련 API 요청을 TanStack Query로 마이그레이션
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
import { get, type Writable } from "svelte/store";
|
import { get, type Writable } from "svelte/store";
|
||||||
import { CheckBox } from "$lib/components/atoms";
|
import { CheckBox } from "$lib/components/atoms";
|
||||||
import { SubCategories, type SelectedCategory } from "$lib/components/molecules";
|
import { SubCategories, type SelectedCategory } from "$lib/components/molecules";
|
||||||
import { getFileInfo, type FileInfo, type CategoryInfo } from "$lib/modules/filesystem";
|
import type { CategoryInfo } from "$lib/modules/filesystem";
|
||||||
|
import { getFileInfo, type FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { SortBy, sortEntries } from "$lib/modules/util";
|
import { SortBy, sortEntries } from "$lib/modules/util";
|
||||||
import { masterKeyStore } from "$lib/stores";
|
import { masterKeyStore } from "$lib/stores";
|
||||||
import File from "./File.svelte";
|
import File from "./File.svelte";
|
||||||
@@ -33,9 +34,7 @@
|
|||||||
isFileRecursive = $bindable(),
|
isFileRecursive = $bindable(),
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
|
|
||||||
let files: { name?: string; info: Writable<FileInfo | null>; isRecursive: boolean }[] = $state(
|
let files: { name?: string; info: FileInfoStore; isRecursive: boolean }[] = $state([]);
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
files =
|
files =
|
||||||
@@ -44,7 +43,7 @@
|
|||||||
.map(({ id, isRecursive }) => {
|
.map(({ id, isRecursive }) => {
|
||||||
const info = getFileInfo(id, $masterKeyStore?.get(1)?.key!);
|
const info = getFileInfo(id, $masterKeyStore?.get(1)?.key!);
|
||||||
return {
|
return {
|
||||||
name: get(info)?.name,
|
name: get(info).data?.name,
|
||||||
info,
|
info,
|
||||||
isRecursive,
|
isRecursive,
|
||||||
};
|
};
|
||||||
@@ -58,8 +57,8 @@
|
|||||||
|
|
||||||
const unsubscribes = files.map((file) =>
|
const unsubscribes = files.map((file) =>
|
||||||
file.info.subscribe((value) => {
|
file.info.subscribe((value) => {
|
||||||
if (file.name === value?.name) return;
|
if (file.name === value.data?.name) return;
|
||||||
file.name = value?.name;
|
file.name = value.data?.name;
|
||||||
sort();
|
sort();
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
import type { Writable } from "svelte/store";
|
import type { Writable } from "svelte/store";
|
||||||
import { ActionEntryButton } from "$lib/components/atoms";
|
import { ActionEntryButton } from "$lib/components/atoms";
|
||||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||||
import type { FileInfo } from "$lib/modules/filesystem";
|
import type { FileInfo, FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { requestFileThumbnailDownload, type SelectedFile } from "./service";
|
import { requestFileThumbnailDownload, type SelectedFile } from "./service";
|
||||||
|
|
||||||
import IconClose from "~icons/material-symbols/close";
|
import IconClose from "~icons/material-symbols/close";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
onclick: (selectedFile: SelectedFile) => void;
|
onclick: (selectedFile: SelectedFile) => void;
|
||||||
onRemoveClick?: (selectedFile: SelectedFile) => void;
|
onRemoveClick?: (selectedFile: SelectedFile) => void;
|
||||||
}
|
}
|
||||||
@@ -18,22 +18,22 @@
|
|||||||
let thumbnail: string | undefined = $state();
|
let thumbnail: string | undefined = $state();
|
||||||
|
|
||||||
const openFile = () => {
|
const openFile = () => {
|
||||||
const { id, dataKey, dataKeyVersion, name } = $info as FileInfo;
|
const { id, dataKey, dataKeyVersion, name } = $info.data as FileInfo;
|
||||||
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
||||||
|
|
||||||
onclick({ id, dataKey, dataKeyVersion, name });
|
onclick({ id, dataKey, dataKeyVersion, name });
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeFile = () => {
|
const removeFile = () => {
|
||||||
const { id, dataKey, dataKeyVersion, name } = $info as FileInfo;
|
const { id, dataKey, dataKeyVersion, name } = $info.data as FileInfo;
|
||||||
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
||||||
|
|
||||||
onRemoveClick!({ id, dataKey, dataKeyVersion, name });
|
onRemoveClick!({ id, dataKey, dataKeyVersion, name });
|
||||||
};
|
};
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if ($info?.dataKey) {
|
if ($info.data?.dataKey) {
|
||||||
requestFileThumbnailDownload($info.id, $info.dataKey)
|
requestFileThumbnailDownload($info.data.id, $info.data.dataKey)
|
||||||
.then((thumbnailUrl) => {
|
.then((thumbnailUrl) => {
|
||||||
thumbnail = thumbnailUrl ?? undefined;
|
thumbnail = thumbnailUrl ?? undefined;
|
||||||
})
|
})
|
||||||
@@ -47,13 +47,13 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $info}
|
{#if $info.status === "success"}
|
||||||
<ActionEntryButton
|
<ActionEntryButton
|
||||||
class="h-12"
|
class="h-12"
|
||||||
onclick={openFile}
|
onclick={openFile}
|
||||||
actionButtonIcon={onRemoveClick && IconClose}
|
actionButtonIcon={onRemoveClick && IconClose}
|
||||||
onActionButtonClick={removeFile}
|
onActionButtonClick={removeFile}
|
||||||
>
|
>
|
||||||
<DirectoryEntryLabel type="file" {thumbnail} name={$info.name} />
|
<DirectoryEntryLabel type="file" {thumbnail} name={$info.data.name} />
|
||||||
</ActionEntryButton>
|
</ActionEntryButton>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -86,6 +86,10 @@ export const storeFileInfo = async (fileInfo: FileInfo) => {
|
|||||||
await filesystem.file.put(fileInfo);
|
await filesystem.file.put(fileInfo);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const updateFileInfo = async (id: number, changes: { name?: string }) => {
|
||||||
|
await filesystem.file.update(id, changes);
|
||||||
|
};
|
||||||
|
|
||||||
export const deleteFileInfo = async (id: number) => {
|
export const deleteFileInfo = async (id: number) => {
|
||||||
await filesystem.file.delete(id);
|
await filesystem.file.delete(id);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { writable, type Writable } from "svelte/store";
|
|||||||
import {
|
import {
|
||||||
encodeToBase64,
|
encodeToBase64,
|
||||||
generateDataKey,
|
generateDataKey,
|
||||||
|
makeAESKeyNonextractable,
|
||||||
wrapDataKey,
|
wrapDataKey,
|
||||||
encryptData,
|
encryptData,
|
||||||
encryptString,
|
encryptString,
|
||||||
@@ -118,12 +119,14 @@ const encryptFile = limitFunction(
|
|||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
dataKey: await makeAESKeyNonextractable(dataKey),
|
||||||
dataKeyWrapped,
|
dataKeyWrapped,
|
||||||
dataKeyVersion,
|
dataKeyVersion,
|
||||||
fileType,
|
fileType,
|
||||||
fileEncrypted,
|
fileEncrypted,
|
||||||
fileEncryptedHash,
|
fileEncryptedHash,
|
||||||
nameEncrypted,
|
nameEncrypted,
|
||||||
|
createdAt,
|
||||||
createdAtEncrypted,
|
createdAtEncrypted,
|
||||||
lastModifiedAtEncrypted,
|
lastModifiedAtEncrypted,
|
||||||
thumbnail: thumbnailEncrypted && { plaintext: thumbnailBuffer, ...thumbnailEncrypted },
|
thumbnail: thumbnailEncrypted && { plaintext: thumbnailBuffer, ...thumbnailEncrypted },
|
||||||
@@ -176,9 +179,7 @@ export const uploadFile = async (
|
|||||||
hmacSecret: HmacSecret,
|
hmacSecret: HmacSecret,
|
||||||
masterKey: MasterKey,
|
masterKey: MasterKey,
|
||||||
onDuplicate: () => Promise<boolean>,
|
onDuplicate: () => Promise<boolean>,
|
||||||
): Promise<
|
) => {
|
||||||
{ fileId: number; fileBuffer: ArrayBuffer; thumbnailBuffer?: ArrayBuffer } | undefined
|
|
||||||
> => {
|
|
||||||
const status = writable<FileUploadStatus>({
|
const status = writable<FileUploadStatus>({
|
||||||
name: file.name,
|
name: file.name,
|
||||||
parentId,
|
parentId,
|
||||||
@@ -208,12 +209,14 @@ export const uploadFile = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
dataKey,
|
||||||
dataKeyWrapped,
|
dataKeyWrapped,
|
||||||
dataKeyVersion,
|
dataKeyVersion,
|
||||||
fileType,
|
fileType,
|
||||||
fileEncrypted,
|
fileEncrypted,
|
||||||
fileEncryptedHash,
|
fileEncryptedHash,
|
||||||
nameEncrypted,
|
nameEncrypted,
|
||||||
|
createdAt,
|
||||||
createdAtEncrypted,
|
createdAtEncrypted,
|
||||||
lastModifiedAtEncrypted,
|
lastModifiedAtEncrypted,
|
||||||
thumbnail,
|
thumbnail,
|
||||||
@@ -256,7 +259,16 @@ export const uploadFile = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { fileId } = await requestFileUpload(status, form, thumbnailForm);
|
const { fileId } = await requestFileUpload(status, form, thumbnailForm);
|
||||||
return { fileId, fileBuffer, thumbnailBuffer: thumbnail?.plaintext };
|
return {
|
||||||
|
fileId,
|
||||||
|
fileDataKey: dataKey,
|
||||||
|
fileDataKeyVersion: dataKeyVersion,
|
||||||
|
fileType,
|
||||||
|
fileEncryptedIv: fileEncrypted.iv,
|
||||||
|
fileCreatedAt: createdAt,
|
||||||
|
fileBuffer,
|
||||||
|
thumbnailBuffer: thumbnail?.plaintext,
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
status.update((value) => {
|
status.update((value) => {
|
||||||
value.status = "error";
|
value.status = "error";
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { get, writable, type Writable } from "svelte/store";
|
import { get, writable, type Writable } from "svelte/store";
|
||||||
import { callGetApi } from "$lib/hooks";
|
import { callGetApi } from "$lib/hooks";
|
||||||
import {
|
import {
|
||||||
getFileInfo as getFileInfoFromIndexedDB,
|
|
||||||
storeFileInfo,
|
|
||||||
deleteFileInfo,
|
|
||||||
getCategoryInfos as getCategoryInfosFromIndexedDB,
|
getCategoryInfos as getCategoryInfosFromIndexedDB,
|
||||||
getCategoryInfo as getCategoryInfoFromIndexedDB,
|
getCategoryInfo as getCategoryInfoFromIndexedDB,
|
||||||
storeCategoryInfo,
|
storeCategoryInfo,
|
||||||
@@ -12,23 +9,7 @@ import {
|
|||||||
type CategoryId,
|
type CategoryId,
|
||||||
} from "$lib/indexedDB";
|
} from "$lib/indexedDB";
|
||||||
import { unwrapDataKey, decryptString } from "$lib/modules/crypto";
|
import { unwrapDataKey, decryptString } from "$lib/modules/crypto";
|
||||||
import type {
|
import type { CategoryInfoResponse, CategoryFileListResponse } from "$lib/server/schemas";
|
||||||
CategoryInfoResponse,
|
|
||||||
CategoryFileListResponse,
|
|
||||||
FileInfoResponse,
|
|
||||||
} from "$lib/server/schemas";
|
|
||||||
|
|
||||||
export interface FileInfo {
|
|
||||||
id: number;
|
|
||||||
dataKey?: CryptoKey;
|
|
||||||
dataKeyVersion?: Date;
|
|
||||||
contentType: string;
|
|
||||||
contentIv?: string;
|
|
||||||
name: string;
|
|
||||||
createdAt?: Date;
|
|
||||||
lastModifiedAt: Date;
|
|
||||||
categoryIds: number[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CategoryInfo =
|
export type CategoryInfo =
|
||||||
| {
|
| {
|
||||||
@@ -50,90 +31,8 @@ export type CategoryInfo =
|
|||||||
isFileRecursive: boolean;
|
isFileRecursive: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fileInfoStore = new Map<number, Writable<FileInfo | null>>();
|
|
||||||
const categoryInfoStore = new Map<CategoryId, Writable<CategoryInfo | null>>();
|
const categoryInfoStore = new Map<CategoryId, Writable<CategoryInfo | null>>();
|
||||||
|
|
||||||
const fetchFileInfoFromIndexedDB = async (id: number, info: Writable<FileInfo | null>) => {
|
|
||||||
if (get(info)) return;
|
|
||||||
|
|
||||||
const file = await getFileInfoFromIndexedDB(id);
|
|
||||||
if (!file) return;
|
|
||||||
|
|
||||||
info.set(file);
|
|
||||||
};
|
|
||||||
|
|
||||||
const decryptDate = async (ciphertext: string, iv: string, dataKey: CryptoKey) => {
|
|
||||||
return new Date(parseInt(await decryptString(ciphertext, iv, dataKey), 10));
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchFileInfoFromServer = async (
|
|
||||||
id: number,
|
|
||||||
info: Writable<FileInfo | null>,
|
|
||||||
masterKey: CryptoKey,
|
|
||||||
) => {
|
|
||||||
const res = await callGetApi(`/api/file/${id}`);
|
|
||||||
if (res.status === 404) {
|
|
||||||
info.set(null);
|
|
||||||
await deleteFileInfo(id);
|
|
||||||
return;
|
|
||||||
} else if (!res.ok) {
|
|
||||||
throw new Error("Failed to fetch file information");
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadata: FileInfoResponse = await res.json();
|
|
||||||
const { dataKey } = await unwrapDataKey(metadata.dek, masterKey);
|
|
||||||
|
|
||||||
const name = await decryptString(metadata.name, metadata.nameIv, dataKey);
|
|
||||||
const createdAt =
|
|
||||||
metadata.createdAt && metadata.createdAtIv
|
|
||||||
? await decryptDate(metadata.createdAt, metadata.createdAtIv, dataKey)
|
|
||||||
: undefined;
|
|
||||||
const lastModifiedAt = await decryptDate(
|
|
||||||
metadata.lastModifiedAt,
|
|
||||||
metadata.lastModifiedAtIv,
|
|
||||||
dataKey,
|
|
||||||
);
|
|
||||||
|
|
||||||
info.set({
|
|
||||||
id,
|
|
||||||
dataKey,
|
|
||||||
dataKeyVersion: new Date(metadata.dekVersion),
|
|
||||||
contentType: metadata.contentType,
|
|
||||||
contentIv: metadata.contentIv,
|
|
||||||
name,
|
|
||||||
createdAt,
|
|
||||||
lastModifiedAt,
|
|
||||||
categoryIds: metadata.categories,
|
|
||||||
});
|
|
||||||
await storeFileInfo({
|
|
||||||
id,
|
|
||||||
parentId: metadata.parent,
|
|
||||||
name,
|
|
||||||
contentType: metadata.contentType,
|
|
||||||
createdAt,
|
|
||||||
lastModifiedAt,
|
|
||||||
categoryIds: metadata.categories,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchFileInfo = async (id: number, info: Writable<FileInfo | null>, masterKey: CryptoKey) => {
|
|
||||||
await fetchFileInfoFromIndexedDB(id, info);
|
|
||||||
await fetchFileInfoFromServer(id, info, masterKey);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getFileInfo = (fileId: number, masterKey: CryptoKey) => {
|
|
||||||
// TODO: MEK rotation
|
|
||||||
|
|
||||||
let info = fileInfoStore.get(fileId);
|
|
||||||
if (!info) {
|
|
||||||
info = writable(null);
|
|
||||||
fileInfoStore.set(fileId, info);
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchFileInfo(fileId, info, masterKey); // Intended
|
|
||||||
return info;
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchCategoryInfoFromIndexedDB = async (
|
const fetchCategoryInfoFromIndexedDB = async (
|
||||||
id: CategoryId,
|
id: CategoryId,
|
||||||
info: Writable<CategoryInfo | null>,
|
info: Writable<CategoryInfo | null>,
|
||||||
|
|||||||
@@ -8,16 +8,8 @@ import {
|
|||||||
updateDirectoryInfo,
|
updateDirectoryInfo,
|
||||||
deleteDirectoryInfo,
|
deleteDirectoryInfo,
|
||||||
getFileInfos as getFileInfosFromIndexedDB,
|
getFileInfos as getFileInfosFromIndexedDB,
|
||||||
getFileInfo as getFileInfoFromIndexedDB,
|
|
||||||
storeFileInfo,
|
|
||||||
deleteFileInfo,
|
deleteFileInfo,
|
||||||
getCategoryInfos as getCategoryInfosFromIndexedDB,
|
|
||||||
getCategoryInfo as getCategoryInfoFromIndexedDB,
|
|
||||||
storeCategoryInfo,
|
|
||||||
updateCategoryInfo as updateCategoryInfoInIndexedDB,
|
|
||||||
deleteCategoryInfo,
|
|
||||||
type DirectoryId,
|
type DirectoryId,
|
||||||
type CategoryId,
|
|
||||||
} from "$lib/indexedDB";
|
} from "$lib/indexedDB";
|
||||||
import {
|
import {
|
||||||
generateDataKey,
|
generateDataKey,
|
||||||
@@ -53,14 +45,14 @@ export type DirectoryInfo =
|
|||||||
fileIds: number[];
|
fileIds: number[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const initializedDirectoryIds = new Set<DirectoryId>();
|
const initializedIds = new Set<DirectoryId>();
|
||||||
let temporaryIdCounter = -1;
|
let temporaryIdCounter = -1;
|
||||||
|
|
||||||
const getInitialDirectoryInfo = async (id: DirectoryId) => {
|
const getInitialDirectoryInfo = async (id: DirectoryId) => {
|
||||||
if (!browser || initializedDirectoryIds.has(id)) {
|
if (!browser || initializedIds.has(id)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
} else {
|
} else {
|
||||||
initializedDirectoryIds.add(id);
|
initializedIds.add(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [directory, subDirectories, files] = await Promise.all([
|
const [directory, subDirectories, files] = await Promise.all([
|
||||||
@@ -254,7 +246,9 @@ export const useDirectoryDeletion = (parentId: DirectoryId) => {
|
|||||||
if (!prevParentInfo) return undefined;
|
if (!prevParentInfo) return undefined;
|
||||||
return {
|
return {
|
||||||
...prevParentInfo,
|
...prevParentInfo,
|
||||||
subDirectoryIds: prevParentInfo.subDirectoryIds.filter((subId) => subId !== id),
|
subDirectoryIds: prevParentInfo.subDirectoryIds.filter(
|
||||||
|
(subDirectoryId) => subDirectoryId !== id,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return {};
|
return {};
|
||||||
238
src/lib/modules/filesystem2/file.ts
Normal file
238
src/lib/modules/filesystem2/file.ts
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
import { useQueryClient, createQuery, createMutation } from "@tanstack/svelte-query";
|
||||||
|
import { browser } from "$app/environment";
|
||||||
|
import { callGetApi, callPostApi } from "$lib/hooks";
|
||||||
|
import {
|
||||||
|
getFileInfo as getFileInfoFromIndexedDB,
|
||||||
|
storeFileInfo,
|
||||||
|
updateFileInfo,
|
||||||
|
deleteFileInfo,
|
||||||
|
type DirectoryId,
|
||||||
|
} from "$lib/indexedDB";
|
||||||
|
import { unwrapDataKey, encryptString, decryptString } from "$lib/modules/crypto";
|
||||||
|
import { uploadFile } from "$lib/modules/file";
|
||||||
|
import type { FileInfoResponse, FileRenameRequest } from "$lib/server/schemas";
|
||||||
|
import type { MasterKey, HmacSecret } from "$lib/stores";
|
||||||
|
import type { DirectoryInfo } from "./directory";
|
||||||
|
|
||||||
|
export interface FileInfo {
|
||||||
|
id: number;
|
||||||
|
dataKey?: CryptoKey;
|
||||||
|
dataKeyVersion?: Date;
|
||||||
|
contentType: string;
|
||||||
|
contentIv?: string;
|
||||||
|
name: string;
|
||||||
|
createdAt?: Date;
|
||||||
|
lastModifiedAt: Date;
|
||||||
|
categoryIds: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const initializedFileIds = new Set<number>();
|
||||||
|
|
||||||
|
const getInitialFileInfo = async (id: number) => {
|
||||||
|
if (!browser || initializedFileIds.has(id)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
initializedFileIds.add(id);
|
||||||
|
return await getFileInfoFromIndexedDB(id);
|
||||||
|
};
|
||||||
|
|
||||||
|
const decryptDate = async (ciphertext: string, iv: string, dataKey: CryptoKey) => {
|
||||||
|
return new Date(parseInt(await decryptString(ciphertext, iv, dataKey), 10));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getFileInfo = (id: number, masterKey: CryptoKey) => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
getInitialFileInfo(id).then((info) => {
|
||||||
|
if (info && !queryClient.getQueryData(["file", id])) {
|
||||||
|
queryClient.setQueryData<FileInfo>(["file", id], info);
|
||||||
|
}
|
||||||
|
}); // Intended
|
||||||
|
return createQuery<FileInfo>({
|
||||||
|
queryKey: ["file", id],
|
||||||
|
queryFn: async () => {
|
||||||
|
const res = await callGetApi(`/api/file/${id}`); // TODO: 404
|
||||||
|
const metadata: FileInfoResponse = await res.json();
|
||||||
|
|
||||||
|
const { dataKey } = await unwrapDataKey(metadata.dek, masterKey);
|
||||||
|
const name = await decryptString(metadata.name, metadata.nameIv, dataKey);
|
||||||
|
const createdAt =
|
||||||
|
metadata.createdAt && metadata.createdAtIv
|
||||||
|
? await decryptDate(metadata.createdAt, metadata.createdAtIv, dataKey)
|
||||||
|
: undefined;
|
||||||
|
const lastModifiedAt = await decryptDate(
|
||||||
|
metadata.lastModifiedAt,
|
||||||
|
metadata.lastModifiedAtIv,
|
||||||
|
dataKey,
|
||||||
|
);
|
||||||
|
|
||||||
|
await storeFileInfo({
|
||||||
|
id,
|
||||||
|
parentId: metadata.parent,
|
||||||
|
name,
|
||||||
|
contentType: metadata.contentType,
|
||||||
|
createdAt,
|
||||||
|
lastModifiedAt,
|
||||||
|
categoryIds: metadata.categories,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
dataKey,
|
||||||
|
dataKeyVersion: new Date(metadata.dekVersion),
|
||||||
|
contentType: metadata.contentType,
|
||||||
|
contentIv: metadata.contentIv,
|
||||||
|
name,
|
||||||
|
createdAt,
|
||||||
|
lastModifiedAt,
|
||||||
|
categoryIds: metadata.categories,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export type FileInfoStore = ReturnType<typeof getFileInfo>;
|
||||||
|
|
||||||
|
export const useFileUpload = (
|
||||||
|
parentId: DirectoryId,
|
||||||
|
masterKey: MasterKey,
|
||||||
|
hmacSecret: HmacSecret,
|
||||||
|
) => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
return createMutation<
|
||||||
|
{ fileId: number; fileBuffer: ArrayBuffer; thumbnailBuffer?: ArrayBuffer },
|
||||||
|
Error,
|
||||||
|
{ file: File; onDuplicate: () => Promise<boolean> },
|
||||||
|
{ tempId: number }
|
||||||
|
>({
|
||||||
|
mutationFn: async ({ file, onDuplicate }) => {
|
||||||
|
const res = await uploadFile(file, parentId, hmacSecret, masterKey, onDuplicate);
|
||||||
|
if (!res) throw new Error("Failed to upload file");
|
||||||
|
|
||||||
|
queryClient.setQueryData<FileInfo>(["file", res.fileId], {
|
||||||
|
id: res.fileId,
|
||||||
|
dataKey: res.fileDataKey,
|
||||||
|
dataKeyVersion: res.fileDataKeyVersion,
|
||||||
|
contentType: res.fileType,
|
||||||
|
contentIv: res.fileEncryptedIv,
|
||||||
|
name: file.name,
|
||||||
|
createdAt: res.fileCreatedAt,
|
||||||
|
lastModifiedAt: new Date(file.lastModified),
|
||||||
|
categoryIds: [],
|
||||||
|
});
|
||||||
|
await storeFileInfo({
|
||||||
|
id: res.fileId,
|
||||||
|
parentId,
|
||||||
|
name: file.name,
|
||||||
|
contentType: res.fileType,
|
||||||
|
createdAt: res.fileCreatedAt,
|
||||||
|
lastModifiedAt: new Date(file.lastModified),
|
||||||
|
categoryIds: [],
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
fileId: res.fileId,
|
||||||
|
fileBuffer: res.fileBuffer,
|
||||||
|
thumbnailBuffer: res.thumbnailBuffer,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
onSuccess: async ({ fileId }) => {
|
||||||
|
await queryClient.cancelQueries({ queryKey: ["directory", parentId] });
|
||||||
|
queryClient.setQueryData<DirectoryInfo>(["directory", parentId], (prevParentInfo) => {
|
||||||
|
if (!prevParentInfo) return undefined;
|
||||||
|
return {
|
||||||
|
...prevParentInfo,
|
||||||
|
fileIds: [...prevParentInfo.fileIds, fileId],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onSettled: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["directory", parentId] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFileRename = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
return createMutation<
|
||||||
|
void,
|
||||||
|
Error,
|
||||||
|
{
|
||||||
|
id: number;
|
||||||
|
dataKey: CryptoKey;
|
||||||
|
dataKeyVersion: Date;
|
||||||
|
newName: string;
|
||||||
|
},
|
||||||
|
{ oldName: string | undefined }
|
||||||
|
>({
|
||||||
|
mutationFn: async ({ id, dataKey, dataKeyVersion, newName }) => {
|
||||||
|
const newNameEncrypted = await encryptString(newName, dataKey);
|
||||||
|
const res = await callPostApi<FileRenameRequest>(`/api/file/${id}/rename`, {
|
||||||
|
dekVersion: dataKeyVersion.toISOString(),
|
||||||
|
name: newNameEncrypted.ciphertext,
|
||||||
|
nameIv: newNameEncrypted.iv,
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error("Failed to rename file");
|
||||||
|
|
||||||
|
await updateFileInfo(id, { name: newName });
|
||||||
|
},
|
||||||
|
onMutate: async ({ id, newName }) => {
|
||||||
|
await queryClient.cancelQueries({ queryKey: ["file", id] });
|
||||||
|
|
||||||
|
const prevInfo = queryClient.getQueryData<FileInfo>(["file", id]);
|
||||||
|
if (prevInfo) {
|
||||||
|
queryClient.setQueryData<FileInfo>(["file", id], {
|
||||||
|
...prevInfo,
|
||||||
|
name: newName,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { oldName: prevInfo?.name };
|
||||||
|
},
|
||||||
|
onError: (_error, { id }, context) => {
|
||||||
|
if (context?.oldName) {
|
||||||
|
queryClient.setQueryData<FileInfo>(["file", id], (prevInfo) => {
|
||||||
|
if (!prevInfo) return undefined;
|
||||||
|
return { ...prevInfo, name: context.oldName! };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSettled: (_data, _error, { id }) => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["file", id] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFileDeletion = (parentId: DirectoryId) => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
return createMutation<void, Error, { id: number }, {}>({
|
||||||
|
mutationFn: async ({ id }) => {
|
||||||
|
const res = await callPostApi(`/api/file/${id}/delete`);
|
||||||
|
if (!res.ok) throw new Error("Failed to delete file");
|
||||||
|
|
||||||
|
await deleteFileInfo(id);
|
||||||
|
},
|
||||||
|
onMutate: async ({ id }) => {
|
||||||
|
await queryClient.cancelQueries({ queryKey: ["directory", parentId] });
|
||||||
|
queryClient.setQueryData<DirectoryInfo>(["directory", parentId], (prevParentInfo) => {
|
||||||
|
if (!prevParentInfo) return undefined;
|
||||||
|
return {
|
||||||
|
...prevParentInfo,
|
||||||
|
fileIds: prevParentInfo.fileIds.filter((fileId) => fileId !== id),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
onError: (_error, { id }, context) => {
|
||||||
|
if (context) {
|
||||||
|
queryClient.setQueryData<DirectoryInfo>(["directory", parentId], (prevParentInfo) => {
|
||||||
|
if (!prevParentInfo) return undefined;
|
||||||
|
return {
|
||||||
|
...prevParentInfo,
|
||||||
|
fileIds: [...prevParentInfo.fileIds, id],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSettled: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["directory", parentId] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
2
src/lib/modules/filesystem2/index.ts
Normal file
2
src/lib/modules/filesystem2/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./directory";
|
||||||
|
export * from "./file";
|
||||||
@@ -5,12 +5,8 @@
|
|||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import { FullscreenDiv } from "$lib/components/atoms";
|
import { FullscreenDiv } from "$lib/components/atoms";
|
||||||
import { Categories, IconEntryButton, TopBar } from "$lib/components/molecules";
|
import { Categories, IconEntryButton, TopBar } from "$lib/components/molecules";
|
||||||
import {
|
import { getCategoryInfo, type CategoryInfo } from "$lib/modules/filesystem";
|
||||||
getFileInfo,
|
import { getFileInfo } from "$lib/modules/filesystem2";
|
||||||
getCategoryInfo,
|
|
||||||
type FileInfo,
|
|
||||||
type CategoryInfo,
|
|
||||||
} from "$lib/modules/filesystem";
|
|
||||||
import { captureVideoThumbnail } from "$lib/modules/thumbnail";
|
import { captureVideoThumbnail } from "$lib/modules/thumbnail";
|
||||||
import { fileDownloadStatusStore, isFileDownloading, masterKeyStore } from "$lib/stores";
|
import { fileDownloadStatusStore, isFileDownloading, masterKeyStore } from "$lib/stores";
|
||||||
import AddToCategoryBottomSheet from "./AddToCategoryBottomSheet.svelte";
|
import AddToCategoryBottomSheet from "./AddToCategoryBottomSheet.svelte";
|
||||||
@@ -28,7 +24,7 @@
|
|||||||
|
|
||||||
let { data } = $props();
|
let { data } = $props();
|
||||||
|
|
||||||
let info: Writable<FileInfo | null> | undefined = $state();
|
let info = $derived(getFileInfo(data.id, $masterKeyStore?.get(1)?.key!));
|
||||||
let categories: Writable<CategoryInfo | null>[] = $state([]);
|
let categories: Writable<CategoryInfo | null>[] = $state([]);
|
||||||
|
|
||||||
let isAddToCategoryBottomSheetOpen = $state(false);
|
let isAddToCategoryBottomSheetOpen = $state(false);
|
||||||
@@ -85,19 +81,19 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
info = getFileInfo(data.id, $masterKeyStore?.get(1)?.key!);
|
data.id;
|
||||||
isDownloadRequested = false;
|
isDownloadRequested = false;
|
||||||
viewerType = undefined;
|
viewerType = undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
categories =
|
categories =
|
||||||
$info?.categoryIds.map((id) => getCategoryInfo(id, $masterKeyStore?.get(1)?.key!)) ?? [];
|
$info.data?.categoryIds.map((id) => getCategoryInfo(id, $masterKeyStore?.get(1)?.key!)) ?? [];
|
||||||
});
|
});
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if ($info && $info.dataKey && $info.contentIv) {
|
if ($info.data?.dataKey && $info.data?.contentIv) {
|
||||||
const contentType = $info.contentType;
|
const contentType = $info.data.contentType;
|
||||||
if (contentType.startsWith("image")) {
|
if (contentType.startsWith("image")) {
|
||||||
viewerType = "image";
|
viewerType = "image";
|
||||||
} else if (contentType.startsWith("video")) {
|
} else if (contentType.startsWith("video")) {
|
||||||
@@ -107,21 +103,23 @@
|
|||||||
untrack(() => {
|
untrack(() => {
|
||||||
if (!downloadStatus && !isDownloadRequested) {
|
if (!downloadStatus && !isDownloadRequested) {
|
||||||
isDownloadRequested = true;
|
isDownloadRequested = true;
|
||||||
requestFileDownload(data.id, $info.contentIv!, $info.dataKey!).then(async (buffer) => {
|
requestFileDownload(data.id, $info.data.contentIv!, $info.data.dataKey!).then(
|
||||||
const blob = await updateViewer(buffer, contentType);
|
async (buffer) => {
|
||||||
if (!viewerType) {
|
const blob = await updateViewer(buffer, contentType);
|
||||||
FileSaver.saveAs(blob, $info.name);
|
if (!viewerType) {
|
||||||
}
|
FileSaver.saveAs(blob, $info.data.name);
|
||||||
});
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if ($info && $downloadStatus?.status === "decrypted") {
|
if ($info.status === "success" && $downloadStatus?.status === "decrypted") {
|
||||||
untrack(
|
untrack(
|
||||||
() => !isDownloadRequested && updateViewer($downloadStatus.result!, $info.contentType),
|
() => !isDownloadRequested && updateViewer($downloadStatus.result!, $info.data.contentType),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -133,11 +131,11 @@
|
|||||||
<title>파일</title>
|
<title>파일</title>
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<TopBar title={$info?.name} />
|
<TopBar title={$info.data?.name} />
|
||||||
<FullscreenDiv>
|
<FullscreenDiv>
|
||||||
<div class="space-y-4 pb-4">
|
<div class="space-y-4 pb-4">
|
||||||
<DownloadStatus status={downloadStatus} />
|
<DownloadStatus status={downloadStatus} />
|
||||||
{#if $info && viewerType}
|
{#if $info.status === "success" && viewerType}
|
||||||
<div class="flex w-full justify-center">
|
<div class="flex w-full justify-center">
|
||||||
{#snippet viewerLoading(message: string)}
|
{#snippet viewerLoading(message: string)}
|
||||||
<p class="text-gray-500">{message}</p>
|
<p class="text-gray-500">{message}</p>
|
||||||
@@ -145,7 +143,7 @@
|
|||||||
|
|
||||||
{#if viewerType === "image"}
|
{#if viewerType === "image"}
|
||||||
{#if fileBlobUrl}
|
{#if fileBlobUrl}
|
||||||
<img src={fileBlobUrl} alt={$info.name} onerror={convertHeicToJpeg} />
|
<img src={fileBlobUrl} alt={$info.data.name} onerror={convertHeicToJpeg} />
|
||||||
{:else}
|
{:else}
|
||||||
{@render viewerLoading("이미지를 불러오고 있어요.")}
|
{@render viewerLoading("이미지를 불러오고 있어요.")}
|
||||||
{/if}
|
{/if}
|
||||||
@@ -156,7 +154,7 @@
|
|||||||
<video bind:this={videoElement} src={fileBlobUrl} controls muted></video>
|
<video bind:this={videoElement} src={fileBlobUrl} controls muted></video>
|
||||||
<IconEntryButton
|
<IconEntryButton
|
||||||
icon={IconCamera}
|
icon={IconCamera}
|
||||||
onclick={() => updateThumbnail($info.dataKey!, $info.dataKeyVersion!)}
|
onclick={() => updateThumbnail($info.data.dataKey!, $info.data.dataKeyVersion!)}
|
||||||
class="w-full"
|
class="w-full"
|
||||||
>
|
>
|
||||||
이 장면을 썸네일로 설정하기
|
이 장면을 썸네일로 설정하기
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { get, type Writable } from "svelte/store";
|
import { get, type Writable } from "svelte/store";
|
||||||
import { getFileInfo, type FileInfo } from "$lib/modules/filesystem";
|
import { getFileInfo } from "$lib/modules/filesystem2";
|
||||||
import { formatNetworkSpeed } from "$lib/modules/util";
|
import { formatNetworkSpeed } from "$lib/modules/util";
|
||||||
import { masterKeyStore, type FileDownloadStatus } from "$lib/stores";
|
import { masterKeyStore, type FileDownloadStatus } from "$lib/stores";
|
||||||
|
|
||||||
@@ -17,14 +17,10 @@
|
|||||||
|
|
||||||
let { status }: Props = $props();
|
let { status }: Props = $props();
|
||||||
|
|
||||||
let fileInfo: Writable<FileInfo | null> | undefined = $state();
|
let fileInfo = $derived(getFileInfo(get(status).id, $masterKeyStore?.get(1)?.key!));
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
fileInfo = getFileInfo(get(status).id, $masterKeyStore?.get(1)?.key!);
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $fileInfo}
|
{#if $fileInfo.status === "success"}
|
||||||
<div class="flex h-14 items-center gap-x-4 p-2">
|
<div class="flex h-14 items-center gap-x-4 p-2">
|
||||||
<div class="flex-shrink-0 text-lg text-gray-600">
|
<div class="flex-shrink-0 text-lg text-gray-600">
|
||||||
{#if $status.status === "download-pending"}
|
{#if $status.status === "download-pending"}
|
||||||
@@ -42,8 +38,8 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<div class="flex-grow overflow-hidden">
|
<div class="flex-grow overflow-hidden">
|
||||||
<p title={$fileInfo.name} class="truncate font-medium">
|
<p title={$fileInfo.data.name} class="truncate font-medium">
|
||||||
{$fileInfo.name}
|
{$fileInfo.data.name}
|
||||||
</p>
|
</p>
|
||||||
<p class="text-xs text-gray-800">
|
<p class="text-xs text-gray-800">
|
||||||
{#if $status.status === "download-pending"}
|
{#if $status.status === "download-pending"}
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
import type { Writable } from "svelte/store";
|
|
||||||
import { FullscreenDiv } from "$lib/components/atoms";
|
import { FullscreenDiv } from "$lib/components/atoms";
|
||||||
import { TopBar } from "$lib/components/molecules";
|
import { TopBar } from "$lib/components/molecules";
|
||||||
import type { FileCacheIndex } from "$lib/indexedDB";
|
import type { FileCacheIndex } from "$lib/indexedDB";
|
||||||
import { getFileCacheIndex, deleteFileCache as doDeleteFileCache } from "$lib/modules/file";
|
import { getFileCacheIndex, deleteFileCache as doDeleteFileCache } from "$lib/modules/file";
|
||||||
import { getFileInfo, type FileInfo } from "$lib/modules/filesystem";
|
import { getFileInfo, type FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { formatFileSize } from "$lib/modules/util";
|
import { formatFileSize } from "$lib/modules/util";
|
||||||
import { masterKeyStore } from "$lib/stores";
|
import { masterKeyStore } from "$lib/stores";
|
||||||
import File from "./File.svelte";
|
import File from "./File.svelte";
|
||||||
|
|
||||||
interface FileCache {
|
interface FileCache {
|
||||||
index: FileCacheIndex;
|
index: FileCacheIndex;
|
||||||
fileInfo: Writable<FileInfo | null>;
|
fileInfo: FileInfoStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fileCache: FileCache[] | undefined = $state();
|
let fileCache: FileCache[] | undefined = $state();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { Writable } from "svelte/store";
|
|
||||||
import type { FileCacheIndex } from "$lib/indexedDB";
|
import type { FileCacheIndex } from "$lib/indexedDB";
|
||||||
import type { FileInfo } from "$lib/modules/filesystem";
|
import type { FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { formatDate, formatFileSize } from "$lib/modules/util";
|
import { formatDate, formatFileSize } from "$lib/modules/util";
|
||||||
|
|
||||||
import IconDraft from "~icons/material-symbols/draft";
|
import IconDraft from "~icons/material-symbols/draft";
|
||||||
@@ -10,7 +9,7 @@
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
index: FileCacheIndex;
|
index: FileCacheIndex;
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
onDeleteClick: (fileId: number) => void;
|
onDeleteClick: (fileId: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,8 +27,8 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<div class="flex-grow overflow-hidden">
|
<div class="flex-grow overflow-hidden">
|
||||||
{#if $info}
|
{#if $info.status === "success"}
|
||||||
<p title={$info.name} class="truncate font-medium">{$info.name}</p>
|
<p title={$info.data.name} class="truncate font-medium">{$info.data.name}</p>
|
||||||
{:else}
|
{:else}
|
||||||
<p class="font-medium">삭제된 파일</p>
|
<p class="font-medium">삭제된 파일</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import { BottomDiv, Button, FullscreenDiv } from "$lib/components/atoms";
|
import { BottomDiv, Button, FullscreenDiv } from "$lib/components/atoms";
|
||||||
import { IconEntryButton, TopBar } from "$lib/components/molecules";
|
import { IconEntryButton, TopBar } from "$lib/components/molecules";
|
||||||
import { deleteAllFileThumbnailCaches } from "$lib/modules/file";
|
import { deleteAllFileThumbnailCaches } from "$lib/modules/file";
|
||||||
import { getFileInfo } from "$lib/modules/filesystem";
|
import { getFileInfo } from "$lib/modules/filesystem2";
|
||||||
import { masterKeyStore } from "$lib/stores";
|
import { masterKeyStore } from "$lib/stores";
|
||||||
import File from "./File.svelte";
|
import File from "./File.svelte";
|
||||||
import {
|
import {
|
||||||
@@ -21,8 +21,8 @@
|
|||||||
const generateAllThumbnails = () => {
|
const generateAllThumbnails = () => {
|
||||||
persistentStates.files.forEach(({ info }) => {
|
persistentStates.files.forEach(({ info }) => {
|
||||||
const fileInfo = get(info);
|
const fileInfo = get(info);
|
||||||
if (fileInfo) {
|
if (fileInfo.data) {
|
||||||
requestThumbnailGeneration(fileInfo);
|
requestThumbnailGeneration(fileInfo.data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,14 +13,14 @@
|
|||||||
import type { Writable } from "svelte/store";
|
import type { Writable } from "svelte/store";
|
||||||
import { ActionEntryButton } from "$lib/components/atoms";
|
import { ActionEntryButton } from "$lib/components/atoms";
|
||||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||||
import type { FileInfo } from "$lib/modules/filesystem";
|
import type { FileInfo, FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { formatDateTime } from "$lib/modules/util";
|
import { formatDateTime } from "$lib/modules/util";
|
||||||
import type { GenerationStatus } from "./service.svelte";
|
import type { GenerationStatus } from "./service.svelte";
|
||||||
|
|
||||||
import IconCamera from "~icons/material-symbols/camera";
|
import IconCamera from "~icons/material-symbols/camera";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
onclick: (selectedFile: FileInfo) => void;
|
onclick: (selectedFile: FileInfo) => void;
|
||||||
onGenerateThumbnailClick: (selectedFile: FileInfo) => void;
|
onGenerateThumbnailClick: (selectedFile: FileInfo) => void;
|
||||||
generationStatus?: Writable<GenerationStatus>;
|
generationStatus?: Writable<GenerationStatus>;
|
||||||
@@ -29,18 +29,18 @@
|
|||||||
let { info, onclick, onGenerateThumbnailClick, generationStatus }: Props = $props();
|
let { info, onclick, onGenerateThumbnailClick, generationStatus }: Props = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $info}
|
{#if $info.status === "success"}
|
||||||
<ActionEntryButton
|
<ActionEntryButton
|
||||||
class="h-14"
|
class="h-14"
|
||||||
onclick={() => onclick($info)}
|
onclick={() => onclick($info.data)}
|
||||||
actionButtonIcon={!$generationStatus || $generationStatus === "error" ? IconCamera : undefined}
|
actionButtonIcon={!$generationStatus || $generationStatus === "error" ? IconCamera : undefined}
|
||||||
onActionButtonClick={() => onGenerateThumbnailClick($info)}
|
onActionButtonClick={() => onGenerateThumbnailClick($info.data)}
|
||||||
actionButtonClass="text-gray-800"
|
actionButtonClass="text-gray-800"
|
||||||
>
|
>
|
||||||
{@const subtext =
|
{@const subtext =
|
||||||
$generationStatus && $generationStatus !== "uploaded"
|
$generationStatus && $generationStatus !== "uploaded"
|
||||||
? subtexts[$generationStatus]
|
? subtexts[$generationStatus]
|
||||||
: formatDateTime($info.createdAt ?? $info.lastModifiedAt)}
|
: formatDateTime($info.data.createdAt ?? $info.data.lastModifiedAt)}
|
||||||
<DirectoryEntryLabel type="file" name={$info.name} {subtext} />
|
<DirectoryEntryLabel type="file" name={$info.data.name} {subtext} />
|
||||||
</ActionEntryButton>
|
</ActionEntryButton>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { limitFunction } from "p-limit";
|
|||||||
import { get, writable, type Writable } from "svelte/store";
|
import { get, writable, type Writable } from "svelte/store";
|
||||||
import { encryptData } from "$lib/modules/crypto";
|
import { encryptData } from "$lib/modules/crypto";
|
||||||
import { storeFileThumbnailCache } from "$lib/modules/file";
|
import { storeFileThumbnailCache } from "$lib/modules/file";
|
||||||
import type { FileInfo } from "$lib/modules/filesystem";
|
import type { FileInfo, FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { generateThumbnail as doGenerateThumbnail } from "$lib/modules/thumbnail";
|
import { generateThumbnail as doGenerateThumbnail } from "$lib/modules/thumbnail";
|
||||||
import { requestFileDownload, requestFileThumbnailUpload } from "$lib/services/file";
|
import { requestFileDownload, requestFileThumbnailUpload } from "$lib/services/file";
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ export type GenerationStatus =
|
|||||||
|
|
||||||
interface File {
|
interface File {
|
||||||
id: number;
|
id: number;
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
status?: Writable<GenerationStatus>;
|
status?: Writable<GenerationStatus>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,20 @@
|
|||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import { FloatingButton } from "$lib/components/atoms";
|
import { FloatingButton } from "$lib/components/atoms";
|
||||||
import { TopBar } from "$lib/components/molecules";
|
import { TopBar } from "$lib/components/molecules";
|
||||||
import { deleteFileCache, deleteFileThumbnailCache } from "$lib/modules/file";
|
import {
|
||||||
|
storeFileCache,
|
||||||
|
deleteFileCache,
|
||||||
|
storeFileThumbnailCache,
|
||||||
|
deleteFileThumbnailCache,
|
||||||
|
} from "$lib/modules/file";
|
||||||
import {
|
import {
|
||||||
getDirectoryInfo,
|
getDirectoryInfo,
|
||||||
useDirectoryCreation,
|
useDirectoryCreation,
|
||||||
useDirectoryRename,
|
useDirectoryRename,
|
||||||
useDirectoryDeletion,
|
useDirectoryDeletion,
|
||||||
|
useFileUpload,
|
||||||
|
useFileRename,
|
||||||
|
useFileDeletion,
|
||||||
} from "$lib/modules/filesystem2";
|
} from "$lib/modules/filesystem2";
|
||||||
import { masterKeyStore, hmacSecretStore } from "$lib/stores";
|
import { masterKeyStore, hmacSecretStore } from "$lib/stores";
|
||||||
import DirectoryCreateModal from "./DirectoryCreateModal.svelte";
|
import DirectoryCreateModal from "./DirectoryCreateModal.svelte";
|
||||||
@@ -20,13 +28,7 @@
|
|||||||
import EntryMenuBottomSheet from "./EntryMenuBottomSheet.svelte";
|
import EntryMenuBottomSheet from "./EntryMenuBottomSheet.svelte";
|
||||||
import EntryRenameModal from "./EntryRenameModal.svelte";
|
import EntryRenameModal from "./EntryRenameModal.svelte";
|
||||||
import UploadStatusCard from "./UploadStatusCard.svelte";
|
import UploadStatusCard from "./UploadStatusCard.svelte";
|
||||||
import {
|
import { createContext, requestHmacSecretDownload } from "./service.svelte";
|
||||||
createContext,
|
|
||||||
requestHmacSecretDownload,
|
|
||||||
requestFileUpload,
|
|
||||||
requestEntryRename,
|
|
||||||
requestEntryDeletion,
|
|
||||||
} from "./service.svelte";
|
|
||||||
|
|
||||||
import IconAdd from "~icons/material-symbols/add";
|
import IconAdd from "~icons/material-symbols/add";
|
||||||
|
|
||||||
@@ -37,6 +39,11 @@
|
|||||||
let requestDirectoryCreation = $derived(useDirectoryCreation(data.id, $masterKeyStore?.get(1)!));
|
let requestDirectoryCreation = $derived(useDirectoryCreation(data.id, $masterKeyStore?.get(1)!));
|
||||||
let requestDirectoryRename = useDirectoryRename();
|
let requestDirectoryRename = useDirectoryRename();
|
||||||
let requestDirectoryDeletion = $derived(useDirectoryDeletion(data.id));
|
let requestDirectoryDeletion = $derived(useDirectoryDeletion(data.id));
|
||||||
|
let requestFileUpload = $derived(
|
||||||
|
useFileUpload(data.id, $masterKeyStore?.get(1)!, $hmacSecretStore?.get(1)!),
|
||||||
|
);
|
||||||
|
let requestFileRename = $derived(useFileRename());
|
||||||
|
let requestFileDeletion = $derived(useFileDeletion(data.id));
|
||||||
|
|
||||||
let fileInput: HTMLInputElement | undefined = $state();
|
let fileInput: HTMLInputElement | undefined = $state();
|
||||||
let duplicatedFile: File | undefined = $state();
|
let duplicatedFile: File | undefined = $state();
|
||||||
@@ -55,21 +62,24 @@
|
|||||||
if (!files || files.length === 0) return;
|
if (!files || files.length === 0) return;
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
requestFileUpload(file, data.id, $hmacSecretStore?.get(1)!, $masterKeyStore?.get(1)!, () => {
|
$requestFileUpload
|
||||||
return new Promise((resolve) => {
|
.mutateAsync({
|
||||||
duplicatedFile = file;
|
file,
|
||||||
resolveForDuplicateFileModal = resolve;
|
onDuplicate: () => {
|
||||||
isDuplicateFileModalOpen = true;
|
return new Promise((resolve) => {
|
||||||
});
|
duplicatedFile = file;
|
||||||
})
|
resolveForDuplicateFileModal = resolve;
|
||||||
.then((res) => {
|
isDuplicateFileModalOpen = true;
|
||||||
if (!res) return;
|
});
|
||||||
// TODO: FIXME
|
},
|
||||||
// info = getDirectoryInfo(data.id, $masterKeyStore?.get(1)?.key!);
|
|
||||||
})
|
})
|
||||||
.catch((e: Error) => {
|
.then((res) => {
|
||||||
// TODO: FIXME
|
if (res) {
|
||||||
console.error(e);
|
storeFileCache(res.fileId, res.fileBuffer); // Intended
|
||||||
|
if (res.thumbnailBuffer) {
|
||||||
|
storeFileThumbnailCache(res.fileId, res.thumbnailBuffer); // Intended
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,11 +184,13 @@
|
|||||||
});
|
});
|
||||||
return true; // TODO
|
return true; // TODO
|
||||||
} else {
|
} else {
|
||||||
if (await requestEntryRename(context.selectedEntry!, newName)) {
|
$requestFileRename.mutate({
|
||||||
// info = getDirectoryInfo(data.id, $masterKeyStore?.get(1)?.key!); // TODO: FIXME
|
id: context.selectedEntry!.id,
|
||||||
return true;
|
dataKey: context.selectedEntry!.dataKey,
|
||||||
}
|
dataKeyVersion: context.selectedEntry!.dataKeyVersion,
|
||||||
return false;
|
newName,
|
||||||
|
});
|
||||||
|
return true; // TODO
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@@ -186,9 +198,7 @@
|
|||||||
bind:isOpen={isEntryDeleteModalOpen}
|
bind:isOpen={isEntryDeleteModalOpen}
|
||||||
onDeleteClick={async () => {
|
onDeleteClick={async () => {
|
||||||
if (context.selectedEntry!.type === "directory") {
|
if (context.selectedEntry!.type === "directory") {
|
||||||
const res = await $requestDirectoryDeletion.mutateAsync({
|
const res = await $requestDirectoryDeletion.mutateAsync({ id: context.selectedEntry!.id });
|
||||||
id: context.selectedEntry!.id,
|
|
||||||
});
|
|
||||||
if (!res) return false;
|
if (!res) return false;
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
res.deletedFiles.flatMap((fileId) => [
|
res.deletedFiles.flatMap((fileId) => [
|
||||||
@@ -198,11 +208,12 @@
|
|||||||
);
|
);
|
||||||
return true; // TODO
|
return true; // TODO
|
||||||
} else {
|
} else {
|
||||||
if (await requestEntryDeletion(context.selectedEntry!)) {
|
await $requestFileDeletion.mutateAsync({ id: context.selectedEntry!.id });
|
||||||
// info = getDirectoryInfo(data.id, $masterKeyStore?.get(1)?.key!); // TODO: FIXME
|
await Promise.all([
|
||||||
return true;
|
deleteFileCache(context.selectedEntry!.id),
|
||||||
}
|
deleteFileThumbnailCache(context.selectedEntry!.id),
|
||||||
return false;
|
]);
|
||||||
|
return true; // TODO
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { untrack } from "svelte";
|
import { untrack } from "svelte";
|
||||||
import { get, type Writable } from "svelte/store";
|
import { get, type Writable } from "svelte/store";
|
||||||
import { getFileInfo, type FileInfo } from "$lib/modules/filesystem";
|
|
||||||
import {
|
import {
|
||||||
getDirectoryInfo,
|
getDirectoryInfo,
|
||||||
|
getFileInfo,
|
||||||
type DirectoryInfo,
|
type DirectoryInfo,
|
||||||
type DirectoryInfoStore,
|
type DirectoryInfoStore,
|
||||||
|
type FileInfoStore,
|
||||||
} from "$lib/modules/filesystem2";
|
} from "$lib/modules/filesystem2";
|
||||||
import { SortBy, sortEntries } from "$lib/modules/util";
|
import { SortBy, sortEntries } from "$lib/modules/util";
|
||||||
import {
|
import {
|
||||||
@@ -37,7 +38,7 @@
|
|||||||
| {
|
| {
|
||||||
type: "file";
|
type: "file";
|
||||||
name?: string;
|
name?: string;
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
type: "uploading-file";
|
type: "uploading-file";
|
||||||
@@ -60,7 +61,7 @@
|
|||||||
const info = getFileInfo(id, $masterKeyStore?.get(1)?.key!);
|
const info = getFileInfo(id, $masterKeyStore?.get(1)?.key!);
|
||||||
return {
|
return {
|
||||||
type: "file",
|
type: "file",
|
||||||
name: get(info)?.name,
|
name: get(info).data?.name,
|
||||||
info,
|
info,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
@@ -93,13 +94,21 @@
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.concat(
|
.concat(
|
||||||
files.map((file) =>
|
files.map((file) => {
|
||||||
file.info.subscribe((value) => {
|
if (file.type === "file") {
|
||||||
if (file.name === value?.name) return;
|
return file.info.subscribe((value) => {
|
||||||
file.name = value?.name;
|
if (file.name === value.data?.name) return;
|
||||||
sort();
|
file.name = value.data?.name;
|
||||||
}),
|
sort();
|
||||||
),
|
});
|
||||||
|
} else {
|
||||||
|
return file.info.subscribe((value) => {
|
||||||
|
if (file.name === value.name) return;
|
||||||
|
file.name = value.name;
|
||||||
|
sort();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
return () => unsubscribes.forEach((unsubscribe) => unsubscribe());
|
return () => unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { Writable } from "svelte/store";
|
|
||||||
import { ActionEntryButton } from "$lib/components/atoms";
|
import { ActionEntryButton } from "$lib/components/atoms";
|
||||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||||
import type { FileInfo } from "$lib/modules/filesystem";
|
import type { FileInfo, FileInfoStore } from "$lib/modules/filesystem2";
|
||||||
import { formatDateTime } from "$lib/modules/util";
|
import { formatDateTime } from "$lib/modules/util";
|
||||||
import { requestFileThumbnailDownload } from "./service";
|
import { requestFileThumbnailDownload } from "./service";
|
||||||
import type { SelectedEntry } from "../service.svelte";
|
import type { SelectedEntry } from "../service.svelte";
|
||||||
@@ -10,7 +9,7 @@
|
|||||||
import IconMoreVert from "~icons/material-symbols/more-vert";
|
import IconMoreVert from "~icons/material-symbols/more-vert";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
info: Writable<FileInfo | null>;
|
info: FileInfoStore;
|
||||||
onclick: (selectedEntry: SelectedEntry) => void;
|
onclick: (selectedEntry: SelectedEntry) => void;
|
||||||
onOpenMenuClick: (selectedEntry: SelectedEntry) => void;
|
onOpenMenuClick: (selectedEntry: SelectedEntry) => void;
|
||||||
}
|
}
|
||||||
@@ -20,22 +19,22 @@
|
|||||||
let thumbnail: string | undefined = $state();
|
let thumbnail: string | undefined = $state();
|
||||||
|
|
||||||
const openFile = () => {
|
const openFile = () => {
|
||||||
const { id, dataKey, dataKeyVersion, name } = $info!;
|
const { id, dataKey, dataKeyVersion, name } = $info.data as FileInfo;
|
||||||
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
||||||
|
|
||||||
onclick({ type: "file", id, dataKey, dataKeyVersion, name });
|
onclick({ type: "file", id, dataKey, dataKeyVersion, name });
|
||||||
};
|
};
|
||||||
|
|
||||||
const openMenu = () => {
|
const openMenu = () => {
|
||||||
const { id, dataKey, dataKeyVersion, name } = $info!;
|
const { id, dataKey, dataKeyVersion, name } = $info.data as FileInfo;
|
||||||
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
if (!dataKey || !dataKeyVersion) return; // TODO: Error handling
|
||||||
|
|
||||||
onOpenMenuClick({ type: "file", id, dataKey, dataKeyVersion, name });
|
onOpenMenuClick({ type: "file", id, dataKey, dataKeyVersion, name });
|
||||||
};
|
};
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if ($info?.dataKey) {
|
if ($info.data?.dataKey) {
|
||||||
requestFileThumbnailDownload($info.id, $info.dataKey)
|
requestFileThumbnailDownload($info.data.id, $info.data.dataKey)
|
||||||
.then((thumbnailUrl) => {
|
.then((thumbnailUrl) => {
|
||||||
thumbnail = thumbnailUrl ?? undefined;
|
thumbnail = thumbnailUrl ?? undefined;
|
||||||
})
|
})
|
||||||
@@ -49,7 +48,7 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $info}
|
{#if $info.status === "success"}
|
||||||
<ActionEntryButton
|
<ActionEntryButton
|
||||||
class="h-14"
|
class="h-14"
|
||||||
onclick={openFile}
|
onclick={openFile}
|
||||||
@@ -59,8 +58,8 @@
|
|||||||
<DirectoryEntryLabel
|
<DirectoryEntryLabel
|
||||||
type="file"
|
type="file"
|
||||||
{thumbnail}
|
{thumbnail}
|
||||||
name={$info.name}
|
name={$info.data.name}
|
||||||
subtext={formatDateTime($info.createdAt ?? $info.lastModifiedAt)}
|
subtext={formatDateTime($info.data.createdAt ?? $info.data.lastModifiedAt)}
|
||||||
/>
|
/>
|
||||||
</ActionEntryButton>
|
</ActionEntryButton>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
Reference in New Issue
Block a user