/api/file/upload, /api/file/[id], /api/file/[id]/download Endpoint 구현

This commit is contained in:
static
2025-01-04 21:44:41 +09:00
parent 034593804f
commit a62d44038a
21 changed files with 298 additions and 11 deletions

View File

@@ -13,6 +13,17 @@ export interface NewDirectroyParams {
encNameIv: string;
}
export interface NewFileParams {
path: string;
parentId: DirectroyId;
userId: number;
mekVersion: number;
encDek: string;
encContentIv: string;
encName: string;
encNameIv: string;
}
export const registerNewDirectory = async (params: NewDirectroyParams) => {
return await db.transaction(async (tx) => {
const meks = await tx
@@ -58,6 +69,31 @@ export const getDirectory = async (userId: number, directoryId: number) => {
return res[0] ?? null;
};
export const registerNewFile = async (params: NewFileParams) => {
await db.transaction(async (tx) => {
const meks = await tx
.select()
.from(mek)
.where(and(eq(mek.userId, params.userId), eq(mek.state, "active")));
if (meks[0]?.version !== params.mekVersion) {
throw new Error("Invalid MEK version");
}
const now = new Date();
await tx.insert(file).values({
path: params.path,
parentId: params.parentId === "root" ? null : params.parentId,
createdAt: now,
userId: params.userId,
mekVersion: params.mekVersion,
encDek: params.encDek,
encryptedAt: now,
encContentIv: params.encContentIv,
encName: { ciphertext: params.encName, iv: params.encNameIv },
});
});
};
export const getAllFilesByParent = async (userId: number, parentId: DirectroyId) => {
return await db
.select()
@@ -70,3 +106,12 @@ export const getAllFilesByParent = async (userId: number, parentId: DirectroyId)
)
.execute();
};
export const getFile = async (userId: number, fileId: number) => {
const res = await db
.select()
.from(file)
.where(and(eq(file.userId, userId), eq(file.id, fileId)))
.execute();
return res[0] ?? null;
};

View File

@@ -47,6 +47,7 @@ export const file = sqliteTable(
mekVersion: integer("master_encryption_key_version").notNull(),
encDek: text("encrypted_data_encryption_key").notNull().unique(), // Base64
encryptedAt: integer("encrypted_at", { mode: "timestamp_ms" }).notNull(),
encContentIv: text("encrypted_content_iv").notNull(), // Base64
encName: ciphertext("encrypted_name").notNull(),
},
(t) => ({