mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 16:16:55 +00:00
DB에 청크 업로드 경로를 저장하도록 변경
This commit is contained in:
@@ -14,54 +14,53 @@ interface FileThumbnail extends Thumbnail {
|
||||
}
|
||||
|
||||
export const updateFileThumbnail = async (
|
||||
trx: typeof db,
|
||||
userId: number,
|
||||
fileId: number,
|
||||
dekVersion: Date,
|
||||
path: string,
|
||||
encContentIv: string | null,
|
||||
) => {
|
||||
return await db.transaction().execute(async (trx) => {
|
||||
const file = await trx
|
||||
.selectFrom("file")
|
||||
.select("data_encryption_key_version")
|
||||
.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");
|
||||
}
|
||||
const file = await trx
|
||||
.selectFrom("file")
|
||||
.select("data_encryption_key_version")
|
||||
.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");
|
||||
}
|
||||
|
||||
const thumbnail = await trx
|
||||
.selectFrom("thumbnail")
|
||||
.select("path as oldPath")
|
||||
.where("file_id", "=", fileId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
const now = new Date();
|
||||
const thumbnail = await trx
|
||||
.selectFrom("thumbnail")
|
||||
.select("path as oldPath")
|
||||
.where("file_id", "=", fileId)
|
||||
.limit(1)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
const now = new Date();
|
||||
|
||||
await trx
|
||||
.insertInto("thumbnail")
|
||||
.values({
|
||||
file_id: fileId,
|
||||
await trx
|
||||
.insertInto("thumbnail")
|
||||
.values({
|
||||
file_id: fileId,
|
||||
path,
|
||||
updated_at: now,
|
||||
encrypted_content_iv: encContentIv,
|
||||
})
|
||||
.onConflict((oc) =>
|
||||
oc.column("file_id").doUpdateSet({
|
||||
path,
|
||||
updated_at: now,
|
||||
encrypted_content_iv: encContentIv,
|
||||
})
|
||||
.onConflict((oc) =>
|
||||
oc.column("file_id").doUpdateSet({
|
||||
path,
|
||||
updated_at: now,
|
||||
encrypted_content_iv: encContentIv,
|
||||
}),
|
||||
)
|
||||
.execute();
|
||||
return thumbnail?.oldPath ?? null;
|
||||
});
|
||||
}),
|
||||
)
|
||||
.execute();
|
||||
return thumbnail?.oldPath ?? null;
|
||||
};
|
||||
|
||||
export const getFileThumbnail = async (userId: number, fileId: number) => {
|
||||
|
||||
@@ -17,9 +17,10 @@ export const up = async (db: Kysely<any>) => {
|
||||
// upload.ts
|
||||
await db.schema
|
||||
.createTable("upload_session")
|
||||
.addColumn("id", "uuid", (col) => col.primaryKey().defaultTo(sql`gen_random_uuid()`))
|
||||
.addColumn("id", "uuid", (col) => col.primaryKey())
|
||||
.addColumn("type", "text", (col) => col.notNull())
|
||||
.addColumn("user_id", "integer", (col) => col.references("user.id").notNull())
|
||||
.addColumn("path", "text", (col) => col.notNull())
|
||||
.addColumn("total_chunks", "integer", (col) => col.notNull())
|
||||
.addColumn("uploaded_chunks", sql`integer[]`, (col) => col.notNull().defaultTo(sql`'{}'`))
|
||||
.addColumn("expires_at", "timestamp(3)", (col) => col.notNull())
|
||||
|
||||
@@ -2,9 +2,10 @@ import type { Generated } from "kysely";
|
||||
import type { Ciphertext } from "./utils";
|
||||
|
||||
interface UploadSessionTable {
|
||||
id: Generated<string>;
|
||||
id: string;
|
||||
type: "file" | "thumbnail";
|
||||
user_id: number;
|
||||
path: string;
|
||||
total_chunks: number;
|
||||
uploaded_chunks: Generated<number[]>;
|
||||
expires_at: Date;
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { Ciphertext } from "./schema";
|
||||
interface BaseUploadSession {
|
||||
id: string;
|
||||
userId: number;
|
||||
path: string;
|
||||
totalChunks: number;
|
||||
uploadedChunks: number[];
|
||||
expiresAt: Date;
|
||||
@@ -31,9 +32,9 @@ interface ThumbnailUploadSession extends BaseUploadSession {
|
||||
}
|
||||
|
||||
export const createFileUploadSession = async (
|
||||
params: Omit<FileUploadSession, "id" | "type" | "uploadedChunks">,
|
||||
params: Omit<FileUploadSession, "type" | "uploadedChunks">,
|
||||
) => {
|
||||
return await db.transaction().execute(async (trx) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const mek = await trx
|
||||
.selectFrom("master_encryption_key")
|
||||
.select("version")
|
||||
@@ -60,11 +61,13 @@ export const createFileUploadSession = async (
|
||||
}
|
||||
}
|
||||
|
||||
const { sessionId } = await trx
|
||||
await trx
|
||||
.insertInto("upload_session")
|
||||
.values({
|
||||
id: params.id,
|
||||
type: "file",
|
||||
user_id: params.userId,
|
||||
path: params.path,
|
||||
total_chunks: params.totalChunks,
|
||||
expires_at: params.expiresAt,
|
||||
parent_id: params.parentId !== "root" ? params.parentId : null,
|
||||
@@ -77,16 +80,14 @@ export const createFileUploadSession = async (
|
||||
encrypted_created_at: params.encCreatedAt,
|
||||
encrypted_last_modified_at: params.encLastModifiedAt,
|
||||
})
|
||||
.returning("id as sessionId")
|
||||
.executeTakeFirstOrThrow();
|
||||
return { id: sessionId };
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
export const createThumbnailUploadSession = async (
|
||||
params: Omit<ThumbnailUploadSession, "id" | "type" | "uploadedChunks" | "totalChunks">,
|
||||
params: Omit<ThumbnailUploadSession, "type" | "uploadedChunks">,
|
||||
) => {
|
||||
return await db.transaction().execute(async (trx) => {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
const file = await trx
|
||||
.selectFrom("file")
|
||||
.select("data_encryption_key_version")
|
||||
@@ -101,19 +102,19 @@ export const createThumbnailUploadSession = async (
|
||||
throw new IntegrityError("Invalid DEK version");
|
||||
}
|
||||
|
||||
const { sessionId } = await trx
|
||||
await trx
|
||||
.insertInto("upload_session")
|
||||
.values({
|
||||
id: params.id,
|
||||
type: "thumbnail",
|
||||
user_id: params.userId,
|
||||
total_chunks: 1,
|
||||
path: params.path,
|
||||
total_chunks: params.totalChunks,
|
||||
expires_at: params.expiresAt,
|
||||
file_id: params.fileId,
|
||||
data_encryption_key_version: params.dekVersion,
|
||||
})
|
||||
.returning("id as sessionId")
|
||||
.executeTakeFirstOrThrow();
|
||||
return { id: sessionId };
|
||||
.execute();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -126,14 +127,14 @@ export const getUploadSession = async (sessionId: string, userId: number) => {
|
||||
.where("expires_at", ">", new Date())
|
||||
.limit(1)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!session) return null;
|
||||
|
||||
if (session.type === "file") {
|
||||
if (!session) {
|
||||
return null;
|
||||
} else if (session.type === "file") {
|
||||
return {
|
||||
type: "file",
|
||||
id: session.id,
|
||||
userId: session.user_id,
|
||||
path: session.path,
|
||||
totalChunks: session.total_chunks,
|
||||
uploadedChunks: session.uploaded_chunks,
|
||||
expiresAt: session.expires_at,
|
||||
@@ -152,6 +153,7 @@ export const getUploadSession = async (sessionId: string, userId: number) => {
|
||||
type: "thumbnail",
|
||||
id: session.id,
|
||||
userId: session.user_id,
|
||||
path: session.path,
|
||||
totalChunks: session.total_chunks,
|
||||
uploadedChunks: session.uploaded_chunks,
|
||||
expiresAt: session.expires_at,
|
||||
@@ -176,8 +178,8 @@ export const deleteUploadSession = async (trx: typeof db, sessionId: string) =>
|
||||
export const cleanupExpiredUploadSessions = async () => {
|
||||
const sessions = await db
|
||||
.deleteFrom("upload_session")
|
||||
.where("expires_at", "<", new Date())
|
||||
.returning("id")
|
||||
.where("expires_at", "<=", new Date())
|
||||
.returning("path")
|
||||
.execute();
|
||||
return sessions.map(({ id }) => id);
|
||||
return sessions.map(({ path }) => path);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user