mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-03-22 15:26:56 +09:00
Compare commits
9 Commits
7f68f6d580
...
v0.8.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b621d6e98 | ||
|
|
3906ec4371 | ||
|
|
90ac5ba4c3 | ||
|
|
dfffa004ac | ||
|
|
0cd55a413d | ||
|
|
361d966a59 | ||
|
|
aef43b8bfa | ||
|
|
7f128cccf6 | ||
|
|
a198e5f6dc |
@@ -12,3 +12,4 @@ USER_CLIENT_CHALLENGE_EXPIRES=
|
||||
SESSION_UPGRADE_CHALLENGE_EXPIRES=
|
||||
LIBRARY_PATH=
|
||||
THUMBNAILS_PATH=
|
||||
UPLOADS_PATH=
|
||||
|
||||
2
.github/workflows/docker.yaml
vendored
2
.github/workflows/docker.yaml
vendored
@@ -31,7 +31,7 @@ jobs:
|
||||
with:
|
||||
images: ghcr.io/${{ github.repository }}
|
||||
tags: |
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,value={{version}}
|
||||
type=raw,value=latest
|
||||
type=sha
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ services:
|
||||
volumes:
|
||||
- ./data/library:/app/data/library
|
||||
- ./data/thumbnails:/app/data/thumbnails
|
||||
- ./data/uploads:/app/data/uploads
|
||||
environment:
|
||||
# ArkVault
|
||||
- DATABASE_HOST=database
|
||||
@@ -20,6 +21,7 @@ services:
|
||||
- SESSION_UPGRADE_CHALLENGE_EXPIRES
|
||||
- LIBRARY_PATH=/app/data/library
|
||||
- THUMBNAILS_PATH=/app/data/thumbnails
|
||||
- UPLOADS_PATH=/app/data/uploads
|
||||
# SvelteKit
|
||||
- ADDRESS_HEADER=${TRUST_PROXY:+X-Forwarded-For}
|
||||
- XFF_DEPTH=${TRUST_PROXY:-}
|
||||
|
||||
59
package.json
59
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "arkvault",
|
||||
"private": true,
|
||||
"version": "0.9.1",
|
||||
"version": "0.8.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
@@ -16,56 +16,55 @@
|
||||
"db:migrate": "kysely migrate"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/compat": "^2.0.3",
|
||||
"@eslint/js": "^9.39.4",
|
||||
"@iconify-json/material-symbols": "^1.2.61",
|
||||
"@eslint/compat": "^2.0.1",
|
||||
"@eslint/js": "^9.39.2",
|
||||
"@iconify-json/material-symbols": "^1.2.51",
|
||||
"@noble/hashes": "^2.0.1",
|
||||
"@sveltejs/adapter-node": "^5.5.4",
|
||||
"@sveltejs/kit": "^2.53.4",
|
||||
"@sveltejs/adapter-node": "^5.4.0",
|
||||
"@sveltejs/kit": "^2.49.4",
|
||||
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
||||
"@tanstack/svelte-virtual": "^3.13.21",
|
||||
"@trpc/client": "^11.12.0",
|
||||
"@tanstack/svelte-virtual": "^3.13.18",
|
||||
"@trpc/client": "^11.8.1",
|
||||
"@types/file-saver": "^2.0.7",
|
||||
"@types/ms": "^0.7.34",
|
||||
"@types/node-schedule": "^2.1.8",
|
||||
"@types/pg": "^8.18.0",
|
||||
"autoprefixer": "^10.4.27",
|
||||
"axios": "^1.13.6",
|
||||
"dexie": "^4.3.0",
|
||||
"es-hangul": "^2.3.8",
|
||||
"eslint": "^9.39.4",
|
||||
"@types/pg": "^8.16.0",
|
||||
"autoprefixer": "^10.4.23",
|
||||
"axios": "^1.13.2",
|
||||
"dexie": "^4.2.1",
|
||||
"eslint": "^9.39.2",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"eslint-plugin-svelte": "^3.15.2",
|
||||
"eslint-plugin-svelte": "^3.14.0",
|
||||
"eslint-plugin-tailwindcss": "^3.18.2",
|
||||
"exifreader": "^4.36.2",
|
||||
"exifreader": "^4.35.0",
|
||||
"file-saver": "^2.0.5",
|
||||
"globals": "^17.4.0",
|
||||
"globals": "^17.0.0",
|
||||
"heic2any": "^0.0.4",
|
||||
"kysely-ctl": "^0.20.0",
|
||||
"lru-cache": "^11.2.6",
|
||||
"kysely-ctl": "^0.19.0",
|
||||
"lru-cache": "^11.2.4",
|
||||
"mime": "^4.1.0",
|
||||
"p-limit": "^7.3.0",
|
||||
"prettier": "^3.8.1",
|
||||
"prettier-plugin-svelte": "^3.5.1",
|
||||
"p-limit": "^7.2.0",
|
||||
"prettier": "^3.7.4",
|
||||
"prettier-plugin-svelte": "^3.4.1",
|
||||
"prettier-plugin-tailwindcss": "^0.7.2",
|
||||
"svelte": "^5.53.9",
|
||||
"svelte-check": "^4.4.5",
|
||||
"svelte": "^5.46.1",
|
||||
"svelte-check": "^4.3.5",
|
||||
"tailwindcss": "^3.4.19",
|
||||
"typescript": "^5.9.3",
|
||||
"typescript-eslint": "^8.57.0",
|
||||
"unplugin-icons": "^23.0.1",
|
||||
"typescript-eslint": "^8.52.0",
|
||||
"unplugin-icons": "^22.5.0",
|
||||
"vite": "^7.3.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@trpc/server": "^11.12.0",
|
||||
"@trpc/server": "^11.8.1",
|
||||
"argon2": "^0.44.0",
|
||||
"kysely": "^0.28.11",
|
||||
"kysely": "^0.28.9",
|
||||
"ms": "^2.1.3",
|
||||
"node-schedule": "^2.1.1",
|
||||
"pg": "^8.20.0",
|
||||
"pg": "^8.16.3",
|
||||
"superjson": "^2.2.6",
|
||||
"uuid": "^13.0.0",
|
||||
"zod": "^4.3.6"
|
||||
"zod": "^4.3.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^22.0.0",
|
||||
|
||||
1436
pnpm-lock.yaml
generated
1436
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,53 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from "svelte";
|
||||
import type { ClassValue } from "svelte/elements";
|
||||
|
||||
import IconClose from "~icons/material-symbols/close";
|
||||
|
||||
interface Props {
|
||||
children: Snippet;
|
||||
class?: ClassValue;
|
||||
onclick?: () => void;
|
||||
onRemoveClick?: () => void;
|
||||
removable?: boolean;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
let {
|
||||
children,
|
||||
class: className,
|
||||
onclick = () => (selected = !selected),
|
||||
onRemoveClick,
|
||||
removable = false,
|
||||
selected = $bindable(false),
|
||||
}: Props = $props();
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
||||
<div
|
||||
onclick={onclick && (() => setTimeout(onclick, 100))}
|
||||
class={[
|
||||
"inline-flex cursor-pointer items-center gap-x-1 rounded-lg px-3 py-1.5 text-sm font-medium transition active:scale-95",
|
||||
selected
|
||||
? "bg-primary-500 text-white active:bg-primary-400"
|
||||
: "bg-gray-100 text-gray-700 active:bg-gray-200",
|
||||
className,
|
||||
]}
|
||||
>
|
||||
<span>
|
||||
{@render children()}
|
||||
</span>
|
||||
{#if removable && selected}
|
||||
<button
|
||||
onclick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (onRemoveClick) {
|
||||
setTimeout(onRemoveClick, 100);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<IconClose />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -6,22 +6,13 @@
|
||||
interface Props {
|
||||
class?: ClassValue;
|
||||
count: number;
|
||||
estimateItemHeight: (index: number) => number;
|
||||
getItemKey?: (index: number) => string | number;
|
||||
item: Snippet<[index: number]>;
|
||||
itemHeight: (index: number) => number;
|
||||
itemGap?: number;
|
||||
placeholder?: Snippet;
|
||||
}
|
||||
|
||||
let {
|
||||
class: className,
|
||||
count,
|
||||
estimateItemHeight,
|
||||
getItemKey,
|
||||
item,
|
||||
itemGap,
|
||||
placeholder,
|
||||
}: Props = $props();
|
||||
let { class: className, count, item, itemHeight, itemGap, placeholder }: Props = $props();
|
||||
|
||||
let element: HTMLElement | undefined = $state();
|
||||
let scrollMargin = $state(0);
|
||||
@@ -29,9 +20,8 @@
|
||||
let virtualizer = $derived(
|
||||
createWindowVirtualizer({
|
||||
count,
|
||||
estimateSize: estimateItemHeight,
|
||||
estimateSize: itemHeight,
|
||||
gap: itemGap,
|
||||
getItemKey: getItemKey,
|
||||
scrollMargin,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
<style>
|
||||
#container:active:not(:has(#action-button:active)) {
|
||||
@apply bg-gray-100;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
import { getFileThumbnail } from "$lib/modules/file";
|
||||
import type { SummarizedFileInfo } from "$lib/modules/filesystem";
|
||||
|
||||
import IconFavorite from "~icons/material-symbols/favorite";
|
||||
|
||||
interface Props {
|
||||
info: SummarizedFileInfo;
|
||||
onclick?: (file: SummarizedFileInfo) => void;
|
||||
@@ -16,19 +14,11 @@
|
||||
|
||||
<button
|
||||
onclick={onclick && (() => setTimeout(() => onclick(info), 100))}
|
||||
class="relative aspect-square overflow-hidden rounded transition active:scale-95 active:brightness-90"
|
||||
class="aspect-square overflow-hidden rounded transition active:scale-95 active:brightness-90"
|
||||
>
|
||||
{#if $thumbnail}
|
||||
<img src={$thumbnail} alt={info.name} class="h-full w-full object-cover" />
|
||||
{:else}
|
||||
<div class="h-full w-full bg-gray-100"></div>
|
||||
{/if}
|
||||
{#if info.isFavorite}
|
||||
<div class={["absolute bottom-0.5 right-0.5"]}>
|
||||
<IconFavorite
|
||||
class="text-sm text-red-500"
|
||||
style="filter: drop-shadow(0 0 1px white) drop-shadow(0 0 1px white)"
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
</button>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
export { default as BottomSheet } from "./BottomSheet.svelte";
|
||||
export * from "./buttons";
|
||||
export { default as Chip } from "./Chip.svelte";
|
||||
export * from "./divs";
|
||||
export * from "./inputs";
|
||||
export { default as Modal } from "./Modal.svelte";
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
<style>
|
||||
input:focus,
|
||||
input:not(:placeholder-shown) {
|
||||
@apply border-primary-300;
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
class?: ClassValue;
|
||||
info: CategoryInfo;
|
||||
onSubCategoryClick: (subCategory: SelectedCategory) => void;
|
||||
onSubCategoryCreateClick?: () => void;
|
||||
onSubCategoryCreateClick: () => void;
|
||||
onSubCategoryMenuClick?: (category: SelectedCategory) => void;
|
||||
subCategoryCreatePosition?: "top" | "bottom" | "none";
|
||||
subCategoryCreatePosition?: "top" | "bottom";
|
||||
subCategoryMenuIcon?: Component<SvelteHTMLElements["svg"]>;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
onSubCategoryClick,
|
||||
onSubCategoryCreateClick,
|
||||
onSubCategoryMenuClick,
|
||||
subCategoryCreatePosition = "none",
|
||||
subCategoryCreatePosition = "bottom",
|
||||
subCategoryMenuIcon,
|
||||
}: Props = $props();
|
||||
</script>
|
||||
|
||||
@@ -8,11 +8,10 @@
|
||||
children?: Snippet;
|
||||
class?: ClassValue;
|
||||
onBackClick?: () => void;
|
||||
showBackButton?: boolean;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
let { children, class: className, onBackClick, showBackButton = true, title }: Props = $props();
|
||||
let { children, class: className, onBackClick, title }: Props = $props();
|
||||
</script>
|
||||
|
||||
<div
|
||||
@@ -21,16 +20,12 @@
|
||||
className,
|
||||
]}
|
||||
>
|
||||
<div class="w-[2.3rem] flex-shrink-0">
|
||||
{#if showBackButton}
|
||||
<button
|
||||
onclick={onBackClick ?? (() => history.back())}
|
||||
class="w-full rounded-full p-1 text-2xl active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconArrowBack />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
<button
|
||||
onclick={onBackClick || (() => history.back())}
|
||||
class="w-[2.3rem] flex-shrink-0 rounded-full p-1 active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconArrowBack class="text-2xl" />
|
||||
</button>
|
||||
{#if title}
|
||||
<p class="flex-grow truncate text-center text-lg font-semibold">{title}</p>
|
||||
{/if}
|
||||
|
||||
@@ -5,11 +5,9 @@
|
||||
import IconFolder from "~icons/material-symbols/folder";
|
||||
import IconDriveFolderUpload from "~icons/material-symbols/drive-folder-upload";
|
||||
import IconDraft from "~icons/material-symbols/draft";
|
||||
import IconFavorite from "~icons/material-symbols/favorite";
|
||||
|
||||
interface Props {
|
||||
class?: ClassValue;
|
||||
isFavorite?: boolean;
|
||||
name: string;
|
||||
subtext?: string;
|
||||
textClass?: ClassValue;
|
||||
@@ -19,7 +17,6 @@
|
||||
|
||||
let {
|
||||
class: className,
|
||||
isFavorite = false,
|
||||
name,
|
||||
subtext,
|
||||
textClass: textClassName,
|
||||
@@ -29,7 +26,7 @@
|
||||
</script>
|
||||
|
||||
{#snippet iconSnippet()}
|
||||
<div class="relative flex h-10 w-10 items-center justify-center text-xl">
|
||||
<div class="flex h-10 w-10 items-center justify-center text-xl">
|
||||
{#if thumbnail}
|
||||
<img src={thumbnail} alt={name} loading="lazy" class="aspect-square rounded object-cover" />
|
||||
{:else if type === "directory"}
|
||||
@@ -39,14 +36,6 @@
|
||||
{:else}
|
||||
<IconDraft class="text-blue-400" />
|
||||
{/if}
|
||||
{#if isFavorite}
|
||||
<div class={["absolute bottom-0 right-0", !thumbnail && "rounded-full bg-white p-0.5"]}>
|
||||
<IconFavorite
|
||||
class="text-xs text-red-500"
|
||||
style="filter: drop-shadow(0 0 1px white) drop-shadow(0 0 1px white)"
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/snippet}
|
||||
|
||||
|
||||
@@ -20,12 +20,7 @@ export const getFileCacheIndex = async () => {
|
||||
};
|
||||
|
||||
export const storeFileCacheIndex = async (fileCacheIndex: FileCacheIndex) => {
|
||||
await cacheIndex.fileCache.put({
|
||||
fileId: fileCacheIndex.fileId,
|
||||
cachedAt: fileCacheIndex.cachedAt,
|
||||
lastRetrievedAt: fileCacheIndex.lastRetrievedAt,
|
||||
size: fileCacheIndex.size,
|
||||
});
|
||||
await cacheIndex.fileCache.put(fileCacheIndex);
|
||||
};
|
||||
|
||||
export const deleteFileCacheIndex = async (fileId: number) => {
|
||||
|
||||
@@ -4,7 +4,6 @@ interface DirectoryInfo {
|
||||
id: number;
|
||||
parentId: DirectoryId;
|
||||
name: string;
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
interface FileInfo {
|
||||
@@ -15,7 +14,6 @@ interface FileInfo {
|
||||
createdAt?: Date;
|
||||
lastModifiedAt: Date;
|
||||
categoryIds?: number[];
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
interface CategoryInfo {
|
||||
@@ -48,23 +46,6 @@ filesystem
|
||||
});
|
||||
});
|
||||
|
||||
filesystem.version(4).upgrade(async (trx) => {
|
||||
await Promise.all([
|
||||
trx
|
||||
.table("directory")
|
||||
.toCollection()
|
||||
.modify((directory) => {
|
||||
directory.isFavorite = false;
|
||||
}),
|
||||
trx
|
||||
.table("file")
|
||||
.toCollection()
|
||||
.modify((file) => {
|
||||
file.isFavorite = false;
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
export const getDirectoryInfos = async (parentId: DirectoryId) => {
|
||||
return await filesystem.directory.where({ parentId }).toArray();
|
||||
};
|
||||
@@ -74,11 +55,7 @@ export const getDirectoryInfo = async (id: number) => {
|
||||
};
|
||||
|
||||
export const storeDirectoryInfo = async (directoryInfo: DirectoryInfo) => {
|
||||
await filesystem.directory.upsert(directoryInfo.id, {
|
||||
parentId: directoryInfo.parentId,
|
||||
name: directoryInfo.name,
|
||||
isFavorite: directoryInfo.isFavorite,
|
||||
});
|
||||
await filesystem.directory.upsert(directoryInfo.id, { ...directoryInfo });
|
||||
};
|
||||
|
||||
export const deleteDirectoryInfo = async (id: number) => {
|
||||
@@ -112,15 +89,7 @@ export const bulkGetFileInfos = async (ids: number[]) => {
|
||||
};
|
||||
|
||||
export const storeFileInfo = async (fileInfo: FileInfo) => {
|
||||
await filesystem.file.upsert(fileInfo.id, {
|
||||
parentId: fileInfo.parentId,
|
||||
name: fileInfo.name,
|
||||
contentType: fileInfo.contentType,
|
||||
createdAt: fileInfo.createdAt,
|
||||
lastModifiedAt: fileInfo.lastModifiedAt,
|
||||
categoryIds: fileInfo.categoryIds,
|
||||
isFavorite: fileInfo.isFavorite,
|
||||
});
|
||||
await filesystem.file.upsert(fileInfo.id, { ...fileInfo });
|
||||
};
|
||||
|
||||
export const deleteFileInfo = async (id: number) => {
|
||||
@@ -147,12 +116,7 @@ export const getCategoryInfo = async (id: number) => {
|
||||
};
|
||||
|
||||
export const storeCategoryInfo = async (categoryInfo: CategoryInfo) => {
|
||||
await filesystem.category.upsert(categoryInfo.id, {
|
||||
parentId: categoryInfo.parentId,
|
||||
name: categoryInfo.name,
|
||||
files: categoryInfo.files,
|
||||
isFileRecursive: categoryInfo.isFileRecursive,
|
||||
});
|
||||
await filesystem.category.upsert(categoryInfo.id, { ...categoryInfo });
|
||||
};
|
||||
|
||||
export const updateCategoryInfo = async (id: number, changes: { isFileRecursive?: boolean }) => {
|
||||
|
||||
@@ -10,7 +10,6 @@ import { Scheduler } from "$lib/utils";
|
||||
import { trpc } from "$trpc/client";
|
||||
|
||||
export interface FileUploadState {
|
||||
id: string;
|
||||
name: string;
|
||||
parentId: DirectoryId;
|
||||
status:
|
||||
@@ -168,7 +167,7 @@ const requestFileUpload = limitFunction(
|
||||
) => {
|
||||
state.status = "uploading";
|
||||
|
||||
const { encContentHash } = await uploadBlob(uploadId, file, dataKey, {
|
||||
await uploadBlob(uploadId, file, dataKey, {
|
||||
onProgress(s) {
|
||||
state.progress = s.progress;
|
||||
state.rate = s.rate;
|
||||
@@ -178,7 +177,6 @@ const requestFileUpload = limitFunction(
|
||||
const { file: fileId } = await trpc().upload.completeFileUpload.mutate({
|
||||
uploadId,
|
||||
contentHmac: fileSigned,
|
||||
encContentHash,
|
||||
});
|
||||
|
||||
if (thumbnailBuffer) {
|
||||
@@ -210,7 +208,6 @@ export const uploadFile = async (
|
||||
onDuplicate: () => Promise<boolean>,
|
||||
) => {
|
||||
uploadingFiles.push({
|
||||
id: crypto.randomUUID(),
|
||||
name: file.name,
|
||||
parentId,
|
||||
status: "queued",
|
||||
|
||||
@@ -21,7 +21,6 @@ const cache = new FilesystemCache<CategoryId, MaybeCategoryInfo>({
|
||||
name: fileInfo.name,
|
||||
createdAt: fileInfo.createdAt,
|
||||
lastModifiedAt: fileInfo.lastModifiedAt,
|
||||
isFavorite: fileInfo.isFavorite,
|
||||
isRecursive: file.isRecursive,
|
||||
}
|
||||
: undefined;
|
||||
@@ -65,7 +64,6 @@ const cache = new FilesystemCache<CategoryId, MaybeCategoryInfo>({
|
||||
id: file.id,
|
||||
parentId: file.parent,
|
||||
contentType: file.contentType,
|
||||
isFavorite: file.isFavorite,
|
||||
isRecursive: file.isRecursive,
|
||||
...(await decryptFileMetadata(file, masterKey)),
|
||||
})),
|
||||
|
||||
@@ -26,7 +26,6 @@ const cache = new FilesystemCache<DirectoryId, MaybeDirectoryInfo>({
|
||||
name: directory.name,
|
||||
subDirectories,
|
||||
files,
|
||||
isFavorite: directory.isFavorite,
|
||||
};
|
||||
}
|
||||
},
|
||||
@@ -39,7 +38,6 @@ const cache = new FilesystemCache<DirectoryId, MaybeDirectoryInfo>({
|
||||
directory.subDirectories.map(async (directory) => ({
|
||||
id: directory.id,
|
||||
parentId: id,
|
||||
isFavorite: directory.isFavorite,
|
||||
...(await decryptDirectoryMetadata(directory, masterKey)),
|
||||
})),
|
||||
),
|
||||
@@ -48,7 +46,6 @@ const cache = new FilesystemCache<DirectoryId, MaybeDirectoryInfo>({
|
||||
id: file.id,
|
||||
parentId: id,
|
||||
contentType: file.contentType,
|
||||
isFavorite: file.isFavorite,
|
||||
...(await decryptFileMetadata(file, masterKey)),
|
||||
})),
|
||||
),
|
||||
@@ -62,7 +59,6 @@ const cache = new FilesystemCache<DirectoryId, MaybeDirectoryInfo>({
|
||||
parentId: directory.metadata!.parent,
|
||||
subDirectories,
|
||||
files,
|
||||
isFavorite: directory.metadata!.isFavorite,
|
||||
...metadata!,
|
||||
}
|
||||
: { id, subDirectories, files },
|
||||
@@ -101,34 +97,6 @@ const storeToIndexedDB = (info: DirectoryInfo) => {
|
||||
return { ...info, exists: true as const };
|
||||
};
|
||||
|
||||
export const getDirectoryInfo = (
|
||||
id: DirectoryId,
|
||||
masterKey: CryptoKey,
|
||||
options?: {
|
||||
serverResponse?: {
|
||||
parent: DirectoryId;
|
||||
dek: string;
|
||||
dekVersion: Date;
|
||||
name: string;
|
||||
nameIv: string;
|
||||
isFavorite: boolean;
|
||||
};
|
||||
},
|
||||
) => {
|
||||
return cache.get(id, masterKey, {
|
||||
fetchFromServer:
|
||||
options?.serverResponse &&
|
||||
(async (cachedValue) => {
|
||||
const metadata = await decryptDirectoryMetadata(options!.serverResponse!, masterKey);
|
||||
return storeToIndexedDB({
|
||||
subDirectories: [],
|
||||
files: [],
|
||||
...cachedValue,
|
||||
id: id as number,
|
||||
parentId: options!.serverResponse!.parent,
|
||||
isFavorite: options!.serverResponse!.isFavorite,
|
||||
...metadata,
|
||||
});
|
||||
}),
|
||||
});
|
||||
export const getDirectoryInfo = (id: DirectoryId, masterKey: CryptoKey) => {
|
||||
return cache.get(id, masterKey);
|
||||
};
|
||||
|
||||
@@ -27,7 +27,6 @@ const cache = new FilesystemCache<number, MaybeFileInfo>({
|
||||
createdAt: file.createdAt,
|
||||
lastModifiedAt: file.lastModifiedAt,
|
||||
categories: categories?.filter((category) => !!category) ?? [],
|
||||
isFavorite: file.isFavorite,
|
||||
};
|
||||
}
|
||||
},
|
||||
@@ -56,7 +55,6 @@ const cache = new FilesystemCache<number, MaybeFileInfo>({
|
||||
createdAt: metadata.createdAt,
|
||||
lastModifiedAt: metadata.lastModifiedAt,
|
||||
categories,
|
||||
isFavorite: file.isFavorite,
|
||||
});
|
||||
} catch (e) {
|
||||
if (isTRPCClientError(e) && e.data?.code === "NOT_FOUND") {
|
||||
@@ -122,7 +120,6 @@ const cache = new FilesystemCache<number, MaybeFileInfo>({
|
||||
parentId: metadataRaw.parent,
|
||||
contentType: metadataRaw.contentType,
|
||||
categories,
|
||||
isFavorite: metadataRaw.isFavorite,
|
||||
...metadata,
|
||||
};
|
||||
}),
|
||||
@@ -171,41 +168,8 @@ const bulkStoreToIndexedDB = (infos: FileInfo[]) => {
|
||||
return infos.map((info) => [info.id, { ...info, exists: true }] as const);
|
||||
};
|
||||
|
||||
export const getFileInfo = (
|
||||
id: number,
|
||||
masterKey: CryptoKey,
|
||||
options?: {
|
||||
serverResponse?: {
|
||||
parent: DirectoryId;
|
||||
dek: string;
|
||||
dekVersion: Date;
|
||||
contentType: string;
|
||||
name: string;
|
||||
nameIv: string;
|
||||
createdAt?: string;
|
||||
createdAtIv?: string;
|
||||
lastModifiedAt: string;
|
||||
lastModifiedAtIv: string;
|
||||
isFavorite: boolean;
|
||||
};
|
||||
},
|
||||
) => {
|
||||
return cache.get(id, masterKey, {
|
||||
fetchFromServer:
|
||||
options?.serverResponse &&
|
||||
(async (cachedValue) => {
|
||||
const metadata = await decryptFileMetadata(options!.serverResponse!, masterKey);
|
||||
return storeToIndexedDB({
|
||||
categories: [],
|
||||
...cachedValue,
|
||||
id,
|
||||
parentId: options!.serverResponse!.parent,
|
||||
contentType: options!.serverResponse!.contentType,
|
||||
isFavorite: options!.serverResponse!.isFavorite,
|
||||
...metadata,
|
||||
});
|
||||
}),
|
||||
});
|
||||
export const getFileInfo = (id: number, masterKey: CryptoKey) => {
|
||||
return cache.get(id, masterKey);
|
||||
};
|
||||
|
||||
export const bulkGetFileInfo = (ids: number[], masterKey: CryptoKey) => {
|
||||
|
||||
@@ -16,11 +16,7 @@ export class FilesystemCache<K, V extends object> {
|
||||
|
||||
constructor(private readonly options: FilesystemCacheOptions<K, V>) {}
|
||||
|
||||
get(
|
||||
key: K,
|
||||
masterKey: CryptoKey,
|
||||
options?: { fetchFromServer?: (cachedValue: V | undefined) => Promise<V> },
|
||||
) {
|
||||
get(key: K, masterKey: CryptoKey) {
|
||||
return untrack(() => {
|
||||
let state = this.map.get(key);
|
||||
if (state?.promise) return state.value ?? state.promise;
|
||||
@@ -34,7 +30,7 @@ export class FilesystemCache<K, V extends object> {
|
||||
}
|
||||
|
||||
(state.value
|
||||
? Promise.resolve($state.snapshot(state.value) as V)
|
||||
? Promise.resolve(state.value)
|
||||
: this.options.fetchFromIndexedDB(key).then((loadedInfo) => {
|
||||
if (loadedInfo) {
|
||||
state.value = loadedInfo;
|
||||
@@ -43,11 +39,7 @@ export class FilesystemCache<K, V extends object> {
|
||||
return loadedInfo;
|
||||
})
|
||||
)
|
||||
.then(
|
||||
(cachedInfo) =>
|
||||
options?.fetchFromServer?.(cachedInfo) ??
|
||||
this.options.fetchFromServer(key, cachedInfo, masterKey),
|
||||
)
|
||||
.then((cachedInfo) => this.options.fetchFromServer(key, cachedInfo, masterKey))
|
||||
.then((loadedInfo) => {
|
||||
if (state.value) {
|
||||
Object.assign(state.value, loadedInfo);
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
export type DataKey = { key: CryptoKey; version: Date };
|
||||
type AllUndefined<T> = { [K in keyof T]?: undefined };
|
||||
|
||||
export interface LocalDirectoryInfo {
|
||||
interface LocalDirectoryInfo {
|
||||
id: number;
|
||||
parentId: DirectoryId;
|
||||
dataKey?: DataKey;
|
||||
name: string;
|
||||
subDirectories: SubDirectoryInfo[];
|
||||
files: SummarizedFileInfo[];
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
export interface RootDirectoryInfo {
|
||||
interface RootDirectoryInfo {
|
||||
id: "root";
|
||||
parentId?: undefined;
|
||||
dataKey?: undefined;
|
||||
name?: undefined;
|
||||
subDirectories: SubDirectoryInfo[];
|
||||
files: SummarizedFileInfo[];
|
||||
isFavorite?: undefined;
|
||||
}
|
||||
|
||||
export type DirectoryInfo = LocalDirectoryInfo | RootDirectoryInfo;
|
||||
@@ -37,7 +36,6 @@ export interface FileInfo {
|
||||
createdAt?: Date;
|
||||
lastModifiedAt: Date;
|
||||
categories: FileCategoryInfo[];
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
export type MaybeFileInfo =
|
||||
@@ -47,7 +45,7 @@ export type MaybeFileInfo =
|
||||
export type SummarizedFileInfo = Omit<FileInfo, "categories">;
|
||||
export type CategoryFileInfo = SummarizedFileInfo & { isRecursive: boolean };
|
||||
|
||||
export interface LocalCategoryInfo {
|
||||
interface LocalCategoryInfo {
|
||||
id: number;
|
||||
parentId: DirectoryId;
|
||||
dataKey?: DataKey;
|
||||
@@ -57,7 +55,7 @@ export interface LocalCategoryInfo {
|
||||
isFileRecursive: boolean;
|
||||
}
|
||||
|
||||
export interface RootCategoryInfo {
|
||||
interface RootCategoryInfo {
|
||||
id: "root";
|
||||
parentId?: undefined;
|
||||
dataKey?: undefined;
|
||||
|
||||
@@ -12,3 +12,11 @@ export const parseRangeHeader = (value: string | null) => {
|
||||
export const getContentRangeHeader = (range?: { start: number; end: number; total: number }) => {
|
||||
return range && { "Content-Range": `bytes ${range.start}-${range.end}/${range.total}` };
|
||||
};
|
||||
|
||||
export const parseContentDigestHeader = (value: string | null) => {
|
||||
if (!value) return undefined;
|
||||
|
||||
const firstDigest = value.split(",")[0]!.trim();
|
||||
const match = firstDigest.match(/^sha-256=:([A-Za-z0-9+/=]+):$/);
|
||||
return match?.[1];
|
||||
};
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { sha256 } from "@noble/hashes/sha2.js";
|
||||
import axios from "axios";
|
||||
import pLimit from "p-limit";
|
||||
import { ENCRYPTION_OVERHEAD, CHUNK_SIZE } from "$lib/constants";
|
||||
import { encodeToBase64, encryptChunk } from "$lib/modules/crypto";
|
||||
import { encryptChunk, digestMessage, encodeToBase64 } from "$lib/modules/crypto";
|
||||
import { BoundedQueue } from "$lib/utils";
|
||||
|
||||
interface UploadStats {
|
||||
@@ -13,6 +12,7 @@ interface UploadStats {
|
||||
interface EncryptedChunk {
|
||||
index: number;
|
||||
data: ArrayBuffer;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
const createSpeedMeter = (timeWindow = 3000, minInterval = 200, warmupPeriod = 500) => {
|
||||
@@ -68,18 +68,27 @@ const createSpeedMeter = (timeWindow = 3000, minInterval = 200, warmupPeriod = 5
|
||||
};
|
||||
};
|
||||
|
||||
const encryptChunkData = async (chunk: Blob, dataKey: CryptoKey): Promise<ArrayBuffer> => {
|
||||
return await encryptChunk(await chunk.arrayBuffer(), dataKey);
|
||||
const encryptChunkData = async (
|
||||
chunk: Blob,
|
||||
dataKey: CryptoKey,
|
||||
): Promise<{ data: ArrayBuffer; hash: string }> => {
|
||||
const encrypted = await encryptChunk(await chunk.arrayBuffer(), dataKey);
|
||||
const hash = encodeToBase64(await digestMessage(encrypted));
|
||||
return { data: encrypted, hash };
|
||||
};
|
||||
|
||||
const uploadEncryptedChunk = async (
|
||||
uploadId: string,
|
||||
chunkIndex: number,
|
||||
encrypted: ArrayBuffer,
|
||||
hash: string,
|
||||
onChunkProgress: (chunkIndex: number, loaded: number) => void,
|
||||
) => {
|
||||
await axios.post(`/api/upload/${uploadId}/chunks/${chunkIndex + 1}`, encrypted, {
|
||||
headers: { "Content-Type": "application/octet-stream" },
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Digest": `sha-256=:${hash}:`,
|
||||
},
|
||||
onUploadProgress(e) {
|
||||
onChunkProgress(chunkIndex, e.loaded ?? 0);
|
||||
},
|
||||
@@ -103,7 +112,6 @@ export const uploadBlob = async (
|
||||
|
||||
const uploadedByChunk = new Array<number>(totalChunks).fill(0);
|
||||
const speedMeter = createSpeedMeter(3000, 200);
|
||||
const hash = sha256.create();
|
||||
|
||||
const emit = () => {
|
||||
if (!onProgress) return;
|
||||
@@ -128,9 +136,8 @@ export const uploadBlob = async (
|
||||
try {
|
||||
for (let i = 0; i < totalChunks; i++) {
|
||||
const chunk = blob.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE);
|
||||
const data = await encryptChunkData(chunk, dataKey);
|
||||
hash.update(new Uint8Array(data));
|
||||
await queue.push({ index: i, data });
|
||||
const { data, hash } = await encryptChunkData(chunk, dataKey);
|
||||
await queue.push({ index: i, data, hash });
|
||||
}
|
||||
} catch (e) {
|
||||
encryptionError = e instanceof Error ? e : new Error(String(e));
|
||||
@@ -151,7 +158,7 @@ export const uploadBlob = async (
|
||||
|
||||
const task = limit(async () => {
|
||||
try {
|
||||
await uploadEncryptedChunk(uploadId, item.index, item.data, onChunkProgress);
|
||||
await uploadEncryptedChunk(uploadId, item.index, item.data, item.hash, onChunkProgress);
|
||||
} finally {
|
||||
// @ts-ignore
|
||||
item.data = null;
|
||||
@@ -173,5 +180,4 @@ export const uploadBlob = async (
|
||||
await Promise.all([encryptionProducer(), uploadConsumer()]);
|
||||
|
||||
onProgress?.({ progress: 1, rate: speedMeter() });
|
||||
return { encContentHash: encodeToBase64(hash.digest()) };
|
||||
};
|
||||
|
||||
@@ -1,278 +0,0 @@
|
||||
import type { Selectable } from "kysely";
|
||||
import { IntegrityError } from "./error";
|
||||
import db from "./kysely";
|
||||
import type { Ciphertext, DirectoryTable } from "./schema";
|
||||
|
||||
interface Directory {
|
||||
id: number;
|
||||
parentId: DirectoryId;
|
||||
userId: number;
|
||||
mekVersion: number;
|
||||
encDek: string;
|
||||
dekVersion: Date;
|
||||
encName: Ciphertext;
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
const toDirectory = (row: Selectable<DirectoryTable>): Directory => ({
|
||||
id: row.id,
|
||||
parentId: row.parent_id ?? "root",
|
||||
userId: row.user_id,
|
||||
mekVersion: row.master_encryption_key_version,
|
||||
encDek: row.encrypted_data_encryption_key,
|
||||
dekVersion: row.data_encryption_key_version,
|
||||
encName: row.encrypted_name,
|
||||
isFavorite: row.is_favorite,
|
||||
});
|
||||
|
||||
export const registerDirectory = async (params: Omit<Directory, "id" | "isFavorite">) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const mek = await trx
|
||||
.selectFrom("master_encryption_key")
|
||||
.select("version")
|
||||
.where("user_id", "=", params.userId)
|
||||
.where("state", "=", "active")
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (mek?.version !== params.mekVersion) {
|
||||
throw new IntegrityError("Inactive MEK version");
|
||||
}
|
||||
|
||||
const { directoryId } = await trx
|
||||
.insertInto("directory")
|
||||
.values({
|
||||
parent_id: params.parentId !== "root" ? params.parentId : null,
|
||||
user_id: params.userId,
|
||||
master_encryption_key_version: params.mekVersion,
|
||||
encrypted_data_encryption_key: params.encDek,
|
||||
data_encryption_key_version: params.dekVersion,
|
||||
encrypted_name: params.encName,
|
||||
})
|
||||
.returning("id as directoryId")
|
||||
.executeTakeFirstOrThrow();
|
||||
await trx
|
||||
.insertInto("directory_log")
|
||||
.values({
|
||||
directory_id: directoryId,
|
||||
timestamp: new Date(),
|
||||
action: "create",
|
||||
new_name: params.encName,
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const getAllDirectoriesByParent = async (userId: number, parentId: DirectoryId) => {
|
||||
const directories = await db
|
||||
.selectFrom("directory")
|
||||
.selectAll()
|
||||
.where("user_id", "=", userId)
|
||||
.$if(parentId === "root", (qb) => qb.where("parent_id", "is", null))
|
||||
.$if(parentId !== "root", (qb) => qb.where("parent_id", "=", parentId as number))
|
||||
.execute();
|
||||
return directories.map(toDirectory);
|
||||
};
|
||||
|
||||
export const getAllFavoriteDirectories = async (userId: number) => {
|
||||
const directories = await db
|
||||
.selectFrom("directory")
|
||||
.selectAll()
|
||||
.where("user_id", "=", userId)
|
||||
.where("is_favorite", "=", true)
|
||||
.execute();
|
||||
return directories.map(toDirectory);
|
||||
};
|
||||
|
||||
export const getDirectory = async (userId: number, directoryId: number) => {
|
||||
const directory = await db
|
||||
.selectFrom("directory")
|
||||
.selectAll()
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.executeTakeFirst();
|
||||
return directory ? toDirectory(directory) : null;
|
||||
};
|
||||
|
||||
export const searchDirectories = async (
|
||||
userId: number,
|
||||
filters: {
|
||||
parentId: DirectoryId;
|
||||
inFavorites: boolean;
|
||||
},
|
||||
) => {
|
||||
const directories = await db
|
||||
.withRecursive("directory_tree", (db) =>
|
||||
db
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.$if(filters.parentId === "root", (qb) => qb.where((eb) => eb.lit(false))) // directory_tree will be empty if parentId is "root"
|
||||
.$if(filters.parentId !== "root", (qb) => qb.where("id", "=", filters.parentId as number))
|
||||
.unionAll(
|
||||
db
|
||||
.selectFrom("directory as d")
|
||||
.innerJoin("directory_tree as dt", "d.parent_id", "dt.id")
|
||||
.select("d.id"),
|
||||
),
|
||||
)
|
||||
.withRecursive("favorite_directory_tree", (db) =>
|
||||
db
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.$if(!filters.inFavorites, (qb) => qb.where((eb) => eb.lit(false))) // favorite_directory_tree will be empty if inFavorites is false
|
||||
.$if(filters.inFavorites, (qb) => qb.where("is_favorite", "=", true))
|
||||
.unionAll((db) =>
|
||||
db
|
||||
.selectFrom("directory as d")
|
||||
.innerJoin("favorite_directory_tree as dt", "d.parent_id", "dt.id")
|
||||
.select("d.id"),
|
||||
),
|
||||
)
|
||||
.selectFrom("directory")
|
||||
.selectAll()
|
||||
.where("user_id", "=", userId)
|
||||
.$if(filters.parentId !== "root", (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb.exists(eb.selectFrom("directory_tree as dt").whereRef("dt.id", "=", "parent_id")),
|
||||
),
|
||||
)
|
||||
.$if(filters.inFavorites, (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb.exists(
|
||||
eb.selectFrom("favorite_directory_tree as dt").whereRef("dt.id", "=", "directory.id"),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
return directories.map(toDirectory);
|
||||
};
|
||||
|
||||
export const setDirectoryEncName = async (
|
||||
userId: number,
|
||||
directoryId: number,
|
||||
dekVersion: Date,
|
||||
encName: Ciphertext,
|
||||
) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const directory = await trx
|
||||
.selectFrom("directory")
|
||||
.select("data_encryption_key_version")
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (!directory) {
|
||||
throw new IntegrityError("Directory not found");
|
||||
} else if (directory.data_encryption_key_version.getTime() !== dekVersion.getTime()) {
|
||||
throw new IntegrityError("Invalid DEK version");
|
||||
}
|
||||
|
||||
await trx
|
||||
.updateTable("directory")
|
||||
.set({ encrypted_name: encName })
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
await trx
|
||||
.insertInto("directory_log")
|
||||
.values({
|
||||
directory_id: directoryId,
|
||||
timestamp: new Date(),
|
||||
action: "rename",
|
||||
new_name: encName,
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const setDirectoryFavorite = async (
|
||||
userId: number,
|
||||
directoryId: number,
|
||||
isFavorite: boolean,
|
||||
) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const directory = await trx
|
||||
.selectFrom("directory")
|
||||
.select("is_favorite")
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (!directory) {
|
||||
throw new IntegrityError("Directory not found");
|
||||
} else if (directory.is_favorite === isFavorite) {
|
||||
throw new IntegrityError(
|
||||
isFavorite ? "Directory already favorited" : "Directory not favorited",
|
||||
);
|
||||
}
|
||||
|
||||
await trx
|
||||
.updateTable("directory")
|
||||
.set({ is_favorite: isFavorite })
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
await trx
|
||||
.insertInto("directory_log")
|
||||
.values({
|
||||
directory_id: directoryId,
|
||||
timestamp: new Date(),
|
||||
action: isFavorite ? "add-to-favorites" : "remove-from-favorites",
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const unregisterDirectory = async (userId: number, directoryId: number) => {
|
||||
return await db
|
||||
.transaction()
|
||||
.setIsolationLevel("repeatable read") // TODO: Sufficient?
|
||||
.execute(async (trx) => {
|
||||
const unregisterFiles = async (parentId: number) => {
|
||||
const files = await trx
|
||||
.selectFrom("file")
|
||||
.leftJoin("thumbnail", "file.id", "thumbnail.file_id")
|
||||
.select(["file.id", "file.path", "thumbnail.path as thumbnailPath"])
|
||||
.where("file.parent_id", "=", parentId)
|
||||
.where("file.user_id", "=", userId)
|
||||
.forUpdate("file")
|
||||
.execute();
|
||||
await trx
|
||||
.deleteFrom("file")
|
||||
.where("parent_id", "=", parentId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
return files;
|
||||
};
|
||||
const unregisterDirectoryRecursively = async (
|
||||
directoryId: number,
|
||||
): Promise<{ id: number; path: string; thumbnailPath: string | null }[]> => {
|
||||
const files = await unregisterFiles(directoryId);
|
||||
const subDirectories = await trx
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("parent_id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
const subDirectoryFilePaths = await Promise.all(
|
||||
subDirectories.map(async ({ id }) => await unregisterDirectoryRecursively(id)),
|
||||
);
|
||||
|
||||
const deleteRes = await trx
|
||||
.deleteFrom("directory")
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.executeTakeFirst();
|
||||
if (deleteRes.numDeletedRows === 0n) {
|
||||
throw new IntegrityError("Directory not found");
|
||||
}
|
||||
return files.concat(...subDirectoryFilePaths);
|
||||
};
|
||||
return await unregisterDirectoryRecursively(directoryId);
|
||||
});
|
||||
};
|
||||
@@ -8,13 +8,10 @@ type IntegrityErrorMessages =
|
||||
| "User client already exists"
|
||||
// File
|
||||
| "Directory not found"
|
||||
| "Directory already favorited"
|
||||
| "Directory not favorited"
|
||||
| "File not found"
|
||||
| "File is not legacy"
|
||||
| "File not found in category"
|
||||
| "File already added to category"
|
||||
| "File already favorited"
|
||||
| "File not favorited"
|
||||
| "Invalid DEK version"
|
||||
// HSK
|
||||
| "HSK already registered"
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
import { sql, type Selectable } from "kysely";
|
||||
import { sql } from "kysely";
|
||||
import { jsonArrayFrom } from "kysely/helpers/postgres";
|
||||
import pg from "pg";
|
||||
import { IntegrityError } from "./error";
|
||||
import db from "./kysely";
|
||||
import type { Ciphertext, FileTable } from "./schema";
|
||||
import type { Ciphertext } from "./schema";
|
||||
|
||||
interface Directory {
|
||||
id: number;
|
||||
parentId: DirectoryId;
|
||||
userId: number;
|
||||
mekVersion: number;
|
||||
encDek: string;
|
||||
dekVersion: Date;
|
||||
encName: Ciphertext;
|
||||
}
|
||||
|
||||
interface File {
|
||||
id: number;
|
||||
@@ -21,7 +31,6 @@ interface File {
|
||||
encName: Ciphertext;
|
||||
encCreatedAt: Ciphertext | null;
|
||||
encLastModifiedAt: Ciphertext;
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
interface FileCategory {
|
||||
@@ -33,26 +42,175 @@ interface FileCategory {
|
||||
encName: Ciphertext;
|
||||
}
|
||||
|
||||
const toFile = (row: Selectable<FileTable>): File => ({
|
||||
id: row.id,
|
||||
parentId: row.parent_id ?? "root",
|
||||
userId: row.user_id,
|
||||
path: row.path,
|
||||
mekVersion: row.master_encryption_key_version,
|
||||
encDek: row.encrypted_data_encryption_key,
|
||||
dekVersion: row.data_encryption_key_version,
|
||||
hskVersion: row.hmac_secret_key_version,
|
||||
contentHmac: row.content_hmac,
|
||||
contentType: row.content_type,
|
||||
encContentIv: row.encrypted_content_iv,
|
||||
encContentHash: row.encrypted_content_hash,
|
||||
encName: row.encrypted_name,
|
||||
encCreatedAt: row.encrypted_created_at,
|
||||
encLastModifiedAt: row.encrypted_last_modified_at,
|
||||
isFavorite: row.is_favorite,
|
||||
});
|
||||
export const registerDirectory = async (params: Omit<Directory, "id">) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const mek = await trx
|
||||
.selectFrom("master_encryption_key")
|
||||
.select("version")
|
||||
.where("user_id", "=", params.userId)
|
||||
.where("state", "=", "active")
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (mek?.version !== params.mekVersion) {
|
||||
throw new IntegrityError("Inactive MEK version");
|
||||
}
|
||||
|
||||
export const registerFile = async (trx: typeof db, params: Omit<File, "id" | "isFavorite">) => {
|
||||
const { directoryId } = await trx
|
||||
.insertInto("directory")
|
||||
.values({
|
||||
parent_id: params.parentId !== "root" ? params.parentId : null,
|
||||
user_id: params.userId,
|
||||
master_encryption_key_version: params.mekVersion,
|
||||
encrypted_data_encryption_key: params.encDek,
|
||||
data_encryption_key_version: params.dekVersion,
|
||||
encrypted_name: params.encName,
|
||||
})
|
||||
.returning("id as directoryId")
|
||||
.executeTakeFirstOrThrow();
|
||||
await trx
|
||||
.insertInto("directory_log")
|
||||
.values({
|
||||
directory_id: directoryId,
|
||||
timestamp: new Date(),
|
||||
action: "create",
|
||||
new_name: params.encName,
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const getAllDirectoriesByParent = async (userId: number, parentId: DirectoryId) => {
|
||||
let query = db.selectFrom("directory").selectAll().where("user_id", "=", userId);
|
||||
query =
|
||||
parentId === "root"
|
||||
? query.where("parent_id", "is", null)
|
||||
: query.where("parent_id", "=", parentId);
|
||||
const directories = await query.execute();
|
||||
return directories.map(
|
||||
(directory) =>
|
||||
({
|
||||
id: directory.id,
|
||||
parentId: directory.parent_id ?? "root",
|
||||
userId: directory.user_id,
|
||||
mekVersion: directory.master_encryption_key_version,
|
||||
encDek: directory.encrypted_data_encryption_key,
|
||||
dekVersion: directory.data_encryption_key_version,
|
||||
encName: directory.encrypted_name,
|
||||
}) satisfies Directory,
|
||||
);
|
||||
};
|
||||
|
||||
export const getDirectory = async (userId: number, directoryId: number) => {
|
||||
const directory = await db
|
||||
.selectFrom("directory")
|
||||
.selectAll()
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.executeTakeFirst();
|
||||
return directory
|
||||
? ({
|
||||
id: directory.id,
|
||||
parentId: directory.parent_id ?? "root",
|
||||
userId: directory.user_id,
|
||||
mekVersion: directory.master_encryption_key_version,
|
||||
encDek: directory.encrypted_data_encryption_key,
|
||||
dekVersion: directory.data_encryption_key_version,
|
||||
encName: directory.encrypted_name,
|
||||
} satisfies Directory)
|
||||
: null;
|
||||
};
|
||||
|
||||
export const setDirectoryEncName = async (
|
||||
userId: number,
|
||||
directoryId: number,
|
||||
dekVersion: Date,
|
||||
encName: Ciphertext,
|
||||
) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const directory = await trx
|
||||
.selectFrom("directory")
|
||||
.select("data_encryption_key_version")
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (!directory) {
|
||||
throw new IntegrityError("Directory not found");
|
||||
} else if (directory.data_encryption_key_version.getTime() !== dekVersion.getTime()) {
|
||||
throw new IntegrityError("Invalid DEK version");
|
||||
}
|
||||
|
||||
await trx
|
||||
.updateTable("directory")
|
||||
.set({ encrypted_name: encName })
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
await trx
|
||||
.insertInto("directory_log")
|
||||
.values({
|
||||
directory_id: directoryId,
|
||||
timestamp: new Date(),
|
||||
action: "rename",
|
||||
new_name: encName,
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const unregisterDirectory = async (userId: number, directoryId: number) => {
|
||||
return await db
|
||||
.transaction()
|
||||
.setIsolationLevel("repeatable read") // TODO: Sufficient?
|
||||
.execute(async (trx) => {
|
||||
const unregisterFiles = async (parentId: number) => {
|
||||
const files = await trx
|
||||
.selectFrom("file")
|
||||
.leftJoin("thumbnail", "file.id", "thumbnail.file_id")
|
||||
.select(["file.id", "file.path", "thumbnail.path as thumbnailPath"])
|
||||
.where("file.parent_id", "=", parentId)
|
||||
.where("file.user_id", "=", userId)
|
||||
.forUpdate("file")
|
||||
.execute();
|
||||
await trx
|
||||
.deleteFrom("file")
|
||||
.where("parent_id", "=", parentId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
return files;
|
||||
};
|
||||
const unregisterDirectoryRecursively = async (
|
||||
directoryId: number,
|
||||
): Promise<{ id: number; path: string; thumbnailPath: string | null }[]> => {
|
||||
const files = await unregisterFiles(directoryId);
|
||||
const subDirectories = await trx
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("parent_id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
const subDirectoryFilePaths = await Promise.all(
|
||||
subDirectories.map(async ({ id }) => await unregisterDirectoryRecursively(id)),
|
||||
);
|
||||
|
||||
const deleteRes = await trx
|
||||
.deleteFrom("directory")
|
||||
.where("id", "=", directoryId)
|
||||
.where("user_id", "=", userId)
|
||||
.executeTakeFirst();
|
||||
if (deleteRes.numDeletedRows === 0n) {
|
||||
throw new IntegrityError("Directory not found");
|
||||
}
|
||||
return files.concat(...subDirectoryFilePaths);
|
||||
};
|
||||
return await unregisterDirectoryRecursively(directoryId);
|
||||
});
|
||||
};
|
||||
|
||||
export const registerFile = async (trx: typeof db, params: Omit<File, "id">) => {
|
||||
if ((params.hskVersion && !params.contentHmac) || (!params.hskVersion && params.contentHmac)) {
|
||||
throw new Error("Invalid arguments");
|
||||
}
|
||||
@@ -90,14 +248,32 @@ export const registerFile = async (trx: typeof db, params: Omit<File, "id" | "is
|
||||
};
|
||||
|
||||
export const getAllFilesByParent = async (userId: number, parentId: DirectoryId) => {
|
||||
const files = await db
|
||||
.selectFrom("file")
|
||||
.selectAll()
|
||||
.where("user_id", "=", userId)
|
||||
.$if(parentId === "root", (qb) => qb.where("parent_id", "is", null))
|
||||
.$if(parentId !== "root", (qb) => qb.where("parent_id", "=", parentId as number))
|
||||
.execute();
|
||||
return files.map(toFile);
|
||||
let query = db.selectFrom("file").selectAll().where("user_id", "=", userId);
|
||||
query =
|
||||
parentId === "root"
|
||||
? query.where("parent_id", "is", null)
|
||||
: query.where("parent_id", "=", parentId);
|
||||
const files = await query.execute();
|
||||
return files.map(
|
||||
(file) =>
|
||||
({
|
||||
id: file.id,
|
||||
parentId: file.parent_id ?? "root",
|
||||
userId: file.user_id,
|
||||
path: file.path,
|
||||
mekVersion: file.master_encryption_key_version,
|
||||
encDek: file.encrypted_data_encryption_key,
|
||||
dekVersion: file.data_encryption_key_version,
|
||||
hskVersion: file.hmac_secret_key_version,
|
||||
contentHmac: file.content_hmac,
|
||||
contentType: file.content_type,
|
||||
encContentIv: file.encrypted_content_iv,
|
||||
encContentHash: file.encrypted_content_hash,
|
||||
encName: file.encrypted_name,
|
||||
encCreatedAt: file.encrypted_created_at,
|
||||
encLastModifiedAt: file.encrypted_last_modified_at,
|
||||
}) satisfies File,
|
||||
);
|
||||
};
|
||||
|
||||
export const getAllFilesByCategory = async (
|
||||
@@ -130,10 +306,27 @@ export const getAllFilesByCategory = async (
|
||||
.orderBy("file_id")
|
||||
.orderBy("depth")
|
||||
.execute();
|
||||
return files.map((file) => ({
|
||||
...toFile(file),
|
||||
isRecursive: file.depth > 0,
|
||||
}));
|
||||
return files.map(
|
||||
(file) =>
|
||||
({
|
||||
id: file.file_id,
|
||||
parentId: file.parent_id ?? "root",
|
||||
userId: file.user_id,
|
||||
path: file.path,
|
||||
mekVersion: file.master_encryption_key_version,
|
||||
encDek: file.encrypted_data_encryption_key,
|
||||
dekVersion: file.data_encryption_key_version,
|
||||
hskVersion: file.hmac_secret_key_version,
|
||||
contentHmac: file.content_hmac,
|
||||
contentType: file.content_type,
|
||||
encContentIv: file.encrypted_content_iv,
|
||||
encContentHash: file.encrypted_content_hash,
|
||||
encName: file.encrypted_name,
|
||||
encCreatedAt: file.encrypted_created_at,
|
||||
encLastModifiedAt: file.encrypted_last_modified_at,
|
||||
isRecursive: file.depth > 0,
|
||||
}) satisfies File & { isRecursive: boolean },
|
||||
);
|
||||
};
|
||||
|
||||
export const getAllFileIds = async (userId: number) => {
|
||||
@@ -141,28 +334,14 @@ export const getAllFileIds = async (userId: number) => {
|
||||
return files.map(({ id }) => id);
|
||||
};
|
||||
|
||||
export const getFilesWithoutThumbnail = async (userId: number, limit: number = 100) => {
|
||||
export const getLegacyFileIds = async (userId: number) => {
|
||||
const files = await db
|
||||
.selectFrom("file")
|
||||
.selectAll()
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.where((eb) =>
|
||||
eb.or([eb("content_type", "like", "image/%"), eb("content_type", "like", "video/%")]),
|
||||
)
|
||||
.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom("thumbnail")
|
||||
.select("thumbnail.id")
|
||||
.whereRef("thumbnail.file_id", "=", "file.id")
|
||||
.limit(1),
|
||||
),
|
||||
),
|
||||
)
|
||||
.limit(limit)
|
||||
.where("encrypted_content_iv", "is not", null)
|
||||
.execute();
|
||||
return files.map(toFile);
|
||||
return files.map(({ id }) => id);
|
||||
};
|
||||
|
||||
export const getAllFileIdsByContentHmac = async (
|
||||
@@ -188,7 +367,25 @@ export const getFile = async (userId: number, fileId: number) => {
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.executeTakeFirst();
|
||||
return file ? toFile(file) : null;
|
||||
return file
|
||||
? ({
|
||||
id: file.id,
|
||||
parentId: file.parent_id ?? "root",
|
||||
userId: file.user_id,
|
||||
path: file.path,
|
||||
mekVersion: file.master_encryption_key_version,
|
||||
encDek: file.encrypted_data_encryption_key,
|
||||
dekVersion: file.data_encryption_key_version,
|
||||
hskVersion: file.hmac_secret_key_version,
|
||||
contentHmac: file.content_hmac,
|
||||
contentType: file.content_type,
|
||||
encContentIv: file.encrypted_content_iv,
|
||||
encContentHash: file.encrypted_content_hash,
|
||||
encName: file.encrypted_name,
|
||||
encCreatedAt: file.encrypted_created_at,
|
||||
encLastModifiedAt: file.encrypted_last_modified_at,
|
||||
} satisfies File)
|
||||
: null;
|
||||
};
|
||||
|
||||
export const getFilesWithCategories = async (userId: number, fileIds: number[]) => {
|
||||
@@ -207,144 +404,34 @@ export const getFilesWithCategories = async (userId: number, fileIds: number[])
|
||||
.where("id", "=", (eb) => eb.fn.any(eb.val(fileIds)))
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
return files.map((file) => ({
|
||||
...toFile(file),
|
||||
categories: file.categories.map(
|
||||
(category) =>
|
||||
({
|
||||
return files.map(
|
||||
(file) =>
|
||||
({
|
||||
id: file.id,
|
||||
parentId: file.parent_id ?? "root",
|
||||
userId: file.user_id,
|
||||
path: file.path,
|
||||
mekVersion: file.master_encryption_key_version,
|
||||
encDek: file.encrypted_data_encryption_key,
|
||||
dekVersion: file.data_encryption_key_version,
|
||||
hskVersion: file.hmac_secret_key_version,
|
||||
contentHmac: file.content_hmac,
|
||||
contentType: file.content_type,
|
||||
encContentIv: file.encrypted_content_iv,
|
||||
encContentHash: file.encrypted_content_hash,
|
||||
encName: file.encrypted_name,
|
||||
encCreatedAt: file.encrypted_created_at,
|
||||
encLastModifiedAt: file.encrypted_last_modified_at,
|
||||
categories: file.categories.map((category) => ({
|
||||
id: category.id,
|
||||
parentId: category.parent_id ?? "root",
|
||||
mekVersion: category.master_encryption_key_version,
|
||||
encDek: category.encrypted_data_encryption_key,
|
||||
dekVersion: new Date(category.data_encryption_key_version),
|
||||
encName: category.encrypted_name,
|
||||
}) satisfies FileCategory,
|
||||
),
|
||||
}));
|
||||
};
|
||||
|
||||
export const getAllFavoriteFiles = async (userId: number) => {
|
||||
const files = await db
|
||||
.selectFrom("file")
|
||||
.selectAll()
|
||||
.where("user_id", "=", userId)
|
||||
.where("is_favorite", "=", true)
|
||||
.execute();
|
||||
return files.map(toFile);
|
||||
};
|
||||
|
||||
export const searchFiles = async (
|
||||
userId: number,
|
||||
filters: {
|
||||
parentId: DirectoryId;
|
||||
inFavorites: boolean;
|
||||
includeCategoryIds: number[];
|
||||
excludeCategoryIds: number[];
|
||||
},
|
||||
) => {
|
||||
const baseQuery = db
|
||||
.withRecursive("directory_tree", (db) =>
|
||||
db
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.$if(filters.parentId === "root", (qb) => qb.where((eb) => eb.lit(false))) // directory_tree will be empty if parentId is "root"
|
||||
.$if(filters.parentId !== "root", (qb) => qb.where("id", "=", filters.parentId as number))
|
||||
.unionAll(
|
||||
db
|
||||
.selectFrom("directory as d")
|
||||
.innerJoin("directory_tree as dt", "d.parent_id", "dt.id")
|
||||
.select("d.id"),
|
||||
),
|
||||
)
|
||||
.withRecursive("favorite_directory_tree", (db) =>
|
||||
db
|
||||
.selectFrom("directory")
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.$if(!filters.inFavorites, (qb) => qb.where((eb) => eb.lit(false))) // favorite_directory_tree will be empty if inFavorites is false
|
||||
.$if(filters.inFavorites, (qb) => qb.where("is_favorite", "=", true))
|
||||
.unionAll((db) =>
|
||||
db
|
||||
.selectFrom("directory as d")
|
||||
.innerJoin("favorite_directory_tree as dt", "d.parent_id", "dt.id")
|
||||
.select("d.id"),
|
||||
),
|
||||
)
|
||||
.withRecursive("include_category_tree", (db) =>
|
||||
db
|
||||
.selectFrom("category")
|
||||
.select(["id", "id as root_id"])
|
||||
.where("id", "=", (eb) => eb.fn.any(eb.val(filters.includeCategoryIds)))
|
||||
.where("user_id", "=", userId)
|
||||
.unionAll(
|
||||
db
|
||||
.selectFrom("category as c")
|
||||
.innerJoin("include_category_tree as ct", "c.parent_id", "ct.id")
|
||||
.select(["c.id", "ct.root_id"]),
|
||||
),
|
||||
)
|
||||
.withRecursive("exclude_category_tree", (db) =>
|
||||
db
|
||||
.selectFrom("category")
|
||||
.select("id")
|
||||
.where("id", "=", (eb) => eb.fn.any(eb.val(filters.excludeCategoryIds)))
|
||||
.where("user_id", "=", userId)
|
||||
.unionAll((db) =>
|
||||
db
|
||||
.selectFrom("category as c")
|
||||
.innerJoin("exclude_category_tree as ct", "c.parent_id", "ct.id")
|
||||
.select("c.id"),
|
||||
),
|
||||
)
|
||||
.selectFrom("file")
|
||||
.selectAll("file")
|
||||
.where("user_id", "=", userId)
|
||||
.$if(filters.parentId !== "root", (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb.exists(eb.selectFrom("directory_tree as dt").whereRef("dt.id", "=", "file.parent_id")),
|
||||
),
|
||||
)
|
||||
.$if(filters.inFavorites, (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb.or([
|
||||
eb("is_favorite", "=", true),
|
||||
eb.exists(
|
||||
eb.selectFrom("favorite_directory_tree as dt").whereRef("dt.id", "=", "file.parent_id"),
|
||||
),
|
||||
]),
|
||||
),
|
||||
)
|
||||
.$if(filters.excludeCategoryIds.length > 0, (qb) =>
|
||||
qb.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom("file_category")
|
||||
.innerJoin("exclude_category_tree", "category_id", "exclude_category_tree.id")
|
||||
.whereRef("file_id", "=", "file.id"),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
const files =
|
||||
filters.includeCategoryIds.length > 0
|
||||
? await baseQuery
|
||||
.innerJoin("file_category", "file.id", "file_category.file_id")
|
||||
.innerJoin(
|
||||
"include_category_tree",
|
||||
"file_category.category_id",
|
||||
"include_category_tree.id",
|
||||
)
|
||||
.groupBy("file.id")
|
||||
.having(
|
||||
(eb) => eb.fn.count("include_category_tree.root_id").distinct(),
|
||||
"=",
|
||||
filters.includeCategoryIds.length,
|
||||
)
|
||||
.execute()
|
||||
: await baseQuery.execute();
|
||||
return files.map(toFile);
|
||||
})),
|
||||
}) satisfies File & { categories: FileCategory[] },
|
||||
);
|
||||
};
|
||||
|
||||
export const setFileEncName = async (
|
||||
@@ -405,6 +492,51 @@ export const unregisterFile = async (userId: number, fileId: number) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const migrateFileContent = async (
|
||||
trx: typeof db,
|
||||
userId: number,
|
||||
fileId: number,
|
||||
newPath: string,
|
||||
dekVersion: Date,
|
||||
encContentHash: string,
|
||||
) => {
|
||||
const file = await trx
|
||||
.selectFrom("file")
|
||||
.select(["path", "data_encryption_key_version", "encrypted_content_iv"])
|
||||
.where("id", "=", fileId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (!file) {
|
||||
throw new IntegrityError("File not found");
|
||||
} else if (file.data_encryption_key_version.getTime() !== dekVersion.getTime()) {
|
||||
throw new IntegrityError("Invalid DEK version");
|
||||
} else if (!file.encrypted_content_iv) {
|
||||
throw new IntegrityError("File is not legacy");
|
||||
}
|
||||
|
||||
await trx
|
||||
.updateTable("file")
|
||||
.set({
|
||||
path: newPath,
|
||||
encrypted_content_iv: null,
|
||||
encrypted_content_hash: encContentHash,
|
||||
})
|
||||
.where("id", "=", fileId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
await trx
|
||||
.insertInto("file_log")
|
||||
.values({
|
||||
file_id: fileId,
|
||||
timestamp: new Date(),
|
||||
action: "migrate",
|
||||
})
|
||||
.execute();
|
||||
return { oldPath: file.path };
|
||||
};
|
||||
|
||||
export const addFileToCategory = async (fileId: number, categoryId: number) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
try {
|
||||
@@ -472,36 +604,3 @@ export const removeFileFromCategory = async (fileId: number, categoryId: number)
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const setFileFavorite = async (userId: number, fileId: number, isFavorite: boolean) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const file = await trx
|
||||
.selectFrom("file")
|
||||
.select("is_favorite")
|
||||
.where("id", "=", fileId)
|
||||
.where("user_id", "=", userId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
if (!file) {
|
||||
throw new IntegrityError("File not found");
|
||||
} else if (file.is_favorite === isFavorite) {
|
||||
throw new IntegrityError(isFavorite ? "File already favorited" : "File not favorited");
|
||||
}
|
||||
|
||||
await trx
|
||||
.updateTable("file")
|
||||
.set({ is_favorite: isFavorite })
|
||||
.where("id", "=", fileId)
|
||||
.where("user_id", "=", userId)
|
||||
.execute();
|
||||
await trx
|
||||
.insertInto("file_log")
|
||||
.values({
|
||||
file_id: fileId,
|
||||
timestamp: new Date(),
|
||||
action: isFavorite ? "add-to-favorites" : "remove-from-favorites",
|
||||
})
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
export * as CategoryRepo from "./category";
|
||||
export * as ClientRepo from "./client";
|
||||
export * as DirectoryRepo from "./directory";
|
||||
export * as FileRepo from "./file";
|
||||
export * as HskRepo from "./hsk";
|
||||
export * as MediaRepo from "./media";
|
||||
|
||||
@@ -83,3 +83,27 @@ export const getFileThumbnail = async (userId: number, fileId: number) => {
|
||||
} satisfies FileThumbnail)
|
||||
: null;
|
||||
};
|
||||
|
||||
export const getMissingFileThumbnails = async (userId: number, limit: number = 100) => {
|
||||
const files = await db
|
||||
.selectFrom("file")
|
||||
.select("id")
|
||||
.where("user_id", "=", userId)
|
||||
.where((eb) =>
|
||||
eb.or([eb("content_type", "like", "image/%"), eb("content_type", "like", "video/%")]),
|
||||
)
|
||||
.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom("thumbnail")
|
||||
.select("thumbnail.id")
|
||||
.whereRef("thumbnail.file_id", "=", "file.id")
|
||||
.limit(1),
|
||||
),
|
||||
),
|
||||
)
|
||||
.limit(limit)
|
||||
.execute();
|
||||
return files.map(({ id }) => id);
|
||||
};
|
||||
|
||||
@@ -71,7 +71,7 @@ export const getAllValidClientMeks = async (userId: number, clientId: number) =>
|
||||
.selectAll()
|
||||
.where("client_master_encryption_key.user_id", "=", userId)
|
||||
.where("client_master_encryption_key.client_id", "=", clientId)
|
||||
.where("state", "in", ["active", "retired"])
|
||||
.where((eb) => eb.or([eb("state", "=", "active"), eb("state", "=", "retired")]))
|
||||
.execute();
|
||||
return clientMeks.map(
|
||||
({ user_id, client_id, version, state, encrypted_key, encrypted_key_signature }) =>
|
||||
|
||||
@@ -135,7 +135,7 @@ export const up = async (db: Kysely<any>) => {
|
||||
)
|
||||
.execute();
|
||||
|
||||
// directory.ts
|
||||
// file.ts
|
||||
await db.schema
|
||||
.createTable("directory")
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().generatedAlwaysAsIdentity())
|
||||
@@ -162,8 +162,6 @@ export const up = async (db: Kysely<any>) => {
|
||||
.addColumn("action", "text", (col) => col.notNull())
|
||||
.addColumn("new_name", "json")
|
||||
.execute();
|
||||
|
||||
// file.ts
|
||||
await db.schema
|
||||
.createTable("file")
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().generatedAlwaysAsIdentity())
|
||||
|
||||
@@ -53,7 +53,9 @@ export const up = async (db: Kysely<any>) => {
|
||||
export const down = async (db: Kysely<any>) => {
|
||||
await db
|
||||
.deleteFrom("file_log")
|
||||
.where("action", "in", ["add-to-category", "remove-from-category"])
|
||||
.where((eb) =>
|
||||
eb.or([eb("action", "=", "add-to-category"), eb("action", "=", "remove-from-category")]),
|
||||
)
|
||||
.execute();
|
||||
|
||||
await db.schema.dropTable("file_category").execute();
|
||||
|
||||
@@ -62,8 +62,6 @@ export const up = async (db: Kysely<any>) => {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const down = async (db: Kysely<any>) => {
|
||||
await db.deleteFrom("file_log").where("action", "=", "migrate").execute();
|
||||
|
||||
await db.schema.dropTable("upload_session").execute();
|
||||
await db.schema
|
||||
.alterTable("thumbnail")
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Kysely } from "kysely";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const up = async (db: Kysely<any>) => {
|
||||
// directory.ts
|
||||
await db.schema
|
||||
.alterTable("directory")
|
||||
.addColumn("is_favorite", "boolean", (col) => col.notNull().defaultTo(false))
|
||||
.execute();
|
||||
|
||||
// file.ts
|
||||
await db.schema
|
||||
.alterTable("file")
|
||||
.addColumn("is_favorite", "boolean", (col) => col.notNull().defaultTo(false))
|
||||
.execute();
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const down = async (db: Kysely<any>) => {
|
||||
await db
|
||||
.deleteFrom("file_log")
|
||||
.where("action", "in", ["add-to-favorites", "remove-from-favorites"])
|
||||
.execute();
|
||||
await db
|
||||
.deleteFrom("directory_log")
|
||||
.where("action", "in", ["add-to-favorites", "remove-from-favorites"])
|
||||
.execute();
|
||||
|
||||
await db.schema.alterTable("file").dropColumn("is_favorite").execute();
|
||||
await db.schema.alterTable("directory").dropColumn("is_favorite").execute();
|
||||
};
|
||||
@@ -2,12 +2,10 @@ import * as Initial1737357000 from "./1737357000-Initial";
|
||||
import * as AddFileCategory1737422340 from "./1737422340-AddFileCategory";
|
||||
import * as AddThumbnail1738409340 from "./1738409340-AddThumbnail";
|
||||
import * as AddChunkedUpload1768062380 from "./1768062380-AddChunkedUpload";
|
||||
import * as AddFavorites1768643000 from "./1768643000-AddFavorites";
|
||||
|
||||
export default {
|
||||
"1737357000-Initial": Initial1737357000,
|
||||
"1737422340-AddFileCategory": AddFileCategory1737422340,
|
||||
"1738409340-AddThumbnail": AddThumbnail1738409340,
|
||||
"1768062380-AddChunkedUpload": AddChunkedUpload1768062380,
|
||||
"1768643000-AddFavorites": AddFavorites1768643000,
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Generated } from "kysely";
|
||||
import type { Ciphertext } from "./utils";
|
||||
|
||||
export interface CategoryTable {
|
||||
interface CategoryTable {
|
||||
id: Generated<number>;
|
||||
parent_id: number | null;
|
||||
user_id: number;
|
||||
@@ -11,7 +11,7 @@ export interface CategoryTable {
|
||||
encrypted_name: Ciphertext;
|
||||
}
|
||||
|
||||
export interface CategoryLogTable {
|
||||
interface CategoryLogTable {
|
||||
id: Generated<number>;
|
||||
category_id: number;
|
||||
timestamp: Date;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ColumnType, Generated } from "kysely";
|
||||
|
||||
export interface ClientTable {
|
||||
interface ClientTable {
|
||||
id: Generated<number>;
|
||||
encryption_public_key: string; // Base64
|
||||
signature_public_key: string; // Base64
|
||||
@@ -8,13 +8,13 @@ export interface ClientTable {
|
||||
|
||||
export type UserClientState = "challenging" | "pending" | "active";
|
||||
|
||||
export interface UserClientTable {
|
||||
interface UserClientTable {
|
||||
user_id: number;
|
||||
client_id: number;
|
||||
state: ColumnType<UserClientState, UserClientState | undefined>;
|
||||
}
|
||||
|
||||
export interface UserClientChallengeTable {
|
||||
interface UserClientChallengeTable {
|
||||
id: Generated<number>;
|
||||
user_id: number;
|
||||
client_id: number;
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import type { ColumnType, Generated } from "kysely";
|
||||
import type { Ciphertext } from "./utils";
|
||||
|
||||
export interface DirectoryTable {
|
||||
id: Generated<number>;
|
||||
parent_id: number | null;
|
||||
user_id: number;
|
||||
master_encryption_key_version: number;
|
||||
encrypted_data_encryption_key: string; // Base64
|
||||
data_encryption_key_version: Date;
|
||||
encrypted_name: Ciphertext;
|
||||
is_favorite: Generated<boolean>;
|
||||
}
|
||||
|
||||
export interface DirectoryLogTable {
|
||||
id: Generated<number>;
|
||||
directory_id: number;
|
||||
timestamp: ColumnType<Date, Date, never>;
|
||||
action: "create" | "rename" | "add-to-favorites" | "remove-from-favorites";
|
||||
new_name: Ciphertext | null;
|
||||
}
|
||||
|
||||
declare module "./index" {
|
||||
interface Database {
|
||||
directory: DirectoryTable;
|
||||
directory_log: DirectoryLogTable;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,25 @@
|
||||
import type { ColumnType, Generated } from "kysely";
|
||||
import type { Ciphertext } from "./utils";
|
||||
|
||||
export interface FileTable {
|
||||
interface DirectoryTable {
|
||||
id: Generated<number>;
|
||||
parent_id: number | null;
|
||||
user_id: number;
|
||||
master_encryption_key_version: number;
|
||||
encrypted_data_encryption_key: string; // Base64
|
||||
data_encryption_key_version: Date;
|
||||
encrypted_name: Ciphertext;
|
||||
}
|
||||
|
||||
interface DirectoryLogTable {
|
||||
id: Generated<number>;
|
||||
directory_id: number;
|
||||
timestamp: ColumnType<Date, Date, never>;
|
||||
action: "create" | "rename";
|
||||
new_name: Ciphertext | null;
|
||||
}
|
||||
|
||||
interface FileTable {
|
||||
id: Generated<number>;
|
||||
parent_id: number | null;
|
||||
user_id: number;
|
||||
@@ -17,32 +35,26 @@ export interface FileTable {
|
||||
encrypted_name: Ciphertext;
|
||||
encrypted_created_at: Ciphertext | null;
|
||||
encrypted_last_modified_at: Ciphertext;
|
||||
is_favorite: Generated<boolean>;
|
||||
}
|
||||
|
||||
export interface FileLogTable {
|
||||
interface FileLogTable {
|
||||
id: Generated<number>;
|
||||
file_id: number;
|
||||
timestamp: ColumnType<Date, Date, never>;
|
||||
action:
|
||||
| "create"
|
||||
| "rename"
|
||||
| "migrate"
|
||||
| "add-to-category"
|
||||
| "remove-from-category"
|
||||
| "add-to-favorites"
|
||||
| "remove-from-favorites";
|
||||
action: "create" | "rename" | "migrate" | "add-to-category" | "remove-from-category";
|
||||
new_name: Ciphertext | null;
|
||||
category_id: number | null;
|
||||
}
|
||||
|
||||
export interface FileCategoryTable {
|
||||
interface FileCategoryTable {
|
||||
file_id: number;
|
||||
category_id: number;
|
||||
}
|
||||
|
||||
declare module "./index" {
|
||||
interface Database {
|
||||
directory: DirectoryTable;
|
||||
directory_log: DirectoryLogTable;
|
||||
file: FileTable;
|
||||
file_log: FileLogTable;
|
||||
file_category: FileCategoryTable;
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { ColumnType, Generated } from "kysely";
|
||||
|
||||
export type HskState = "active";
|
||||
|
||||
export interface HskTable {
|
||||
interface HskTable {
|
||||
user_id: number;
|
||||
version: number;
|
||||
state: HskState;
|
||||
@@ -10,7 +10,7 @@ export interface HskTable {
|
||||
encrypted_key: string; // Base64
|
||||
}
|
||||
|
||||
export interface HskLogTable {
|
||||
interface HskLogTable {
|
||||
id: Generated<number>;
|
||||
user_id: number;
|
||||
hmac_secret_key_version: number;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from "./category";
|
||||
export * from "./client";
|
||||
export * from "./directory";
|
||||
export * from "./file";
|
||||
export * from "./hsk";
|
||||
export * from "./media";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Generated } from "kysely";
|
||||
|
||||
export interface ThumbnailTable {
|
||||
interface ThumbnailTable {
|
||||
id: Generated<number>;
|
||||
directory_id: number | null;
|
||||
file_id: number | null;
|
||||
|
||||
@@ -2,13 +2,13 @@ import type { ColumnType, Generated } from "kysely";
|
||||
|
||||
export type MekState = "active" | "retired" | "dead";
|
||||
|
||||
export interface MekTable {
|
||||
interface MekTable {
|
||||
user_id: number;
|
||||
version: number;
|
||||
state: MekState;
|
||||
}
|
||||
|
||||
export interface MekLogTable {
|
||||
interface MekLogTable {
|
||||
id: Generated<number>;
|
||||
user_id: number;
|
||||
master_encryption_key_version: number;
|
||||
@@ -17,7 +17,7 @@ export interface MekLogTable {
|
||||
action_by: number | null;
|
||||
}
|
||||
|
||||
export interface ClientMekTable {
|
||||
interface ClientMekTable {
|
||||
user_id: number;
|
||||
client_id: number;
|
||||
version: number;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ColumnType, Generated } from "kysely";
|
||||
|
||||
export interface SessionTable {
|
||||
interface SessionTable {
|
||||
id: string;
|
||||
user_id: number;
|
||||
client_id: number | null;
|
||||
@@ -10,7 +10,7 @@ export interface SessionTable {
|
||||
last_used_by_agent: string | null;
|
||||
}
|
||||
|
||||
export interface SessionUpgradeChallengeTable {
|
||||
interface SessionUpgradeChallengeTable {
|
||||
id: Generated<number>;
|
||||
session_id: string;
|
||||
client_id: number;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Generated } from "kysely";
|
||||
import type { Ciphertext } from "./utils";
|
||||
|
||||
export interface UploadSessionTable {
|
||||
interface UploadSessionTable {
|
||||
id: string;
|
||||
type: "file" | "thumbnail";
|
||||
type: "file" | "thumbnail" | "migration";
|
||||
user_id: number;
|
||||
path: string;
|
||||
bitmap: Buffer;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Generated } from "kysely";
|
||||
|
||||
export interface UserTable {
|
||||
interface UserTable {
|
||||
id: Generated<number>;
|
||||
email: string;
|
||||
nickname: string;
|
||||
|
||||
@@ -26,8 +26,8 @@ interface FileUploadSession extends BaseUploadSession {
|
||||
encLastModifiedAt: Ciphertext;
|
||||
}
|
||||
|
||||
interface ThumbnailUploadSession extends BaseUploadSession {
|
||||
type: "thumbnail";
|
||||
interface ThumbnailOrMigrationUploadSession extends BaseUploadSession {
|
||||
type: "thumbnail" | "migration";
|
||||
fileId: number;
|
||||
dekVersion: Date;
|
||||
}
|
||||
@@ -86,8 +86,8 @@ export const createFileUploadSession = async (
|
||||
});
|
||||
};
|
||||
|
||||
export const createThumbnailUploadSession = async (
|
||||
params: Omit<ThumbnailUploadSession, "bitmap" | "uploadedChunks">,
|
||||
export const createThumbnailOrMigrationUploadSession = async (
|
||||
params: Omit<ThumbnailOrMigrationUploadSession, "bitmap" | "uploadedChunks">,
|
||||
) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const file = await trx
|
||||
@@ -164,7 +164,7 @@ export const getUploadSession = async (sessionId: string, userId: number) => {
|
||||
expiresAt: session.expires_at,
|
||||
fileId: session.file_id!,
|
||||
dekVersion: session.data_encryption_key_version!,
|
||||
} satisfies ThumbnailUploadSession;
|
||||
} satisfies ThumbnailOrMigrationUploadSession;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,4 +26,5 @@ export default {
|
||||
},
|
||||
libraryPath: env.LIBRARY_PATH || "library",
|
||||
thumbnailsPath: env.THUMBNAILS_PATH || "thumbnails",
|
||||
uploadsPath: env.UPLOADS_PATH || "uploads",
|
||||
};
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { unlink } from "fs/promises";
|
||||
import { rm, unlink } from "fs/promises";
|
||||
|
||||
export const safeRecursiveRm = async (path: string | null | undefined) => {
|
||||
if (path) {
|
||||
await rm(path, { recursive: true }).catch(console.error);
|
||||
}
|
||||
};
|
||||
|
||||
export const safeUnlink = async (path: string | null | undefined) => {
|
||||
if (path) {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { error } from "@sveltejs/kit";
|
||||
import { open } from "fs/promises";
|
||||
import { createHash } from "crypto";
|
||||
import { createWriteStream } from "fs";
|
||||
import { Readable } from "stream";
|
||||
import { ENCRYPTION_OVERHEAD, ENCRYPTED_CHUNK_SIZE } from "$lib/constants";
|
||||
import { UploadRepo } from "$lib/server/db";
|
||||
import { safeUnlink } from "$lib/server/modules/filesystem";
|
||||
import { safeRecursiveRm, safeUnlink } from "$lib/server/modules/filesystem";
|
||||
|
||||
const chunkLocks = new Set<string>();
|
||||
|
||||
@@ -13,61 +14,12 @@ const isChunkUploaded = (bitmap: Buffer, chunkIndex: number) => {
|
||||
return !!byte && (byte & (1 << (chunkIndex % 8))) !== 0; // Postgres sucks
|
||||
};
|
||||
|
||||
const writeChunkAtOffset = async (
|
||||
path: string,
|
||||
encChunkStream: Readable,
|
||||
chunkIndex: number,
|
||||
isLastChunk: boolean,
|
||||
) => {
|
||||
const offset = (chunkIndex - 1) * ENCRYPTED_CHUNK_SIZE;
|
||||
const file = await open(path, "r+");
|
||||
let written = 0;
|
||||
|
||||
try {
|
||||
for await (const chunk of encChunkStream) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
||||
written += buffer.length;
|
||||
if (written > ENCRYPTED_CHUNK_SIZE) {
|
||||
throw new Error("Invalid chunk size");
|
||||
}
|
||||
|
||||
let chunkOffset = 0;
|
||||
while (chunkOffset < buffer.length) {
|
||||
const { bytesWritten } = await file.write(
|
||||
buffer,
|
||||
chunkOffset,
|
||||
buffer.length - chunkOffset,
|
||||
offset + written - buffer.length + chunkOffset,
|
||||
);
|
||||
if (bytesWritten <= 0) {
|
||||
throw new Error("Failed to write chunk");
|
||||
}
|
||||
chunkOffset += bytesWritten;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
(!isLastChunk && written !== ENCRYPTED_CHUNK_SIZE) ||
|
||||
(isLastChunk && (written <= ENCRYPTION_OVERHEAD || written > ENCRYPTED_CHUNK_SIZE))
|
||||
) {
|
||||
throw new Error("Invalid chunk size");
|
||||
}
|
||||
|
||||
if (isLastChunk) {
|
||||
await file.truncate(offset + written);
|
||||
}
|
||||
|
||||
return written;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
};
|
||||
|
||||
export const uploadChunk = async (
|
||||
userId: number,
|
||||
sessionId: string,
|
||||
chunkIndex: number,
|
||||
encChunkStream: Readable,
|
||||
encChunkHash: string,
|
||||
) => {
|
||||
const lockKey = `${sessionId}/${chunkIndex}`;
|
||||
if (chunkLocks.has(lockKey)) {
|
||||
@@ -76,6 +28,8 @@ export const uploadChunk = async (
|
||||
chunkLocks.add(lockKey);
|
||||
}
|
||||
|
||||
let filePath;
|
||||
|
||||
try {
|
||||
const session = await UploadRepo.getUploadSession(sessionId, userId);
|
||||
if (!session) {
|
||||
@@ -87,10 +41,39 @@ export const uploadChunk = async (
|
||||
}
|
||||
|
||||
const isLastChunk = chunkIndex === session.totalChunks;
|
||||
await writeChunkAtOffset(session.path, encChunkStream, chunkIndex, isLastChunk);
|
||||
filePath = `${session.path}/${chunkIndex}`;
|
||||
|
||||
const hashStream = createHash("sha256");
|
||||
const writeStream = createWriteStream(filePath, { flags: "wx", mode: 0o600 });
|
||||
let writtenBytes = 0;
|
||||
|
||||
for await (const chunk of encChunkStream) {
|
||||
hashStream.update(chunk);
|
||||
writeStream.write(chunk);
|
||||
writtenBytes += chunk.length;
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writeStream.end((e: any) => (e ? reject(e) : resolve()));
|
||||
});
|
||||
|
||||
if (hashStream.digest("base64") !== encChunkHash) {
|
||||
throw new Error("Invalid checksum");
|
||||
} else if (
|
||||
(!isLastChunk && writtenBytes !== ENCRYPTED_CHUNK_SIZE) ||
|
||||
(isLastChunk && (writtenBytes <= ENCRYPTION_OVERHEAD || writtenBytes > ENCRYPTED_CHUNK_SIZE))
|
||||
) {
|
||||
throw new Error("Invalid chunk size");
|
||||
}
|
||||
|
||||
await UploadRepo.markChunkAsUploaded(sessionId, chunkIndex);
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message === "Invalid chunk size") {
|
||||
await safeUnlink(filePath);
|
||||
|
||||
if (
|
||||
e instanceof Error &&
|
||||
(e.message === "Invalid checksum" || e.message === "Invalid chunk size")
|
||||
) {
|
||||
error(400, "Invalid request body");
|
||||
}
|
||||
throw e;
|
||||
@@ -101,5 +84,5 @@ export const uploadChunk = async (
|
||||
|
||||
export const cleanupExpiredUploadSessions = async () => {
|
||||
const paths = await UploadRepo.cleanupExpiredUploadSessions();
|
||||
await Promise.all(paths.map(safeUnlink));
|
||||
await Promise.all(paths.map(safeRecursiveRm));
|
||||
};
|
||||
|
||||
1
src/lib/types/utils.d.ts
vendored
1
src/lib/types/utils.d.ts
vendored
@@ -1 +0,0 @@
|
||||
type AllUndefined<T> = { [K in keyof T]?: undefined };
|
||||
@@ -90,42 +90,4 @@ export class HybridPromise<T> implements PromiseLike<T> {
|
||||
return HybridPromise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
static all<T extends readonly unknown[] | []>(
|
||||
maybePromises: T,
|
||||
): HybridPromise<{ -readonly [P in keyof T]: HybridAwaited<T[P]> }> {
|
||||
const length = maybePromises.length;
|
||||
if (length === 0) {
|
||||
return HybridPromise.resolve([] as any);
|
||||
}
|
||||
|
||||
const hps = Array.from(maybePromises).map((p) => HybridPromise.resolve(p));
|
||||
if (hps.some((hp) => !hp.isSync())) {
|
||||
return new HybridPromise({
|
||||
mode: "async",
|
||||
promise: Promise.all(hps.map((hp) => hp.toPromise())) as any,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
return HybridPromise.resolve(
|
||||
Array.from(
|
||||
hps.map((hp) => {
|
||||
if (hp.state.mode === "sync") {
|
||||
if (hp.state.status === "fulfilled") {
|
||||
return hp.state.value;
|
||||
} else {
|
||||
throw hp.state.reason;
|
||||
}
|
||||
}
|
||||
}),
|
||||
) as any,
|
||||
);
|
||||
} catch (e) {
|
||||
return HybridPromise.reject(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type HybridAwaited<T> =
|
||||
T extends HybridPromise<infer U> ? U : T extends Promise<infer U> ? U : T;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from "./concurrency";
|
||||
export * from "./format";
|
||||
export * from "./gotoStateful";
|
||||
export * from "./search";
|
||||
export * from "./sort";
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import { disassemble, getChoseong } from "es-hangul";
|
||||
|
||||
const normalize = (s: string) => {
|
||||
return s.normalize("NFC").toLowerCase().replace(/\s/g, "");
|
||||
};
|
||||
|
||||
const extractHangul = (s: string) => {
|
||||
return s.replace(/[^가-힣ㄱ-ㅎㅏ-ㅣ]/g, "");
|
||||
};
|
||||
|
||||
const hangulSearch = (original: string, query: string) => {
|
||||
original = extractHangul(original);
|
||||
query = extractHangul(query);
|
||||
if (!original || !query) return false;
|
||||
|
||||
return (
|
||||
disassemble(original).includes(disassemble(query)) ||
|
||||
getChoseong(original).includes(getChoseong(query))
|
||||
);
|
||||
};
|
||||
|
||||
export const searchString = (original: string, query: string) => {
|
||||
original = normalize(original);
|
||||
query = normalize(query);
|
||||
if (!original || !query) return false;
|
||||
|
||||
return original.includes(query) || hangulSearch(original, query);
|
||||
};
|
||||
@@ -18,7 +18,6 @@
|
||||
requestThumbnailUpload,
|
||||
requestFileAdditionToCategory,
|
||||
requestVideoStream,
|
||||
requestFavoriteToggle,
|
||||
} from "./service";
|
||||
import TopBarMenu from "./TopBarMenu.svelte";
|
||||
|
||||
@@ -76,11 +75,6 @@
|
||||
void getFileInfo(data.id, $masterKeyStore?.get(1)?.key!); // TODO: FIXME
|
||||
};
|
||||
|
||||
const toggleFavorite = async () => {
|
||||
await requestFavoriteToggle(data.id, !!info?.isFavorite);
|
||||
void getFileInfo(data.id, $masterKeyStore?.get(1)?.key!); // TODO: FIXME
|
||||
};
|
||||
|
||||
$effect(() => {
|
||||
HybridPromise.resolve(getFileInfo(data.id, $masterKeyStore?.get(1)?.key!)).then((result) => {
|
||||
if (data.id === result.id) {
|
||||
@@ -150,22 +144,18 @@
|
||||
<div onclick={(e) => e.stopPropagation()}>
|
||||
<button
|
||||
onclick={() => (isMenuOpen = !isMenuOpen)}
|
||||
class="w-full rounded-full p-1 text-2xl active:bg-black active:bg-opacity-[0.04]"
|
||||
class="w-[2.3rem] flex-shrink-0 rounded-full p-1 active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconMoreVert />
|
||||
<IconMoreVert class="text-2xl" />
|
||||
</button>
|
||||
<TopBarMenu
|
||||
bind:isOpen={isMenuOpen}
|
||||
directoryId={["category", "gallery", "search", "favorite"].includes(
|
||||
page.url.searchParams.get("from") ?? "",
|
||||
)
|
||||
directoryId={["category", "gallery"].includes(page.url.searchParams.get("from") ?? "")
|
||||
? info?.parentId
|
||||
: undefined}
|
||||
{fileBlob}
|
||||
downloadUrl={videoStreamUrl}
|
||||
filename={info?.name}
|
||||
isFavorite={info?.isFavorite}
|
||||
onToggleFavorite={toggleFavorite}
|
||||
/>
|
||||
</div>
|
||||
</TopBar>
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
import { fly } from "svelte/transition";
|
||||
import { goto } from "$app/navigation";
|
||||
|
||||
import IconFavorite from "~icons/material-symbols/favorite";
|
||||
import IconFavoriteOutline from "~icons/material-symbols/favorite-outline";
|
||||
import IconFolderOpen from "~icons/material-symbols/folder-open";
|
||||
import IconCloudDownload from "~icons/material-symbols/cloud-download";
|
||||
|
||||
@@ -15,20 +13,10 @@
|
||||
downloadUrl?: string;
|
||||
fileBlob?: Blob;
|
||||
filename?: string;
|
||||
isFavorite?: boolean;
|
||||
isOpen: boolean;
|
||||
onToggleFavorite?: () => void;
|
||||
}
|
||||
|
||||
let {
|
||||
directoryId,
|
||||
downloadUrl,
|
||||
fileBlob,
|
||||
filename,
|
||||
isFavorite,
|
||||
isOpen = $bindable(),
|
||||
onToggleFavorite,
|
||||
}: Props = $props();
|
||||
let { directoryId, downloadUrl, fileBlob, filename, isOpen = $bindable() }: Props = $props();
|
||||
|
||||
const handleDownload = () => {
|
||||
if (fileBlob && filename) {
|
||||
@@ -46,7 +34,7 @@
|
||||
|
||||
{#if isOpen && (directoryId || downloadUrl || fileBlob)}
|
||||
<div
|
||||
class="absolute right-2 top-full z-20 min-w-44 space-y-1 rounded-lg bg-white px-1 py-2 shadow-2xl"
|
||||
class="absolute right-2 top-full z-20 space-y-1 rounded-lg bg-white px-1 py-2 shadow-2xl"
|
||||
transition:fly={{ y: -8, duration: 200 }}
|
||||
>
|
||||
<p class="px-3 pt-2 text-sm font-semibold text-gray-600">더보기</p>
|
||||
@@ -66,13 +54,6 @@
|
||||
</button>
|
||||
{/snippet}
|
||||
|
||||
{#if isFavorite !== undefined}
|
||||
{@render menuButton(
|
||||
isFavorite ? IconFavorite : IconFavoriteOutline,
|
||||
isFavorite ? "즐겨찾기 해제" : "즐겨찾기",
|
||||
onToggleFavorite ?? (() => {}),
|
||||
)}
|
||||
{/if}
|
||||
{#if directoryId}
|
||||
{@render menuButton(IconFolderOpen, "폴더에서 보기", () =>
|
||||
goto(
|
||||
|
||||
@@ -48,17 +48,3 @@ export const requestFileAdditionToCategory = async (fileId: number, categoryId:
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const requestFavoriteToggle = async (fileId: number, isFavorite: boolean) => {
|
||||
try {
|
||||
if (isFavorite) {
|
||||
await trpc().favorites.removeFile.mutate({ id: fileId });
|
||||
} else {
|
||||
await trpc().favorites.addFile.mutate({ id: fileId });
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<TopBar />
|
||||
<FullscreenDiv>
|
||||
<div class="space-y-2 pb-4">
|
||||
{#each uploadingFiles as file (file.id)}
|
||||
{#each uploadingFiles as file}
|
||||
<File state={file} />
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
<FullscreenDiv>
|
||||
<RowVirtualizer
|
||||
count={rows.length}
|
||||
estimateItemHeight={(index) =>
|
||||
itemHeight={(index) =>
|
||||
rows[index]!.type === "header" ? 28 : 181 + (rows[index]!.isLast ? 16 : 4)}
|
||||
class="flex flex-grow flex-col"
|
||||
>
|
||||
|
||||
@@ -1,254 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { Snapshot } from "@sveltejs/kit";
|
||||
import superjson, { type SuperJSONResult } from "superjson";
|
||||
import { untrack } from "svelte";
|
||||
import { slide } from "svelte/transition";
|
||||
import { goto } from "$app/navigation";
|
||||
import { Chip, FullscreenDiv, RowVirtualizer } from "$lib/components/atoms";
|
||||
import {
|
||||
getDirectoryInfo,
|
||||
type LocalCategoryInfo,
|
||||
type MaybeDirectoryInfo,
|
||||
} from "$lib/modules/filesystem";
|
||||
import { masterKeyStore } from "$lib/stores";
|
||||
import { HybridPromise, searchString, sortEntries } from "$lib/utils";
|
||||
import Directory from "./Directory.svelte";
|
||||
import File from "./File.svelte";
|
||||
import SearchBar from "./SearchBar.svelte";
|
||||
import SelectCategoryBottomSheet from "./SelectCategoryBottomSheet.svelte";
|
||||
import { requestSearch, type SearchFilter, type SearchResult } from "./service";
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
interface SearchFilters {
|
||||
name: string;
|
||||
includeImages: boolean;
|
||||
includeVideos: boolean;
|
||||
includeDirectories: boolean;
|
||||
searchInFavorites: boolean;
|
||||
searchInDirectory: boolean;
|
||||
categories: SearchFilter["categories"];
|
||||
}
|
||||
|
||||
let directoryInfo: MaybeDirectoryInfo | undefined = $state();
|
||||
|
||||
let filters: SearchFilters = $state({
|
||||
name: "",
|
||||
includeImages: false,
|
||||
includeVideos: false,
|
||||
includeDirectories: false,
|
||||
searchInDirectory: false,
|
||||
searchInFavorites: false,
|
||||
categories: [],
|
||||
});
|
||||
let hasCategoryFilter = $derived(filters.categories.length > 0);
|
||||
let hasAnyFilter = $derived(
|
||||
hasCategoryFilter ||
|
||||
filters.includeImages ||
|
||||
filters.includeVideos ||
|
||||
filters.includeDirectories ||
|
||||
filters.name.trim().length > 0,
|
||||
);
|
||||
|
||||
let isRestoredFromSnapshot = $state(false);
|
||||
let serverResult: SearchResult | undefined = $state();
|
||||
let result = $derived.by(() => {
|
||||
if (!serverResult) return [];
|
||||
|
||||
const nameFilter = filters.name.trim();
|
||||
const hasTypeFilter =
|
||||
filters.includeImages || filters.includeVideos || filters.includeDirectories;
|
||||
|
||||
const directories =
|
||||
!hasTypeFilter || filters.includeDirectories
|
||||
? serverResult.directories.map((directory) => ({
|
||||
type: "directory" as const,
|
||||
...directory,
|
||||
}))
|
||||
: [];
|
||||
const files =
|
||||
!hasTypeFilter || filters.includeImages || filters.includeVideos
|
||||
? serverResult.files
|
||||
.filter(
|
||||
({ contentType }) =>
|
||||
!hasTypeFilter ||
|
||||
(filters.includeImages && contentType.startsWith("image/")) ||
|
||||
(filters.includeVideos && contentType.startsWith("video/")),
|
||||
)
|
||||
.map((file) => ({
|
||||
type: "file" as const,
|
||||
...file,
|
||||
}))
|
||||
: [];
|
||||
|
||||
return sortEntries(
|
||||
[...directories, ...files].filter(
|
||||
(entry) => !nameFilter || searchString(entry.name, nameFilter),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
let isSelectCategoryBottomSheetOpen = $state(false);
|
||||
let categorySelectMode: "include" | "exclude" = $state("include");
|
||||
|
||||
const openSelectCategoryBottomSheet = (mode: "include" | "exclude") => {
|
||||
categorySelectMode = mode;
|
||||
isSelectCategoryBottomSheetOpen = true;
|
||||
};
|
||||
|
||||
const addCategoryFilter = (category: LocalCategoryInfo) => {
|
||||
if (!filters.categories.some(({ info }) => info.id === category.id)) {
|
||||
filters.categories.push({
|
||||
info: category,
|
||||
type: categorySelectMode,
|
||||
});
|
||||
isSelectCategoryBottomSheetOpen = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const snapshot: Snapshot<{
|
||||
filters: SearchFilters;
|
||||
serverResult: SuperJSONResult;
|
||||
}> = {
|
||||
capture() {
|
||||
return { filters, serverResult: superjson.serialize(serverResult) };
|
||||
},
|
||||
restore(value) {
|
||||
filters = value.filters;
|
||||
serverResult = superjson.deserialize(value.serverResult, { inPlace: true });
|
||||
isRestoredFromSnapshot = true;
|
||||
},
|
||||
};
|
||||
|
||||
$effect.pre(() => {
|
||||
if (data.directoryId) {
|
||||
HybridPromise.resolve(getDirectoryInfo(data.directoryId, $masterKeyStore?.get(1)?.key!)).then(
|
||||
(res) => {
|
||||
directoryInfo = res;
|
||||
filters.searchInDirectory = res.exists;
|
||||
},
|
||||
);
|
||||
} else {
|
||||
directoryInfo = undefined;
|
||||
filters.searchInDirectory = false;
|
||||
}
|
||||
});
|
||||
|
||||
$effect.pre(() => {
|
||||
filters.searchInFavorites = data.fromFavorites;
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
// Svelte sucks
|
||||
hasAnyFilter;
|
||||
filters.searchInDirectory;
|
||||
filters.searchInFavorites;
|
||||
filters.categories.length;
|
||||
|
||||
if (untrack(() => isRestoredFromSnapshot)) {
|
||||
isRestoredFromSnapshot = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasAnyFilter) {
|
||||
requestSearch(
|
||||
{
|
||||
ancestorId: filters.searchInDirectory ? data.directoryId! : "root",
|
||||
inFavorites: filters.searchInFavorites,
|
||||
categories: filters.categories,
|
||||
},
|
||||
$masterKeyStore?.get(1)?.key!,
|
||||
).then((res) => {
|
||||
serverResult = res;
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>검색</title>
|
||||
</svelte:head>
|
||||
|
||||
<SearchBar bind:value={filters.name} />
|
||||
<FullscreenDiv class="bg-gray-100 !px-0">
|
||||
<div class="flex flex-grow flex-col space-y-4">
|
||||
<div class="space-y-2 bg-white p-4 !pt-0">
|
||||
<div class="space-y-3">
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<Chip bind:selected={filters.includeImages}>사진</Chip>
|
||||
<Chip bind:selected={filters.includeVideos}>동영상</Chip>
|
||||
{#if !hasCategoryFilter}
|
||||
<Chip bind:selected={filters.includeDirectories}>폴더</Chip>
|
||||
{/if}
|
||||
<Chip bind:selected={filters.searchInFavorites}>즐겨찾기</Chip>
|
||||
{#if directoryInfo?.exists}
|
||||
<Chip bind:selected={filters.searchInDirectory}>
|
||||
위치: {directoryInfo.name}
|
||||
</Chip>
|
||||
{/if}
|
||||
</div>
|
||||
{#if !filters.includeDirectories}
|
||||
<div class="space-y-2" transition:slide={{ duration: 300 }}>
|
||||
<p class="text-sm font-medium text-gray-600">카테고리</p>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
{#each filters.categories as { info, type }, i (info.id)}
|
||||
<Chip
|
||||
selected
|
||||
removable
|
||||
onclick={() => {}}
|
||||
onRemoveClick={() => filters.categories.splice(i, 1)}
|
||||
>
|
||||
{#if type === "include"}
|
||||
포함:
|
||||
{:else}
|
||||
제외:
|
||||
{/if}
|
||||
{info.name}
|
||||
</Chip>
|
||||
{/each}
|
||||
<Chip onclick={() => openSelectCategoryBottomSheet("include")}>+ 포함</Chip>
|
||||
<Chip onclick={() => openSelectCategoryBottomSheet("exclude")}>- 제외</Chip>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{#if hasAnyFilter}
|
||||
<div class="flex flex-grow flex-col space-y-2 bg-white p-4">
|
||||
<p class="text-lg font-bold text-gray-800">
|
||||
검색 결과
|
||||
{#if result.length > 0}
|
||||
{" "}({result.length}개)
|
||||
{/if}
|
||||
</p>
|
||||
{#if result.length > 0}
|
||||
<RowVirtualizer
|
||||
count={result.length}
|
||||
getItemKey={(index) => `${result[index]!.type}-${result[index]!.id}`}
|
||||
estimateItemHeight={() => 56}
|
||||
itemGap={4}
|
||||
>
|
||||
{#snippet item(index)}
|
||||
{@const info = result[index]!}
|
||||
{#if info.type === "directory"}
|
||||
<Directory {info} onclick={() => goto(`/directory/${info.id}?from=search`)} />
|
||||
{:else}
|
||||
<File {info} onclick={() => goto(`/file/${info.id}?from=search`)} />
|
||||
{/if}
|
||||
{/snippet}
|
||||
</RowVirtualizer>
|
||||
{:else}
|
||||
<div class="flex flex-grow items-center justify-center py-8">
|
||||
<p class="text-gray-500">검색 결과가 없어요.</p>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</FullscreenDiv>
|
||||
|
||||
<SelectCategoryBottomSheet
|
||||
bind:isOpen={isSelectCategoryBottomSheetOpen}
|
||||
mode={categorySelectMode}
|
||||
onSelectCategoryClick={addCategoryFilter}
|
||||
/>
|
||||
@@ -1,21 +0,0 @@
|
||||
import { error } from "@sveltejs/kit";
|
||||
import { z } from "zod";
|
||||
import type { PageLoad } from "./$types";
|
||||
|
||||
export const load: PageLoad = ({ url }) => {
|
||||
const directoryId = url.searchParams.get("directoryId");
|
||||
const from = url.searchParams.get("from");
|
||||
|
||||
const zodRes = z
|
||||
.object({
|
||||
directoryId: z.coerce.number().int().positive().nullable(),
|
||||
from: z.enum(["favorites"]).nullable(),
|
||||
})
|
||||
.safeParse({ directoryId, from });
|
||||
if (!zodRes.success) error(400, "Invalid query parameters");
|
||||
|
||||
return {
|
||||
directoryId: zodRes.data.directoryId,
|
||||
fromFavorites: zodRes.data.from === "favorites",
|
||||
};
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { ActionEntryButton } from "$lib/components/atoms";
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import type { SubDirectoryInfo } from "$lib/modules/filesystem";
|
||||
|
||||
interface Props {
|
||||
info: SubDirectoryInfo;
|
||||
onclick: () => void;
|
||||
}
|
||||
|
||||
let { info, onclick }: Props = $props();
|
||||
</script>
|
||||
|
||||
<ActionEntryButton class="h-14" {onclick}>
|
||||
<DirectoryEntryLabel type="directory" name={info.name} isFavorite={info.isFavorite} />
|
||||
</ActionEntryButton>
|
||||
@@ -1,26 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { ActionEntryButton } from "$lib/components/atoms";
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import { getFileThumbnail } from "$lib/modules/file";
|
||||
import type { SummarizedFileInfo } from "$lib/modules/filesystem";
|
||||
import { formatDateTime } from "$lib/utils";
|
||||
|
||||
interface Props {
|
||||
info: SummarizedFileInfo;
|
||||
onclick: () => void;
|
||||
}
|
||||
|
||||
let { info, onclick }: Props = $props();
|
||||
|
||||
let thumbnail = $derived(getFileThumbnail(info));
|
||||
</script>
|
||||
|
||||
<ActionEntryButton class="h-14" {onclick}>
|
||||
<DirectoryEntryLabel
|
||||
type="file"
|
||||
thumbnail={$thumbnail}
|
||||
name={info.name}
|
||||
subtext={formatDateTime(info.createdAt ?? info.lastModifiedAt)}
|
||||
isFavorite={info.isFavorite}
|
||||
/>
|
||||
</ActionEntryButton>
|
||||
@@ -1,24 +0,0 @@
|
||||
<script lang="ts">
|
||||
import IconArrowBack from "~icons/material-symbols/arrow-back";
|
||||
|
||||
interface Props {
|
||||
value: string;
|
||||
}
|
||||
|
||||
let { value = $bindable() }: Props = $props();
|
||||
</script>
|
||||
|
||||
<div class={"sticky top-0 z-10 flex items-center gap-x-2 px-2 py-3 backdrop-blur-2xl"}>
|
||||
<button
|
||||
onclick={() => history.back()}
|
||||
class="w-[2.3rem] flex-shrink-0 rounded-full p-1 active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconArrowBack class="text-2xl" />
|
||||
</button>
|
||||
<input
|
||||
bind:value
|
||||
type="text"
|
||||
placeholder="검색"
|
||||
class="mr-1 h-[2.3rem] min-w-0 flex-grow rounded-lg bg-gray-100 px-3 py-1.5 placeholder-gray-600 focus:outline-none"
|
||||
/>
|
||||
</div>
|
||||
@@ -1,54 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { BottomDiv, BottomSheet, Button, FullscreenDiv } from "$lib/components/atoms";
|
||||
import { SubCategories } from "$lib/components/molecules";
|
||||
import {
|
||||
getCategoryInfo,
|
||||
type LocalCategoryInfo,
|
||||
type MaybeCategoryInfo,
|
||||
} from "$lib/modules/filesystem";
|
||||
import { masterKeyStore } from "$lib/stores";
|
||||
import { HybridPromise } from "$lib/utils";
|
||||
|
||||
interface Props {
|
||||
isOpen: boolean;
|
||||
mode: "include" | "exclude";
|
||||
onSelectCategoryClick: (category: LocalCategoryInfo) => void;
|
||||
}
|
||||
|
||||
let { isOpen = $bindable(), mode, onSelectCategoryClick }: Props = $props();
|
||||
|
||||
let categoryInfo: MaybeCategoryInfo | undefined = $state();
|
||||
|
||||
$effect(() => {
|
||||
if (isOpen) {
|
||||
HybridPromise.resolve(getCategoryInfo("root", $masterKeyStore?.get(1)?.key!)).then(
|
||||
(result) => (categoryInfo = result),
|
||||
);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if categoryInfo?.exists}
|
||||
<BottomSheet bind:isOpen class="flex flex-col">
|
||||
<FullscreenDiv>
|
||||
<SubCategories
|
||||
class="py-4"
|
||||
info={categoryInfo}
|
||||
onSubCategoryClick={({ id }) =>
|
||||
HybridPromise.resolve(getCategoryInfo(id, $masterKeyStore?.get(1)?.key!)).then(
|
||||
(result) => (categoryInfo = result),
|
||||
)}
|
||||
/>
|
||||
{#if categoryInfo.id !== "root"}
|
||||
<BottomDiv>
|
||||
<Button
|
||||
onclick={() => onSelectCategoryClick(categoryInfo as LocalCategoryInfo)}
|
||||
class="w-full"
|
||||
>
|
||||
{categoryInfo.name} 카테고리 {mode === "include" ? "꼭 포함하기" : "제외하기"}
|
||||
</Button>
|
||||
</BottomDiv>
|
||||
{/if}
|
||||
</FullscreenDiv>
|
||||
</BottomSheet>
|
||||
{/if}
|
||||
@@ -1,44 +0,0 @@
|
||||
import {
|
||||
getDirectoryInfo,
|
||||
getFileInfo,
|
||||
type LocalDirectoryInfo,
|
||||
type FileInfo,
|
||||
type LocalCategoryInfo,
|
||||
} from "$lib/modules/filesystem";
|
||||
import { HybridPromise } from "$lib/utils";
|
||||
import { trpc } from "$trpc/client";
|
||||
|
||||
export interface SearchFilter {
|
||||
ancestorId: DirectoryId;
|
||||
inFavorites: boolean;
|
||||
categories: { info: LocalCategoryInfo; type: "include" | "exclude" }[];
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
directories: LocalDirectoryInfo[];
|
||||
files: FileInfo[];
|
||||
}
|
||||
|
||||
export const requestSearch = async (filter: SearchFilter, masterKey: CryptoKey) => {
|
||||
const { directories: directoriesRaw, files: filesRaw } = await trpc().search.search.query({
|
||||
ancestor: filter.ancestorId,
|
||||
inFavorites: filter.inFavorites,
|
||||
includeCategories: filter.categories
|
||||
.filter(({ type }) => type === "include")
|
||||
.map(({ info }) => info.id),
|
||||
excludeCategories: filter.categories
|
||||
.filter(({ type }) => type === "exclude")
|
||||
.map(({ info }) => info.id),
|
||||
});
|
||||
const [directories, files] = await HybridPromise.all([
|
||||
HybridPromise.all(
|
||||
directoriesRaw.map((directory) =>
|
||||
getDirectoryInfo(directory.id, masterKey, { serverResponse: directory }),
|
||||
),
|
||||
),
|
||||
HybridPromise.all(
|
||||
filesRaw.map((file) => getFileInfo(file.id, masterKey, { serverResponse: file })),
|
||||
),
|
||||
]);
|
||||
return { directories, files } as SearchResult;
|
||||
};
|
||||
@@ -2,6 +2,6 @@ import { createCaller } from "$trpc/router.server";
|
||||
import type { PageServerLoad } from "./$types";
|
||||
|
||||
export const load: PageServerLoad = async (event) => {
|
||||
const favorites = await createCaller(event).favorites.get();
|
||||
return { favorites };
|
||||
const files = await createCaller(event).file.listLegacy();
|
||||
return { files };
|
||||
};
|
||||
79
src/routes/(fullscreen)/settings/migration/+page.svelte
Normal file
79
src/routes/(fullscreen)/settings/migration/+page.svelte
Normal file
@@ -0,0 +1,79 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import { goto } from "$app/navigation";
|
||||
import { BottomDiv, Button, FullscreenDiv } from "$lib/components/atoms";
|
||||
import { TopBar } from "$lib/components/molecules";
|
||||
import { bulkGetFileInfo, type MaybeFileInfo } from "$lib/modules/filesystem";
|
||||
import { masterKeyStore } from "$lib/stores";
|
||||
import { sortEntries } from "$lib/utils";
|
||||
import File from "./File.svelte";
|
||||
import { getMigrationState, clearMigrationStates, requestFileMigration } from "./service.svelte";
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
let fileInfos: MaybeFileInfo[] = $state([]);
|
||||
let files = $derived(
|
||||
fileInfos
|
||||
.map((info) => ({
|
||||
info,
|
||||
state: getMigrationState(info.id),
|
||||
}))
|
||||
.filter((file) => file.state?.status !== "uploaded"),
|
||||
);
|
||||
|
||||
const migrateAllFiles = () => {
|
||||
files.forEach(({ info }) => {
|
||||
if (info.exists) {
|
||||
requestFileMigration(info);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
onMount(async () => {
|
||||
fileInfos = sortEntries(
|
||||
Array.from((await bulkGetFileInfo(data.files, $masterKeyStore?.get(1)?.key!)).values()),
|
||||
);
|
||||
});
|
||||
|
||||
$effect(() => clearMigrationStates);
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>암호화 마이그레이션</title>
|
||||
</svelte:head>
|
||||
|
||||
<TopBar title="암호화 마이그레이션" />
|
||||
<FullscreenDiv>
|
||||
{#if files.length > 0}
|
||||
<div class="space-y-4 pb-4">
|
||||
<p class="break-keep text-gray-800">
|
||||
이전 버전의 ArkVault에서 업로드된 {files.length}개 파일을 다시 암호화할 수 있어요.
|
||||
</p>
|
||||
<div class="space-y-2">
|
||||
{#each files as { info, state } (info.id)}
|
||||
{#if info.exists}
|
||||
<File
|
||||
{info}
|
||||
{state}
|
||||
onclick={({ id }) => goto(`/file/${id}`)}
|
||||
onMigrateClick={requestFileMigration}
|
||||
/>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
<BottomDiv>
|
||||
<Button onclick={migrateAllFiles} class="w-full">모두 다시 암호화하기</Button>
|
||||
</BottomDiv>
|
||||
{:else}
|
||||
<div class="flex flex-grow items-center justify-center">
|
||||
<p class="text-gray-500">
|
||||
{#if data.files.length === 0}
|
||||
마이그레이션할 파일이 없어요.
|
||||
{:else}
|
||||
파일 목록을 불러오고 있어요.
|
||||
{/if}
|
||||
</p>
|
||||
</div>
|
||||
{/if}
|
||||
</FullscreenDiv>
|
||||
52
src/routes/(fullscreen)/settings/migration/File.svelte
Normal file
52
src/routes/(fullscreen)/settings/migration/File.svelte
Normal file
@@ -0,0 +1,52 @@
|
||||
<script module lang="ts">
|
||||
const subtexts = {
|
||||
queued: "대기 중",
|
||||
downloading: "다운로드하는 중",
|
||||
"upload-pending": "업로드를 기다리는 중",
|
||||
uploaded: "",
|
||||
error: "실패",
|
||||
} as const;
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { ActionEntryButton } from "$lib/components/atoms";
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import type { FileInfo } from "$lib/modules/filesystem";
|
||||
import { formatDateTime, formatNetworkSpeed } from "$lib/utils";
|
||||
import type { MigrationState } from "./service.svelte";
|
||||
|
||||
import IconSync from "~icons/material-symbols/sync";
|
||||
|
||||
type FileInfoWithExists = FileInfo & { exists: true };
|
||||
|
||||
interface Props {
|
||||
info: FileInfoWithExists;
|
||||
onclick: (file: FileInfo) => void;
|
||||
onMigrateClick: (file: FileInfoWithExists) => void;
|
||||
state: MigrationState | undefined;
|
||||
}
|
||||
|
||||
let { info, onclick, onMigrateClick, state }: Props = $props();
|
||||
|
||||
let subtext = $derived.by(() => {
|
||||
if (!state) {
|
||||
return formatDateTime(info.createdAt ?? info.lastModifiedAt);
|
||||
}
|
||||
if (state.status === "uploading") {
|
||||
const progress = Math.floor((state.progress ?? 0) * 100);
|
||||
const speed = formatNetworkSpeed((state.rate ?? 0) * 8);
|
||||
return `전송됨 ${progress}% · ${speed}`;
|
||||
}
|
||||
return subtexts[state.status] ?? state.status;
|
||||
});
|
||||
</script>
|
||||
|
||||
<ActionEntryButton
|
||||
class="h-14"
|
||||
onclick={() => onclick(info)}
|
||||
actionButtonIcon={!state || state.status === "error" ? IconSync : undefined}
|
||||
onActionButtonClick={() => onMigrateClick(info)}
|
||||
actionButtonClass="text-gray-800"
|
||||
>
|
||||
<DirectoryEntryLabel type="file" name={info.name} {subtext} />
|
||||
</ActionEntryButton>
|
||||
105
src/routes/(fullscreen)/settings/migration/service.svelte.ts
Normal file
105
src/routes/(fullscreen)/settings/migration/service.svelte.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { limitFunction } from "p-limit";
|
||||
import { SvelteMap } from "svelte/reactivity";
|
||||
import { CHUNK_SIZE } from "$lib/constants";
|
||||
import type { FileInfo } from "$lib/modules/filesystem";
|
||||
import { uploadBlob } from "$lib/modules/upload";
|
||||
import { requestFileDownload } from "$lib/services/file";
|
||||
import { Scheduler } from "$lib/utils";
|
||||
import { trpc } from "$trpc/client";
|
||||
|
||||
export type MigrationStatus =
|
||||
| "queued"
|
||||
| "downloading"
|
||||
| "upload-pending"
|
||||
| "uploading"
|
||||
| "uploaded"
|
||||
| "error";
|
||||
|
||||
export interface MigrationState {
|
||||
status: MigrationStatus;
|
||||
progress?: number;
|
||||
rate?: number;
|
||||
}
|
||||
|
||||
const scheduler = new Scheduler();
|
||||
const states = new SvelteMap<number, MigrationState>();
|
||||
|
||||
const createState = (status: MigrationStatus): MigrationState => {
|
||||
const state = $state({ status });
|
||||
return state;
|
||||
};
|
||||
|
||||
export const getMigrationState = (fileId: number) => {
|
||||
return states.get(fileId);
|
||||
};
|
||||
|
||||
export const clearMigrationStates = () => {
|
||||
for (const [id, state] of states) {
|
||||
if (state.status === "uploaded" || state.status === "error") {
|
||||
states.delete(id);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const requestFileUpload = limitFunction(
|
||||
async (
|
||||
state: MigrationState,
|
||||
fileId: number,
|
||||
fileBuffer: ArrayBuffer,
|
||||
dataKey: CryptoKey,
|
||||
dataKeyVersion: Date,
|
||||
) => {
|
||||
state.status = "uploading";
|
||||
|
||||
const { uploadId } = await trpc().upload.startMigrationUpload.mutate({
|
||||
file: fileId,
|
||||
chunks: Math.ceil(fileBuffer.byteLength / CHUNK_SIZE),
|
||||
dekVersion: dataKeyVersion,
|
||||
});
|
||||
|
||||
await uploadBlob(uploadId, new Blob([fileBuffer]), dataKey, {
|
||||
onProgress(s) {
|
||||
state.progress = s.progress;
|
||||
state.rate = s.rate;
|
||||
},
|
||||
});
|
||||
|
||||
await trpc().upload.completeMigrationUpload.mutate({ uploadId });
|
||||
state.status = "uploaded";
|
||||
},
|
||||
{ concurrency: 1 },
|
||||
);
|
||||
|
||||
export const requestFileMigration = async (fileInfo: FileInfo) => {
|
||||
let state = states.get(fileInfo.id);
|
||||
if (state) {
|
||||
if (state.status !== "error") return;
|
||||
state.status = "queued";
|
||||
state.progress = undefined;
|
||||
state.rate = undefined;
|
||||
} else {
|
||||
state = createState("queued");
|
||||
states.set(fileInfo.id, state);
|
||||
}
|
||||
|
||||
try {
|
||||
const dataKey = fileInfo.dataKey;
|
||||
if (!dataKey) {
|
||||
throw new Error("Data key not available");
|
||||
}
|
||||
|
||||
let fileBuffer: ArrayBuffer | undefined;
|
||||
|
||||
await scheduler.schedule(
|
||||
async () => {
|
||||
state.status = "downloading";
|
||||
fileBuffer = await requestFileDownload(fileInfo.id, dataKey.key, true);
|
||||
return fileBuffer.byteLength;
|
||||
},
|
||||
() => requestFileUpload(state, fileInfo.id, fileBuffer!, dataKey.key, dataKey.version),
|
||||
);
|
||||
} catch (e) {
|
||||
state.status = "error";
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -4,14 +4,13 @@
|
||||
import { BottomDiv, Button, FullscreenDiv } from "$lib/components/atoms";
|
||||
import { IconEntryButton, TopBar } from "$lib/components/molecules";
|
||||
import { deleteAllFileThumbnailCaches } from "$lib/modules/file";
|
||||
import type { MaybeFileInfo } from "$lib/modules/filesystem";
|
||||
import { bulkGetFileInfo, type MaybeFileInfo } from "$lib/modules/filesystem";
|
||||
import { masterKeyStore } from "$lib/stores";
|
||||
import { sortEntries } from "$lib/utils";
|
||||
import File from "./File.svelte";
|
||||
import {
|
||||
getThumbnailGenerationStatus,
|
||||
clearThumbnailGenerationStatuses,
|
||||
requestMissingThumbnailFiles,
|
||||
requestThumbnailGeneration,
|
||||
type GenerationStatus,
|
||||
} from "./service";
|
||||
@@ -43,7 +42,7 @@
|
||||
|
||||
onMount(async () => {
|
||||
fileInfos = sortEntries(
|
||||
await requestMissingThumbnailFiles(data.files, $masterKeyStore?.get(1)?.key!),
|
||||
Array.from((await bulkGetFileInfo(data.files, $masterKeyStore?.get(1)?.key!)).values()),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { limitFunction } from "p-limit";
|
||||
import { SvelteMap } from "svelte/reactivity";
|
||||
import { storeFileThumbnailCache } from "$lib/modules/file";
|
||||
import { getFileInfo, type FileInfo } from "$lib/modules/filesystem";
|
||||
import type { FileInfo } from "$lib/modules/filesystem";
|
||||
import { generateThumbnail } from "$lib/modules/thumbnail";
|
||||
import { requestFileDownload, requestFileThumbnailUpload } from "$lib/services/file";
|
||||
import { HybridPromise, Scheduler } from "$lib/utils";
|
||||
import type { RouterOutputs } from "$trpc/router.server";
|
||||
import { Scheduler } from "$lib/utils";
|
||||
|
||||
export type GenerationStatus =
|
||||
| "queued"
|
||||
@@ -30,16 +29,6 @@ export const clearThumbnailGenerationStatuses = () => {
|
||||
}
|
||||
};
|
||||
|
||||
export const requestMissingThumbnailFiles = async (
|
||||
filesRaw: RouterOutputs["file"]["listWithoutThumbnail"],
|
||||
masterKey: CryptoKey,
|
||||
) => {
|
||||
const files = await HybridPromise.all(
|
||||
filesRaw.map((file) => getFileInfo(file.id, masterKey, { serverResponse: file })),
|
||||
);
|
||||
return files;
|
||||
};
|
||||
|
||||
const requestThumbnailUpload = limitFunction(
|
||||
async (fileInfo: FileInfo, fileBuffer: ArrayBuffer) => {
|
||||
statuses.set(fileInfo.id, "generating");
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
const pages = [
|
||||
{ path: "/home", label: "홈", icon: IconHome },
|
||||
{ path: "/directory", label: "파일", icon: IconFolder },
|
||||
{ path: "/favorites", label: "즐겨찾기", icon: IconFavorite },
|
||||
{ path: "/favorite", label: "즐겨찾기", icon: IconFavorite },
|
||||
{ path: "/category", label: "카테고리", icon: IconCategory },
|
||||
{ path: "/menu", label: "전체", icon: IconMenu },
|
||||
];
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
<div class="min-h-full bg-gray-100 pb-[5.5em]">
|
||||
{#if info?.exists}
|
||||
<div class="space-y-4">
|
||||
<div class="space-y-2 bg-white p-4">
|
||||
<div class="space-y-4 bg-white p-4">
|
||||
{#if info.id !== "root"}
|
||||
<p class="text-lg font-bold text-gray-800">하위 카테고리</p>
|
||||
{/if}
|
||||
@@ -84,7 +84,6 @@
|
||||
{info}
|
||||
onSubCategoryClick={({ id }) => goto(`/category/${id}`)}
|
||||
onSubCategoryCreateClick={() => (isCategoryCreateModalOpen = true)}
|
||||
subCategoryCreatePosition="bottom"
|
||||
onSubCategoryMenuClick={(subCategory) => {
|
||||
context.selectedCategory = subCategory;
|
||||
isCategoryMenuBottomSheetOpen = true;
|
||||
@@ -93,19 +92,14 @@
|
||||
/>
|
||||
</div>
|
||||
{#if info.id !== "root"}
|
||||
<div class="space-y-2 bg-white p-4">
|
||||
<div class="space-y-4 bg-white p-4">
|
||||
<div class="flex items-center justify-between">
|
||||
<p class="text-lg font-bold text-gray-800">파일</p>
|
||||
<CheckBox bind:checked={info.isFileRecursive}>
|
||||
<p class="font-medium">하위 카테고리의 파일</p>
|
||||
</CheckBox>
|
||||
</div>
|
||||
<RowVirtualizer
|
||||
count={files.length}
|
||||
getItemKey={(index) => files[index]!.details.id}
|
||||
estimateItemHeight={() => 48}
|
||||
itemGap={4}
|
||||
>
|
||||
<RowVirtualizer count={files.length} itemHeight={() => 48} itemGap={4}>
|
||||
{#snippet item(index)}
|
||||
{@const { details } = files[index]!}
|
||||
<File
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import { getFileThumbnail } from "$lib/modules/file";
|
||||
import type { CategoryFileInfo } from "$lib/modules/filesystem";
|
||||
import { formatDateTime } from "$lib/utils";
|
||||
import type { SelectedFile } from "./service.svelte";
|
||||
|
||||
import IconClose from "~icons/material-symbols/close";
|
||||
@@ -20,16 +19,10 @@
|
||||
</script>
|
||||
|
||||
<ActionEntryButton
|
||||
class="h-14"
|
||||
class="h-12"
|
||||
onclick={() => onclick(info)}
|
||||
actionButtonIcon={onRemoveClick && IconClose}
|
||||
onActionButtonClick={() => onRemoveClick?.(info)}
|
||||
>
|
||||
<DirectoryEntryLabel
|
||||
type="file"
|
||||
thumbnail={$thumbnail}
|
||||
name={info.name}
|
||||
subtext={formatDateTime(info.createdAt ?? info.lastModifiedAt)}
|
||||
isFavorite={info.isFavorite}
|
||||
/>
|
||||
<DirectoryEntryLabel type="file" thumbnail={$thumbnail} name={info.name} />
|
||||
</ActionEntryButton>
|
||||
|
||||
@@ -23,10 +23,8 @@
|
||||
requestFileUpload,
|
||||
requestEntryRename,
|
||||
requestEntryDeletion,
|
||||
requestFavoriteToggle,
|
||||
} from "./service.svelte";
|
||||
|
||||
import IconSearch from "~icons/material-symbols/search";
|
||||
import IconAdd from "~icons/material-symbols/add";
|
||||
|
||||
let { data } = $props();
|
||||
@@ -45,19 +43,8 @@
|
||||
let isEntryRenameModalOpen = $state(false);
|
||||
let isEntryDeleteModalOpen = $state(false);
|
||||
|
||||
let showParentEntry = $derived(
|
||||
["file", "search", "favorite"].includes(page.url.searchParams.get("from") ?? ""),
|
||||
);
|
||||
let showBackButton = $derived(data.id !== "root" || showParentEntry);
|
||||
|
||||
const onSearchClick = async () => {
|
||||
const params = new URLSearchParams();
|
||||
if (data.id !== "root") {
|
||||
params.set("directoryId", data.id.toString());
|
||||
}
|
||||
const query = params.toString();
|
||||
await goto(`/search${query ? `?${query}` : ""}`);
|
||||
};
|
||||
let isFromFilePage = $derived(page.url.searchParams.get("from") === "file");
|
||||
let showTopBar = $derived(data.id !== "root" || isFromFilePage);
|
||||
|
||||
const uploadFile = () => {
|
||||
const files = fileInput?.files;
|
||||
@@ -103,22 +90,17 @@
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>내 파일</title>
|
||||
<title>파일</title>
|
||||
</svelte:head>
|
||||
|
||||
<input bind:this={fileInput} onchange={uploadFile} type="file" multiple class="hidden" />
|
||||
|
||||
<div class="flex h-full flex-col">
|
||||
<TopBar title={info?.name ?? "내 파일"} {showBackButton} class="flex-shrink-0">
|
||||
<button
|
||||
onclick={onSearchClick}
|
||||
class="w-[2.3rem] flex-shrink-0 rounded-full p-1 active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconSearch class="text-2xl" />
|
||||
</button>
|
||||
</TopBar>
|
||||
{#if showTopBar}
|
||||
<TopBar title={info?.name} class="flex-shrink-0" />
|
||||
{/if}
|
||||
{#if info?.exists}
|
||||
<div class="flex flex-grow flex-col px-4 pb-4">
|
||||
<div class={["flex flex-grow flex-col px-4 pb-4", !showTopBar && "pt-4"]}>
|
||||
<div class="flex gap-x-2">
|
||||
<UploadStatusCard onclick={() => goto("/file/uploads")} />
|
||||
<DownloadStatusCard onclick={() => goto("/file/downloads")} />
|
||||
@@ -131,12 +113,12 @@
|
||||
context.selectedEntry = entry;
|
||||
isEntryMenuBottomSheetOpen = true;
|
||||
}}
|
||||
showParentEntry={showParentEntry && data.id !== "root"}
|
||||
showParentEntry={isFromFilePage && info.parentId !== undefined}
|
||||
onParentClick={() =>
|
||||
goto(
|
||||
info!.parentId === "root"
|
||||
? `/directory?from=${page.url.searchParams.get("from")}`
|
||||
: `/directory/${info!.parentId}?from=${page.url.searchParams.get("from")}`,
|
||||
? "/directory?from=file"
|
||||
: `/directory/${info!.parentId}?from=file`,
|
||||
)}
|
||||
/>
|
||||
{/key}
|
||||
@@ -195,12 +177,6 @@
|
||||
isEntryMenuBottomSheetOpen = false;
|
||||
isEntryDeleteModalOpen = true;
|
||||
}}
|
||||
onFavoriteClick={async () => {
|
||||
if (await requestFavoriteToggle(context.selectedEntry!)) {
|
||||
isEntryMenuBottomSheetOpen = false;
|
||||
void getDirectoryInfo(data.id, $masterKeyStore?.get(1)?.key!); // TODO: FIXME
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<EntryRenameModal
|
||||
bind:isOpen={isEntryRenameModalOpen}
|
||||
|
||||
@@ -46,16 +46,7 @@
|
||||
</script>
|
||||
|
||||
{#if entries.length > 0}
|
||||
<RowVirtualizer
|
||||
count={entries.length}
|
||||
getItemKey={(index) =>
|
||||
entries[index]!.type !== "parent"
|
||||
? `${entries[index]!.type}-${entries[index]!.details.id}`
|
||||
: entries[index]!.type!}
|
||||
estimateItemHeight={() => 56}
|
||||
itemGap={4}
|
||||
class="pb-[4.5rem]"
|
||||
>
|
||||
<RowVirtualizer count={entries.length} itemHeight={() => 56} itemGap={4} class="pb-[4.5rem]">
|
||||
{#snippet item(index)}
|
||||
{@const entry = entries[index]!}
|
||||
{#if entry.type === "parent"}
|
||||
|
||||
@@ -19,13 +19,7 @@
|
||||
let thumbnail = $derived(getFileThumbnail(info));
|
||||
|
||||
const action = (callback: typeof onclick) => {
|
||||
callback({
|
||||
type: "file",
|
||||
id: info.id,
|
||||
dataKey: info.dataKey,
|
||||
name: info.name,
|
||||
isFavorite: info.isFavorite,
|
||||
});
|
||||
callback({ type: "file", id: info.id, dataKey: info.dataKey, name: info.name });
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -40,6 +34,5 @@
|
||||
thumbnail={$thumbnail}
|
||||
name={info.name}
|
||||
subtext={formatDateTime(info.createdAt ?? info.lastModifiedAt)}
|
||||
isFavorite={info.isFavorite}
|
||||
/>
|
||||
</ActionEntryButton>
|
||||
|
||||
@@ -15,13 +15,7 @@
|
||||
let { info, onclick, onOpenMenuClick }: Props = $props();
|
||||
|
||||
const action = (callback: typeof onclick) => {
|
||||
callback({
|
||||
type: "directory",
|
||||
id: info.id,
|
||||
dataKey: info.dataKey,
|
||||
name: info.name,
|
||||
isFavorite: info.isFavorite,
|
||||
});
|
||||
callback({ type: "directory", id: info.id, dataKey: info.dataKey, name: info.name });
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -31,5 +25,5 @@
|
||||
actionButtonIcon={IconMoreVert}
|
||||
onActionButtonClick={() => action(onOpenMenuClick)}
|
||||
>
|
||||
<DirectoryEntryLabel type="directory" name={info.name} isFavorite={info.isFavorite} />
|
||||
<DirectoryEntryLabel type="directory" name={info.name} />
|
||||
</ActionEntryButton>
|
||||
|
||||
@@ -3,35 +3,24 @@
|
||||
import { DirectoryEntryLabel, IconEntryButton } from "$lib/components/molecules";
|
||||
import { useContext } from "./service.svelte";
|
||||
|
||||
import IconFavorite from "~icons/material-symbols/favorite";
|
||||
import IconFavoriteBorder from "~icons/material-symbols/favorite-outline";
|
||||
import IconEdit from "~icons/material-symbols/edit";
|
||||
import IconDelete from "~icons/material-symbols/delete";
|
||||
|
||||
interface Props {
|
||||
isOpen: boolean;
|
||||
onDeleteClick: () => void;
|
||||
onFavoriteClick: () => void;
|
||||
onRenameClick: () => void;
|
||||
}
|
||||
|
||||
let { isOpen = $bindable(), onDeleteClick, onFavoriteClick, onRenameClick }: Props = $props();
|
||||
let { isOpen = $bindable(), onDeleteClick, onRenameClick }: Props = $props();
|
||||
let context = useContext();
|
||||
</script>
|
||||
|
||||
{#if context.selectedEntry}
|
||||
{@const { name, type, isFavorite } = context.selectedEntry}
|
||||
{@const { name, type } = context.selectedEntry}
|
||||
<BottomSheet bind:isOpen class="p-4">
|
||||
<DirectoryEntryLabel {type} {name} class="h-12 p-2" textClass="!font-semibold" />
|
||||
<div class="my-2 h-px w-full bg-gray-200"></div>
|
||||
<IconEntryButton
|
||||
icon={isFavorite ? IconFavorite : IconFavoriteBorder}
|
||||
onclick={onFavoriteClick}
|
||||
class="h-12 w-full"
|
||||
iconClass={isFavorite ? "text-red-500" : ""}
|
||||
>
|
||||
{isFavorite ? "즐겨찾기에서 해제하기" : "즐겨찾기에 추가하기"}
|
||||
</IconEntryButton>
|
||||
<IconEntryButton icon={IconEdit} onclick={onRenameClick} class="h-12 w-full">
|
||||
이름 바꾸기
|
||||
</IconEntryButton>
|
||||
|
||||
@@ -17,7 +17,6 @@ export interface SelectedEntry {
|
||||
id: number;
|
||||
dataKey: DataKey | undefined;
|
||||
name: string;
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
export const createContext = () => {
|
||||
@@ -150,25 +149,3 @@ export const requestEntryDeletion = async (entry: SelectedEntry) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const requestFavoriteToggle = async (entry: SelectedEntry) => {
|
||||
try {
|
||||
if (entry.type === "directory") {
|
||||
if (entry.isFavorite) {
|
||||
await trpc().favorites.removeDirectory.mutate({ id: entry.id });
|
||||
} else {
|
||||
await trpc().favorites.addDirectory.mutate({ id: entry.id });
|
||||
}
|
||||
} else {
|
||||
if (entry.isFavorite) {
|
||||
await trpc().favorites.removeFile.mutate({ id: entry.id });
|
||||
} else {
|
||||
await trpc().favorites.addFile.mutate({ id: entry.id });
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
3
src/routes/(main)/favorite/+page.svelte
Normal file
3
src/routes/(main)/favorite/+page.svelte
Normal file
@@ -0,0 +1,3 @@
|
||||
<div class="flex h-full items-center justify-center p-4">
|
||||
<p class="text-gray-500">아직 개발 중이에요.</p>
|
||||
</div>
|
||||
@@ -1,93 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import { goto } from "$app/navigation";
|
||||
import { RowVirtualizer } from "$lib/components/atoms";
|
||||
import { TopBar } from "$lib/components/molecules";
|
||||
import { masterKeyStore } from "$lib/stores";
|
||||
import Directory from "./Directory.svelte";
|
||||
import File from "./File.svelte";
|
||||
import { requestFavoriteEntries, requestRemoveFavorite, type FavoriteEntry } from "./service";
|
||||
|
||||
import IconSearch from "~icons/material-symbols/search";
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
let entries: FavoriteEntry[] = $state([]);
|
||||
let isLoading = $state(true);
|
||||
|
||||
onMount(async () => {
|
||||
const masterKey = $masterKeyStore?.get(1)?.key;
|
||||
if (masterKey) {
|
||||
entries = await requestFavoriteEntries(data.favorites, masterKey);
|
||||
}
|
||||
isLoading = false;
|
||||
});
|
||||
|
||||
const handleRemove = async (entry: FavoriteEntry) => {
|
||||
if (await requestRemoveFavorite(entry.type, entry.details.id)) {
|
||||
entries = entries.filter(
|
||||
(e) => !(e.type === entry.type && e.details.id === entry.details.id),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const handleClick = (entry: FavoriteEntry) => {
|
||||
goto(
|
||||
entry.type === "file"
|
||||
? `/file/${entry.details.id}?from=favorite`
|
||||
: `/directory/${entry.details.id}?from=favorite`,
|
||||
);
|
||||
};
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>즐겨찾기</title>
|
||||
</svelte:head>
|
||||
|
||||
<div class="flex h-full flex-col">
|
||||
<TopBar title="즐겨찾기" showBackButton={false}>
|
||||
<button
|
||||
onclick={() => goto("/search?from=favorites")}
|
||||
class="w-full rounded-full p-1 text-2xl active:bg-black active:bg-opacity-[0.04]"
|
||||
>
|
||||
<IconSearch />
|
||||
</button>
|
||||
</TopBar>
|
||||
<div class="flex flex-grow flex-col px-4 pb-4">
|
||||
{#if entries.length > 0}
|
||||
<RowVirtualizer
|
||||
count={entries.length}
|
||||
getItemKey={(index) => `${entries[index]!.type}-${entries[index]!.details.id}`}
|
||||
estimateItemHeight={() => 56}
|
||||
itemGap={4}
|
||||
>
|
||||
{#snippet item(index)}
|
||||
{@const entry = entries[index]!}
|
||||
{#if entry.type === "directory"}
|
||||
<Directory
|
||||
info={entry.details}
|
||||
onclick={() => handleClick(entry)}
|
||||
onRemoveClick={() => handleRemove(entry)}
|
||||
/>
|
||||
{:else}
|
||||
<File
|
||||
info={entry.details}
|
||||
onclick={() => handleClick(entry)}
|
||||
onRemoveClick={() => handleRemove(entry)}
|
||||
/>
|
||||
{/if}
|
||||
{/snippet}
|
||||
</RowVirtualizer>
|
||||
{:else}
|
||||
<div class="flex flex-grow items-center justify-center">
|
||||
<p class="text-gray-500">
|
||||
{#if isLoading}
|
||||
즐겨찾기 목록을 불러오고 있어요.
|
||||
{:else}
|
||||
즐겨찾기한 항목이 없어요.
|
||||
{/if}
|
||||
</p>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,24 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { ActionEntryButton } from "$lib/components/atoms";
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import type { SubDirectoryInfo } from "$lib/modules/filesystem";
|
||||
|
||||
import IconClose from "~icons/material-symbols/close";
|
||||
|
||||
interface Props {
|
||||
info: SubDirectoryInfo;
|
||||
onclick: () => void;
|
||||
onRemoveClick: () => void;
|
||||
}
|
||||
|
||||
let { info, onclick, onRemoveClick }: Props = $props();
|
||||
</script>
|
||||
|
||||
<ActionEntryButton
|
||||
class="h-14"
|
||||
{onclick}
|
||||
actionButtonIcon={IconClose}
|
||||
onActionButtonClick={onRemoveClick}
|
||||
>
|
||||
<DirectoryEntryLabel type="directory" name={info.name} isFavorite />
|
||||
</ActionEntryButton>
|
||||
@@ -1,34 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { ActionEntryButton } from "$lib/components/atoms";
|
||||
import { DirectoryEntryLabel } from "$lib/components/molecules";
|
||||
import { getFileThumbnail } from "$lib/modules/file";
|
||||
import type { SummarizedFileInfo } from "$lib/modules/filesystem";
|
||||
import { formatDateTime } from "$lib/utils";
|
||||
|
||||
import IconClose from "~icons/material-symbols/close";
|
||||
|
||||
interface Props {
|
||||
info: SummarizedFileInfo;
|
||||
onclick: () => void;
|
||||
onRemoveClick: () => void;
|
||||
}
|
||||
|
||||
let { info, onclick, onRemoveClick }: Props = $props();
|
||||
|
||||
let thumbnail = $derived(getFileThumbnail(info));
|
||||
</script>
|
||||
|
||||
<ActionEntryButton
|
||||
class="h-14"
|
||||
{onclick}
|
||||
actionButtonIcon={IconClose}
|
||||
onActionButtonClick={onRemoveClick}
|
||||
>
|
||||
<DirectoryEntryLabel
|
||||
type="file"
|
||||
thumbnail={$thumbnail}
|
||||
name={info.name}
|
||||
subtext={formatDateTime(info.createdAt ?? info.lastModifiedAt)}
|
||||
isFavorite
|
||||
/>
|
||||
</ActionEntryButton>
|
||||
@@ -1,68 +0,0 @@
|
||||
import {
|
||||
getDirectoryInfo,
|
||||
getFileInfo,
|
||||
type SummarizedFileInfo,
|
||||
type SubDirectoryInfo,
|
||||
type LocalDirectoryInfo,
|
||||
} from "$lib/modules/filesystem";
|
||||
import { HybridPromise, sortEntries } from "$lib/utils";
|
||||
import { trpc } from "$trpc/client";
|
||||
import type { RouterOutputs } from "$trpc/router.server";
|
||||
|
||||
export type FavoriteEntry =
|
||||
| { type: "directory"; name: string; details: SubDirectoryInfo }
|
||||
| { type: "file"; name: string; details: SummarizedFileInfo };
|
||||
|
||||
export const requestFavoriteEntries = async (
|
||||
favorites: RouterOutputs["favorites"]["get"],
|
||||
masterKey: CryptoKey,
|
||||
) => {
|
||||
const [directories, files] = await HybridPromise.all([
|
||||
HybridPromise.all(
|
||||
favorites.directories.map((directory) =>
|
||||
getDirectoryInfo(directory.id, masterKey, {
|
||||
serverResponse: { ...directory, isFavorite: true },
|
||||
}),
|
||||
),
|
||||
),
|
||||
HybridPromise.all(
|
||||
favorites.files.map((file) =>
|
||||
getFileInfo(file.id, masterKey, { serverResponse: { ...file, isFavorite: true } }),
|
||||
),
|
||||
),
|
||||
]);
|
||||
return [
|
||||
...sortEntries(
|
||||
directories.map(
|
||||
(directory): FavoriteEntry => ({
|
||||
type: "directory",
|
||||
name: directory.name!,
|
||||
details: directory as LocalDirectoryInfo,
|
||||
}),
|
||||
),
|
||||
),
|
||||
...sortEntries(
|
||||
files.map(
|
||||
(file): FavoriteEntry => ({
|
||||
type: "file",
|
||||
name: file.name!,
|
||||
details: file as SummarizedFileInfo,
|
||||
}),
|
||||
),
|
||||
),
|
||||
];
|
||||
};
|
||||
|
||||
export const requestRemoveFavorite = async (type: "file" | "directory", id: number) => {
|
||||
try {
|
||||
if (type === "directory") {
|
||||
await trpc().favorites.removeDirectory.mutate({ id });
|
||||
} else {
|
||||
await trpc().favorites.removeFile.mutate({ id });
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import IconStorage from "~icons/material-symbols/storage";
|
||||
import IconImage from "~icons/material-symbols/image";
|
||||
import IconLockReset from "~icons/material-symbols/lock-reset";
|
||||
import IconPassword from "~icons/material-symbols/password";
|
||||
import IconLogout from "~icons/material-symbols/logout";
|
||||
|
||||
@@ -41,6 +42,13 @@
|
||||
>
|
||||
썸네일
|
||||
</MenuEntryButton>
|
||||
<MenuEntryButton
|
||||
onclick={() => goto("/settings/migration")}
|
||||
icon={IconLockReset}
|
||||
iconColor="text-teal-500"
|
||||
>
|
||||
암호화 마이그레이션
|
||||
</MenuEntryButton>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<p class="font-semibold">보안</p>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { error, text } from "@sveltejs/kit";
|
||||
import { Readable } from "stream";
|
||||
import type { ReadableStream } from "stream/web";
|
||||
import { z } from "zod";
|
||||
import { parseContentDigestHeader } from "$lib/modules/http";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { uploadChunk } from "$lib/server/services/upload";
|
||||
import type { RequestHandler } from "./$types";
|
||||
@@ -18,7 +19,10 @@ export const POST: RequestHandler = async ({ locals, params, request }) => {
|
||||
if (!zodRes.success) error(400, "Invalid path parameters");
|
||||
const { id: sessionId, index: chunkIndex } = zodRes.data;
|
||||
|
||||
if (!request.body) {
|
||||
const encContentHash = parseContentDigestHeader(request.headers.get("Content-Digest"));
|
||||
if (!encContentHash) {
|
||||
error(400, "Invalid request headers");
|
||||
} else if (!request.body) {
|
||||
error(400, "Invalid request body");
|
||||
}
|
||||
|
||||
@@ -27,6 +31,7 @@ export const POST: RequestHandler = async ({ locals, params, request }) => {
|
||||
sessionId,
|
||||
chunkIndex,
|
||||
Readable.fromWeb(request.body as ReadableStream),
|
||||
encContentHash,
|
||||
);
|
||||
return text("Chunk uploaded", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
|
||||
@@ -62,7 +62,9 @@ const streamFromServer = async (
|
||||
) => {
|
||||
const totalSize = getDecryptedSize(metadata.encContentSize, metadata.isLegacy);
|
||||
const start = range?.start ?? 0;
|
||||
const end = range?.end ?? totalSize - 1;
|
||||
const end =
|
||||
range?.end ??
|
||||
(range && !metadata.isLegacy ? Math.min(start + CHUNK_SIZE, totalSize) : totalSize) - 1;
|
||||
if (start > end || start < 0 || end >= totalSize) {
|
||||
return new Response("Invalid range", { status: 416 });
|
||||
}
|
||||
|
||||
@@ -6,11 +6,9 @@ import {
|
||||
categoryRouter,
|
||||
clientRouter,
|
||||
directoryRouter,
|
||||
favoritesRouter,
|
||||
fileRouter,
|
||||
hskRouter,
|
||||
mekRouter,
|
||||
searchRouter,
|
||||
uploadRouter,
|
||||
userRouter,
|
||||
} from "./routers";
|
||||
@@ -20,11 +18,9 @@ export const appRouter = router({
|
||||
category: categoryRouter,
|
||||
client: clientRouter,
|
||||
directory: directoryRouter,
|
||||
favorites: favoritesRouter,
|
||||
file: fileRouter,
|
||||
hsk: hskRouter,
|
||||
mek: mekRouter,
|
||||
search: searchRouter,
|
||||
upload: uploadRouter,
|
||||
user: userRouter,
|
||||
});
|
||||
|
||||
@@ -57,7 +57,6 @@ const categoryRouter = router({
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
isRecursive: file.isRecursive,
|
||||
})),
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { DirectoryIdSchema } from "$lib/schemas";
|
||||
import { DirectoryRepo, FileRepo, IntegrityError } from "$lib/server/db";
|
||||
import { FileRepo, IntegrityError } from "$lib/server/db";
|
||||
import { safeUnlink } from "$lib/server/modules/filesystem";
|
||||
import { router, roleProcedure } from "../init.server";
|
||||
|
||||
@@ -14,15 +14,13 @@ const directoryRouter = router({
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const directory =
|
||||
input.id !== "root"
|
||||
? await DirectoryRepo.getDirectory(ctx.session.userId, input.id)
|
||||
: undefined;
|
||||
input.id !== "root" ? await FileRepo.getDirectory(ctx.session.userId, input.id) : undefined;
|
||||
if (directory === null) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid directory id" });
|
||||
}
|
||||
|
||||
const [directories, files] = await Promise.all([
|
||||
DirectoryRepo.getAllDirectoriesByParent(ctx.session.userId, input.id),
|
||||
FileRepo.getAllDirectoriesByParent(ctx.session.userId, input.id),
|
||||
FileRepo.getAllFilesByParent(ctx.session.userId, input.id),
|
||||
]);
|
||||
return {
|
||||
@@ -33,7 +31,6 @@ const directoryRouter = router({
|
||||
dekVersion: directory.dekVersion,
|
||||
name: directory.encName.ciphertext,
|
||||
nameIv: directory.encName.iv,
|
||||
isFavorite: directory.isFavorite,
|
||||
},
|
||||
subDirectories: directories.map((directory) => ({
|
||||
id: directory.id,
|
||||
@@ -42,7 +39,6 @@ const directoryRouter = router({
|
||||
dekVersion: directory.dekVersion,
|
||||
name: directory.encName.ciphertext,
|
||||
nameIv: directory.encName.iv,
|
||||
isFavorite: directory.isFavorite,
|
||||
})),
|
||||
files: files.map((file) => ({
|
||||
id: file.id,
|
||||
@@ -56,7 +52,6 @@ const directoryRouter = router({
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
})),
|
||||
};
|
||||
}),
|
||||
@@ -80,7 +75,7 @@ const directoryRouter = router({
|
||||
}
|
||||
|
||||
try {
|
||||
await DirectoryRepo.registerDirectory({
|
||||
await FileRepo.registerDirectory({
|
||||
parentId: input.parent,
|
||||
userId: ctx.session.userId,
|
||||
mekVersion: input.mekVersion,
|
||||
@@ -107,7 +102,7 @@ const directoryRouter = router({
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
await DirectoryRepo.setDirectoryEncName(ctx.session.userId, input.id, input.dekVersion, {
|
||||
await FileRepo.setDirectoryEncName(ctx.session.userId, input.id, input.dekVersion, {
|
||||
ciphertext: input.name,
|
||||
iv: input.nameIv,
|
||||
});
|
||||
@@ -131,7 +126,7 @@ const directoryRouter = router({
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
const files = await DirectoryRepo.unregisterDirectory(ctx.session.userId, input.id);
|
||||
const files = await FileRepo.unregisterDirectory(ctx.session.userId, input.id);
|
||||
return {
|
||||
deletedFiles: files.map((file) => {
|
||||
safeUnlink(file.path); // Intended
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { DirectoryRepo, FileRepo, IntegrityError } from "$lib/server/db";
|
||||
import { router, roleProcedure } from "../init.server";
|
||||
|
||||
const favoritesRouter = router({
|
||||
get: roleProcedure["activeClient"].query(async ({ ctx }) => {
|
||||
const [directories, files] = await Promise.all([
|
||||
DirectoryRepo.getAllFavoriteDirectories(ctx.session.userId),
|
||||
FileRepo.getAllFavoriteFiles(ctx.session.userId),
|
||||
]);
|
||||
return {
|
||||
directories: directories.map((directory) => ({
|
||||
id: directory.id,
|
||||
parent: directory.parentId,
|
||||
mekVersion: directory.mekVersion,
|
||||
dek: directory.encDek,
|
||||
dekVersion: directory.dekVersion,
|
||||
name: directory.encName.ciphertext,
|
||||
nameIv: directory.encName.iv,
|
||||
})),
|
||||
files: files.map((file) => ({
|
||||
id: file.id,
|
||||
parent: file.parentId,
|
||||
mekVersion: file.mekVersion,
|
||||
dek: file.encDek,
|
||||
dekVersion: file.dekVersion,
|
||||
contentType: file.contentType,
|
||||
name: file.encName.ciphertext,
|
||||
nameIv: file.encName.iv,
|
||||
createdAt: file.encCreatedAt?.ciphertext,
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
})),
|
||||
};
|
||||
}),
|
||||
|
||||
addDirectory: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
id: z.int().positive(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
await DirectoryRepo.setDirectoryFavorite(ctx.session.userId, input.id, true);
|
||||
} catch (e) {
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "Directory not found") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid directory id" });
|
||||
} else if (e.message === "Directory already favorited") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: e.message });
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}),
|
||||
|
||||
removeDirectory: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
id: z.int().positive(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
await DirectoryRepo.setDirectoryFavorite(ctx.session.userId, input.id, false);
|
||||
} catch (e) {
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "Directory not found") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid directory id" });
|
||||
} else if (e.message === "Directory not favorited") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: e.message });
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}),
|
||||
|
||||
addFile: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
id: z.int().positive(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
await FileRepo.setFileFavorite(ctx.session.userId, input.id, true);
|
||||
} catch (e) {
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "File not found") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid file id" });
|
||||
} else if (e.message === "File already favorited") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: e.message });
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}),
|
||||
|
||||
removeFile: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
id: z.int().positive(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
try {
|
||||
await FileRepo.setFileFavorite(ctx.session.userId, input.id, false);
|
||||
} catch (e) {
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "File not found") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid file id" });
|
||||
} else if (e.message === "File not favorited") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: e.message });
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
export default favoritesRouter;
|
||||
@@ -31,7 +31,6 @@ const fileRouter = router({
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
categories: categories.map((category) => ({
|
||||
id: category.id,
|
||||
parent: category.parentId,
|
||||
@@ -66,7 +65,6 @@ const fileRouter = router({
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
categories: file.categories.map((category) => ({
|
||||
id: category.id,
|
||||
parent: category.parentId,
|
||||
@@ -99,23 +97,11 @@ const fileRouter = router({
|
||||
}),
|
||||
|
||||
listWithoutThumbnail: roleProcedure["activeClient"].query(async ({ ctx }) => {
|
||||
const files = await FileRepo.getFilesWithoutThumbnail(ctx.session.userId);
|
||||
return files.map((file) => ({
|
||||
id: file.id,
|
||||
isLegacy: !!file.encContentIv,
|
||||
parent: file.parentId,
|
||||
mekVersion: file.mekVersion,
|
||||
dek: file.encDek,
|
||||
dekVersion: file.dekVersion,
|
||||
contentType: file.contentType,
|
||||
name: file.encName.ciphertext,
|
||||
nameIv: file.encName.iv,
|
||||
createdAt: file.encCreatedAt?.ciphertext,
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
}));
|
||||
return await MediaRepo.getMissingFileThumbnails(ctx.session.userId);
|
||||
}),
|
||||
|
||||
listLegacy: roleProcedure["activeClient"].query(async ({ ctx }) => {
|
||||
return await FileRepo.getLegacyFileIds(ctx.session.userId);
|
||||
}),
|
||||
|
||||
rename: roleProcedure["activeClient"]
|
||||
|
||||
@@ -2,10 +2,8 @@ export { default as authRouter } from "./auth";
|
||||
export { default as categoryRouter } from "./category";
|
||||
export { default as clientRouter } from "./client";
|
||||
export { default as directoryRouter } from "./directory";
|
||||
export { default as favoritesRouter } from "./favorites";
|
||||
export { default as fileRouter } from "./file";
|
||||
export { default as hskRouter } from "./hsk";
|
||||
export { default as mekRouter } from "./mek";
|
||||
export { default as searchRouter } from "./search";
|
||||
export { default as uploadRouter } from "./upload";
|
||||
export { default as userRouter } from "./user";
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import { z } from "zod";
|
||||
import { DirectoryIdSchema } from "$lib/schemas";
|
||||
import { DirectoryRepo, FileRepo } from "$lib/server/db";
|
||||
import { router, roleProcedure } from "../init.server";
|
||||
|
||||
const searchRouter = router({
|
||||
search: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
ancestor: DirectoryIdSchema.default("root"),
|
||||
inFavorites: z.boolean().default(false),
|
||||
includeCategories: z.number().positive().array().default([]),
|
||||
excludeCategories: z.number().positive().array().default([]),
|
||||
}),
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const [directories, files] = await Promise.all([
|
||||
input.includeCategories.length === 0 && input.excludeCategories.length === 0
|
||||
? DirectoryRepo.searchDirectories(ctx.session.userId, {
|
||||
parentId: input.ancestor,
|
||||
inFavorites: input.inFavorites,
|
||||
})
|
||||
: [],
|
||||
FileRepo.searchFiles(ctx.session.userId, {
|
||||
parentId: input.ancestor,
|
||||
inFavorites: input.inFavorites,
|
||||
includeCategoryIds: input.includeCategories,
|
||||
excludeCategoryIds: input.excludeCategories,
|
||||
}),
|
||||
]);
|
||||
return {
|
||||
directories: directories.map((directory) => ({
|
||||
id: directory.id,
|
||||
parent: directory.parentId,
|
||||
mekVersion: directory.mekVersion,
|
||||
dek: directory.encDek,
|
||||
dekVersion: directory.dekVersion,
|
||||
name: directory.encName.ciphertext,
|
||||
nameIv: directory.encName.iv,
|
||||
isFavorite: directory.isFavorite,
|
||||
})),
|
||||
files: files.map((file) => ({
|
||||
id: file.id,
|
||||
parent: file.parentId,
|
||||
mekVersion: file.mekVersion,
|
||||
dek: file.encDek,
|
||||
dekVersion: file.dekVersion,
|
||||
contentType: file.contentType,
|
||||
name: file.encName.ciphertext,
|
||||
nameIv: file.encName.iv,
|
||||
createdAt: file.encCreatedAt?.ciphertext,
|
||||
createdAtIv: file.encCreatedAt?.iv,
|
||||
lastModifiedAt: file.encLastModifiedAt.ciphertext,
|
||||
lastModifiedAtIv: file.encLastModifiedAt.iv,
|
||||
isFavorite: file.isFavorite,
|
||||
})),
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
export default searchRouter;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { createHash } from "crypto";
|
||||
import { createReadStream } from "fs";
|
||||
import { mkdir, open } from "fs/promises";
|
||||
import { createReadStream, createWriteStream } from "fs";
|
||||
import { copyFile, mkdir } from "fs/promises";
|
||||
import mime from "mime";
|
||||
import { dirname } from "path";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
@@ -10,30 +10,17 @@ import { DirectoryIdSchema } from "$lib/schemas";
|
||||
import { FileRepo, MediaRepo, UploadRepo, IntegrityError } from "$lib/server/db";
|
||||
import db from "$lib/server/db/kysely";
|
||||
import env from "$lib/server/loadenv";
|
||||
import { safeUnlink } from "$lib/server/modules/filesystem";
|
||||
import { safeRecursiveRm, safeUnlink } from "$lib/server/modules/filesystem";
|
||||
import { router, roleProcedure } from "../init.server";
|
||||
|
||||
const UPLOADS_EXPIRES = 24 * 3600 * 1000; // 24 hours
|
||||
|
||||
const sessionLocks = new Set<string>();
|
||||
|
||||
const reserveUploadPath = async (path: string) => {
|
||||
await mkdir(dirname(path), { recursive: true });
|
||||
const file = await open(path, "wx", 0o600);
|
||||
await file.close();
|
||||
};
|
||||
|
||||
const generateFileUploadSession = async (userId: number) => {
|
||||
const generateSessionId = async () => {
|
||||
const id = uuidv4();
|
||||
const path = `${env.libraryPath}/${userId}/${uuidv4()}`;
|
||||
await reserveUploadPath(path);
|
||||
return { id, path };
|
||||
};
|
||||
|
||||
const generateThumbnailUploadSession = async (userId: number) => {
|
||||
const id = uuidv4();
|
||||
const path = `${env.thumbnailsPath}/${userId}/${id}`;
|
||||
await reserveUploadPath(path);
|
||||
const path = `${env.uploadsPath}/${id}`;
|
||||
await mkdir(path, { recursive: true });
|
||||
return { id, path };
|
||||
};
|
||||
|
||||
@@ -67,7 +54,7 @@ const uploadRouter = router({
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Invalid DEK version" });
|
||||
}
|
||||
|
||||
const { id, path } = await generateFileUploadSession(ctx.session.userId);
|
||||
const { id, path } = await generateSessionId();
|
||||
|
||||
try {
|
||||
await UploadRepo.createFileUploadSession({
|
||||
@@ -91,7 +78,7 @@ const uploadRouter = router({
|
||||
});
|
||||
return { uploadId: id };
|
||||
} catch (e) {
|
||||
await safeUnlink(path);
|
||||
await safeRecursiveRm(path);
|
||||
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "Inactive MEK version") {
|
||||
@@ -109,7 +96,6 @@ const uploadRouter = router({
|
||||
z.object({
|
||||
uploadId: z.uuidv4(),
|
||||
contentHmac: z.base64().nonempty().optional(),
|
||||
encContentHash: z.base64().nonempty(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
@@ -120,6 +106,8 @@ const uploadRouter = router({
|
||||
sessionLocks.add(uploadId);
|
||||
}
|
||||
|
||||
let filePath = "";
|
||||
|
||||
try {
|
||||
const session = await UploadRepo.getUploadSession(uploadId, ctx.session.userId);
|
||||
if (session?.type !== "file") {
|
||||
@@ -133,24 +121,29 @@ const uploadRouter = router({
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Upload not completed" });
|
||||
}
|
||||
|
||||
const hashStream = createHash("sha256");
|
||||
filePath = `${env.libraryPath}/${ctx.session.userId}/${uuidv4()}`;
|
||||
await mkdir(dirname(filePath), { recursive: true });
|
||||
|
||||
for await (const chunk of createReadStream(session.path)) {
|
||||
hashStream.update(chunk);
|
||||
const hashStream = createHash("sha256");
|
||||
const writeStream = createWriteStream(filePath, { flags: "wx", mode: 0o600 });
|
||||
|
||||
for (let i = 1; i <= session.totalChunks; i++) {
|
||||
for await (const chunk of createReadStream(`${session.path}/${i}`)) {
|
||||
hashStream.update(chunk);
|
||||
writeStream.write(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writeStream.end((e: any) => (e ? reject(e) : resolve()));
|
||||
});
|
||||
|
||||
const hash = hashStream.digest("base64");
|
||||
if (hash !== input.encContentHash) {
|
||||
await UploadRepo.deleteUploadSession(db, uploadId);
|
||||
await safeUnlink(session.path);
|
||||
throw new TRPCError({ code: "CONFLICT", message: "Uploaded file corrupted" });
|
||||
}
|
||||
|
||||
const fileId = await db.transaction().execute(async (trx) => {
|
||||
const { id: fileId } = await FileRepo.registerFile(trx, {
|
||||
...session,
|
||||
userId: ctx.session.userId,
|
||||
path: session.path,
|
||||
path: filePath,
|
||||
contentHmac: input.contentHmac ?? null,
|
||||
encContentHash: hash,
|
||||
encContentIv: null,
|
||||
@@ -159,7 +152,11 @@ const uploadRouter = router({
|
||||
return fileId;
|
||||
});
|
||||
|
||||
await safeRecursiveRm(session.path);
|
||||
return { file: fileId };
|
||||
} catch (e) {
|
||||
await safeUnlink(filePath);
|
||||
throw e;
|
||||
} finally {
|
||||
sessionLocks.delete(uploadId);
|
||||
}
|
||||
@@ -173,10 +170,10 @@ const uploadRouter = router({
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { id, path } = await generateThumbnailUploadSession(ctx.session.userId);
|
||||
const { id, path } = await generateSessionId();
|
||||
|
||||
try {
|
||||
await UploadRepo.createThumbnailUploadSession({
|
||||
await UploadRepo.createThumbnailOrMigrationUploadSession({
|
||||
id,
|
||||
type: "thumbnail",
|
||||
userId: ctx.session.userId,
|
||||
@@ -188,7 +185,7 @@ const uploadRouter = router({
|
||||
});
|
||||
return { uploadId: id };
|
||||
} catch (e) {
|
||||
await safeUnlink(path);
|
||||
await safeRecursiveRm(path);
|
||||
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "File not found") {
|
||||
@@ -215,6 +212,8 @@ const uploadRouter = router({
|
||||
sessionLocks.add(uploadId);
|
||||
}
|
||||
|
||||
let thumbnailPath = "";
|
||||
|
||||
try {
|
||||
const session = await UploadRepo.getUploadSession(uploadId, ctx.session.userId);
|
||||
if (session?.type !== "thumbnail") {
|
||||
@@ -223,20 +222,26 @@ const uploadRouter = router({
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Upload not completed" });
|
||||
}
|
||||
|
||||
thumbnailPath = `${env.thumbnailsPath}/${ctx.session.userId}/${uploadId}`;
|
||||
await mkdir(dirname(thumbnailPath), { recursive: true });
|
||||
await copyFile(`${session.path}/1`, thumbnailPath);
|
||||
|
||||
const oldThumbnailPath = await db.transaction().execute(async (trx) => {
|
||||
const oldPath = await MediaRepo.updateFileThumbnail(
|
||||
trx,
|
||||
ctx.session.userId,
|
||||
session.fileId,
|
||||
session.dekVersion,
|
||||
session.path,
|
||||
thumbnailPath,
|
||||
null,
|
||||
);
|
||||
await UploadRepo.deleteUploadSession(trx, uploadId);
|
||||
return oldPath;
|
||||
});
|
||||
await safeUnlink(oldThumbnailPath);
|
||||
await Promise.all([safeUnlink(oldThumbnailPath), safeRecursiveRm(session.path)]);
|
||||
} catch (e) {
|
||||
await safeUnlink(thumbnailPath);
|
||||
|
||||
if (e instanceof IntegrityError && e.message === "Invalid DEK version") {
|
||||
// DEK rotated after this upload started
|
||||
throw new TRPCError({ code: "CONFLICT", message: e.message });
|
||||
@@ -246,6 +251,112 @@ const uploadRouter = router({
|
||||
sessionLocks.delete(uploadId);
|
||||
}
|
||||
}),
|
||||
|
||||
startMigrationUpload: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
file: z.int().positive(),
|
||||
chunks: z.int().positive(),
|
||||
dekVersion: z.date(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { id, path } = await generateSessionId();
|
||||
|
||||
try {
|
||||
await UploadRepo.createThumbnailOrMigrationUploadSession({
|
||||
id,
|
||||
type: "migration",
|
||||
userId: ctx.session.userId,
|
||||
path,
|
||||
totalChunks: input.chunks,
|
||||
expiresAt: new Date(Date.now() + UPLOADS_EXPIRES),
|
||||
fileId: input.file,
|
||||
dekVersion: input.dekVersion,
|
||||
});
|
||||
return { uploadId: id };
|
||||
} catch (e) {
|
||||
await safeRecursiveRm(path);
|
||||
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "File not found") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid file id" });
|
||||
} else if (e.message === "File is not legacy") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: e.message });
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}),
|
||||
|
||||
completeMigrationUpload: roleProcedure["activeClient"]
|
||||
.input(
|
||||
z.object({
|
||||
uploadId: z.uuidv4(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { uploadId } = input;
|
||||
if (sessionLocks.has(uploadId)) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: "Completion already in progress" });
|
||||
} else {
|
||||
sessionLocks.add(uploadId);
|
||||
}
|
||||
|
||||
let filePath = "";
|
||||
|
||||
try {
|
||||
const session = await UploadRepo.getUploadSession(uploadId, ctx.session.userId);
|
||||
if (session?.type !== "migration") {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Invalid upload id" });
|
||||
} else if (session.uploadedChunks < session.totalChunks) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Upload not completed" });
|
||||
}
|
||||
|
||||
filePath = `${env.libraryPath}/${ctx.session.userId}/${uuidv4()}`;
|
||||
await mkdir(dirname(filePath), { recursive: true });
|
||||
|
||||
const hashStream = createHash("sha256");
|
||||
const writeStream = createWriteStream(filePath, { flags: "wx", mode: 0o600 });
|
||||
|
||||
for (let i = 1; i <= session.totalChunks; i++) {
|
||||
for await (const chunk of createReadStream(`${session.path}/${i}`)) {
|
||||
hashStream.update(chunk);
|
||||
writeStream.write(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writeStream.end((e: any) => (e ? reject(e) : resolve()));
|
||||
});
|
||||
|
||||
const hash = hashStream.digest("base64");
|
||||
const oldPath = await db.transaction().execute(async (trx) => {
|
||||
const { oldPath } = await FileRepo.migrateFileContent(
|
||||
trx,
|
||||
ctx.session.userId,
|
||||
session.fileId,
|
||||
filePath,
|
||||
session.dekVersion!,
|
||||
hash,
|
||||
);
|
||||
await UploadRepo.deleteUploadSession(trx, uploadId);
|
||||
return oldPath;
|
||||
});
|
||||
|
||||
await Promise.all([safeUnlink(oldPath), safeRecursiveRm(session.path)]);
|
||||
} catch (e) {
|
||||
await safeUnlink(filePath);
|
||||
|
||||
if (e instanceof IntegrityError && e.message === "File is not legacy") {
|
||||
// File migrated after this upload started
|
||||
throw new TRPCError({ code: "CONFLICT", message: e.message });
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
sessionLocks.delete(uploadId);
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
export default uploadRouter;
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
User-agent: *
|
||||
Disallow: /
|
||||
Reference in New Issue
Block a user