파일 업로드 방식을 Chunking 방식으로 변경

This commit is contained in:
static committed 2026-01-11 04:45:21 +09:00
1 parent b9e6f17b0c
commit 4b783a36e9
30 files changed
+667 -315

No files matched your search

@@ -95,7 +95,7 @@
untrack(() => {
if (!downloadState && !isDownloadRequested) {
isDownloadRequested = true;
requestFileDownload(data.id, info!.dataKey!.key).then(async (buffer) => {
requestFileDownload(data.id, info!.dataKey!.key, info!.isLegacy!).then(async (buffer) => {
const blob = await updateViewer(buffer, contentType);
if (!viewerType) {
FileSaver.saveAs(blob, info!.name);
@@ -50,7 +50,7 @@ const requestThumbnailUpload = limitFunction(
async (
fileId: number,
dataKeyVersion: Date,
thumbnail: { plaintext: ArrayBuffer; ciphertext: ArrayBuffer; iv: string },
thumbnail: { plaintext: ArrayBuffer; ciphertext: ArrayBuffer; iv: ArrayBuffer },
) => {
statuses.set(fileId, "uploading");
@@ -77,7 +77,7 @@ export const requestThumbnailGeneration = async (fileInfo: FileInfo) => {
await scheduler.schedule(
async () => {
statuses.set(fileInfo.id, "generation-pending");
file = await requestFileDownload(fileInfo.id, fileInfo.dataKey?.key!);
file = await requestFileDownload(fileInfo.id, fileInfo.dataKey?.key!, fileInfo.isLegacy!);
return file.byteLength;
},
async () => {
-108
View File
@@ -1,108 +0,0 @@
import Busboy from "@fastify/busboy";
import { error, json } from "@sveltejs/kit";
import { Readable, Writable } from "stream";
import { authorize } from "$lib/server/modules/auth";
import {
fileUploadRequest,
fileUploadResponse,
type FileUploadResponse,
} from "$lib/server/schemas";
import { uploadFile } from "$lib/server/services/file";
import type { RequestHandler } from "./$types";
type FileMetadata = Parameters<typeof uploadFile>[0];
const parseFileMetadata = (userId: number, json: string) => {
const zodRes = fileUploadRequest.safeParse(JSON.parse(json));
if (!zodRes.success) error(400, "Invalid request body");
const {
parent,
mekVersion,
dek,
dekVersion,
hskVersion,
contentHmac,
contentType,
contentIv,
name,
nameIv,
createdAt,
createdAtIv,
lastModifiedAt,
lastModifiedAtIv,
} = zodRes.data;
if ((createdAt && !createdAtIv) || (!createdAt && createdAtIv))
error(400, "Invalid request body");
return {
userId,
parentId: parent,
mekVersion,
encDek: dek,
dekVersion: new Date(dekVersion),
hskVersion,
contentHmac,
contentType,
encContentIv: contentIv,
encName: { ciphertext: name, iv: nameIv },
encCreatedAt: createdAt && createdAtIv ? { ciphertext: createdAt, iv: createdAtIv } : null,
encLastModifiedAt: { ciphertext: lastModifiedAt, iv: lastModifiedAtIv },
} satisfies FileMetadata;
};
export const POST: RequestHandler = async ({ locals, request }) => {
const { userId } = await authorize(locals, "activeClient");
const contentType = request.headers.get("Content-Type");
if (!contentType?.startsWith("multipart/form-data") || !request.body) {
error(400, "Invalid request body");
}
return new Promise<Response>((resolve, reject) => {
const bb = Busboy({ headers: { "content-type": contentType } });
const handler =
<T extends unknown[]>(f: (...args: T) => Promise<void>) =>
(...args: T) => {
f(...args).catch(reject);
};
let metadata: FileMetadata | null = null;
let content: Readable | null = null;
const checksum = new Promise<string>((resolveChecksum, rejectChecksum) => {
bb.on(
"field",
handler(async (fieldname, val) => {
if (fieldname === "metadata") {
// Ignore subsequent metadata fields
if (!metadata) {
metadata = parseFileMetadata(userId, val);
}
} else if (fieldname === "checksum") {
// Ignore subsequent checksum fields
resolveChecksum(val);
} else {
error(400, "Invalid request body");
}
}),
);
bb.on(
"file",
handler(async (fieldname, file) => {
if (fieldname !== "content") error(400, "Invalid request body");
if (!metadata || content) error(400, "Invalid request body");
content = file;
const { fileId } = await uploadFile(metadata, content, checksum);
resolve(json(fileUploadResponse.parse({ file: fileId } satisfies FileUploadResponse)));
}),
);
bb.on("finish", () => rejectChecksum(new Error("Invalid request body")));
bb.on("error", (e) => {
content?.emit("error", e) ?? reject(e);
rejectChecksum(e);
});
});
request.body!.pipeTo(Writable.toWeb(bb)).catch(() => {}); // busboy will handle the error
});
};
@@ -0,0 +1,43 @@
import { error, text } from "@sveltejs/kit";
import { Readable } from "stream";
import { z } from "zod";
import { authorize } from "$lib/server/modules/auth";
import { uploadChunk } from "$lib/server/services/file";
import type { RequestHandler } from "./$types";
export const POST: RequestHandler = async ({ locals, params, request }) => {
const { userId } = await authorize(locals, "activeClient");
const zodRes = z
.object({
id: z.uuidv4(),
index: z.coerce.number().int().nonnegative(),
})
.safeParse(params);
if (!zodRes.success) error(400, "Invalid path parameters");
const { id: uploadId, index: chunkIndex } = zodRes.data;
// Parse Content-Digest header (RFC 9530)
// Expected format: sha-256=:base64hash:
const contentDigest = request.headers.get("Content-Digest");
if (!contentDigest) error(400, "Missing Content-Digest header");
const digestMatch = contentDigest.match(/^sha-256=:([A-Za-z0-9+/=]+):$/);
if (!digestMatch || !digestMatch[1])
error(400, "Invalid Content-Digest format, must be sha-256=:base64:");
const encChunkHash = digestMatch[1];
const contentType = request.headers.get("Content-Type");
if (contentType !== "application/octet-stream" || !request.body) {
error(400, "Invalid request body");
}
// Convert web ReadableStream to Node Readable
const nodeReadable = Readable.fromWeb(
request.body as unknown as Parameters<typeof Readable.fromWeb>[0],
);
await uploadChunk(userId, uploadId, chunkIndex, nodeReadable, encChunkHash);
return text("Chunk uploaded", { headers: { "Content-Type": "text/plain" } });
};