mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 15:08:46 +00:00
카테고리 페이지에 파일 목록 부분 구현
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
} from "$lib/indexedDB";
|
||||
import { unwrapDataKey, decryptString } from "$lib/modules/crypto";
|
||||
import type {
|
||||
CategoryFileListResponse,
|
||||
CategoryInfoResponse,
|
||||
DirectoryInfoResponse,
|
||||
FileInfoResponse,
|
||||
@@ -56,6 +57,7 @@ export type CategoryInfo =
|
||||
dataKeyVersion?: undefined;
|
||||
name?: undefined;
|
||||
subCategoryIds: number[];
|
||||
files?: undefined;
|
||||
}
|
||||
| {
|
||||
id: number;
|
||||
@@ -63,6 +65,7 @@ export type CategoryInfo =
|
||||
dataKeyVersion?: Date;
|
||||
name: string;
|
||||
subCategoryIds: number[];
|
||||
files: number[];
|
||||
};
|
||||
|
||||
const directoryInfoStore = new Map<DirectoryId, Writable<DirectoryInfo | null>>();
|
||||
@@ -235,7 +238,7 @@ const fetchCategoryInfoFromServer = async (
|
||||
info: Writable<CategoryInfo | null>,
|
||||
masterKey: CryptoKey,
|
||||
) => {
|
||||
const res = await callGetApi(`/api/category/${id}`);
|
||||
let res = await callGetApi(`/api/category/${id}`);
|
||||
if (res.status === 404) {
|
||||
info.set(null);
|
||||
return;
|
||||
@@ -251,12 +254,20 @@ const fetchCategoryInfoFromServer = async (
|
||||
const { dataKey } = await unwrapDataKey(metadata!.dek, masterKey);
|
||||
const name = await decryptString(metadata!.name, metadata!.nameIv, dataKey);
|
||||
|
||||
res = await callGetApi(`/api/category/${id}/file/list`);
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to fetch category files");
|
||||
}
|
||||
|
||||
const { files }: CategoryFileListResponse = await res.json();
|
||||
|
||||
info.set({
|
||||
id,
|
||||
dataKey,
|
||||
dataKeyVersion: new Date(metadata!.dekVersion),
|
||||
name,
|
||||
subCategoryIds: subCategories,
|
||||
files,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -17,6 +17,16 @@ export const categoryInfoResponse = z.object({
|
||||
});
|
||||
export type CategoryInfoResponse = z.infer<typeof categoryInfoResponse>;
|
||||
|
||||
export const categoryFileAddRequest = z.object({
|
||||
file: z.number().int().positive(),
|
||||
});
|
||||
export type CategoryFileAddRequest = z.infer<typeof categoryFileAddRequest>;
|
||||
|
||||
export const categoryFileListResponse = z.object({
|
||||
files: z.number().int().positive().array(),
|
||||
});
|
||||
export type CategoryFileListResponse = z.infer<typeof categoryFileListResponse>;
|
||||
|
||||
export const categoryCreateRequest = z.object({
|
||||
parent: categoryIdSchema,
|
||||
mekVersion: z.number().int().positive(),
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type NewCategory,
|
||||
} from "$lib/server/db/category";
|
||||
import { IntegrityError } from "$lib/server/db/error";
|
||||
import { getAllFilesByCategory, getFile, addFileToCategory } from "$lib/server/db/file";
|
||||
|
||||
export const getCategoryInformation = async (userId: number, categoryId: CategoryId) => {
|
||||
const category = categoryId !== "root" ? await getCategory(userId, categoryId) : undefined;
|
||||
@@ -27,6 +28,35 @@ export const getCategoryInformation = async (userId: number, categoryId: Categor
|
||||
};
|
||||
};
|
||||
|
||||
export const addCategoryFile = async (userId: number, categoryId: number, fileId: number) => {
|
||||
const category = await getCategory(userId, categoryId);
|
||||
const file = await getFile(userId, fileId);
|
||||
if (!category) {
|
||||
error(404, "Invalid category id");
|
||||
} else if (!file) {
|
||||
error(404, "Invalid file id");
|
||||
}
|
||||
|
||||
try {
|
||||
await addFileToCategory(fileId, categoryId);
|
||||
} catch (e) {
|
||||
if (e instanceof IntegrityError && e.message === "File already added to category") {
|
||||
error(400, "File already added");
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
export const getCategoryFiles = async (userId: number, categoryId: number) => {
|
||||
const category = await getCategory(userId, categoryId);
|
||||
if (!category) {
|
||||
error(404, "Invalid category id");
|
||||
}
|
||||
|
||||
const files = await getAllFilesByCategory(userId, categoryId);
|
||||
return { files: files.map(({ id }) => id) };
|
||||
};
|
||||
|
||||
export const createCategory = async (params: NewCategory) => {
|
||||
const oneMinuteAgo = new Date(Date.now() - 60 * 1000);
|
||||
const oneMinuteLater = new Date(Date.now() + 60 * 1000);
|
||||
|
||||
Reference in New Issue
Block a user