mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 08:06:56 +00:00
파일 및 썸네일 다운로드 Endpoint의 핸들러를 하나로 통합
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
export const parseRangeHeader = (rangeHeader: string | null) => {
|
export const parseRangeHeader = (value: string | null) => {
|
||||||
if (!rangeHeader) return undefined;
|
if (!value) return undefined;
|
||||||
|
|
||||||
const firstRange = rangeHeader.split(",")[0]!.trim();
|
const firstRange = value.split(",")[0]!.trim();
|
||||||
const parts = firstRange.replace(/bytes=/, "").split("-");
|
const parts = firstRange.replace(/bytes=/, "").split("-");
|
||||||
return {
|
return {
|
||||||
start: parts[0] ? parseInt(parts[0], 10) : undefined,
|
start: parts[0] ? parseInt(parts[0], 10) : undefined,
|
||||||
@@ -12,3 +12,11 @@ export const parseRangeHeader = (rangeHeader: string | null) => {
|
|||||||
export const getContentRangeHeader = (range?: { start: number; end: number; total: number }) => {
|
export const getContentRangeHeader = (range?: { start: number; end: number; total: number }) => {
|
||||||
return range && { "Content-Range": `bytes ${range.start}-${range.end}/${range.total}` };
|
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];
|
||||||
|
};
|
||||||
|
|||||||
5
src/params/thumbnail.ts
Normal file
5
src/params/thumbnail.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import type { ParamMatcher } from "@sveltejs/kit";
|
||||||
|
|
||||||
|
export const match: ParamMatcher = (param) => {
|
||||||
|
return param === "thumbnail";
|
||||||
|
};
|
||||||
@@ -2,14 +2,10 @@ import { error } from "@sveltejs/kit";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { parseRangeHeader, getContentRangeHeader } from "$lib/modules/http";
|
import { parseRangeHeader, getContentRangeHeader } from "$lib/modules/http";
|
||||||
import { authorize } from "$lib/server/modules/auth";
|
import { authorize } from "$lib/server/modules/auth";
|
||||||
import { getFileStream } from "$lib/server/services/file";
|
import { getFileStream, getFileThumbnailStream } from "$lib/server/services/file";
|
||||||
import type { RequestHandler } from "./$types";
|
import type { RequestHandler, RouteParams } from "./$types";
|
||||||
|
|
||||||
const downloadHandler = async (
|
const downloadHandler = async (locals: App.Locals, params: RouteParams, request: Request) => {
|
||||||
locals: App.Locals,
|
|
||||||
params: Record<string, string>,
|
|
||||||
request: Request,
|
|
||||||
) => {
|
|
||||||
const { userId } = await authorize(locals, "activeClient");
|
const { userId } = await authorize(locals, "activeClient");
|
||||||
|
|
||||||
const zodRes = z
|
const zodRes = z
|
||||||
@@ -20,29 +16,29 @@ const downloadHandler = async (
|
|||||||
if (!zodRes.success) error(400, "Invalid path parameters");
|
if (!zodRes.success) error(400, "Invalid path parameters");
|
||||||
const { id } = zodRes.data;
|
const { id } = zodRes.data;
|
||||||
|
|
||||||
const { encContentStream, range } = await getFileStream(
|
const getStream = params.thumbnail ? getFileThumbnailStream : getFileStream;
|
||||||
|
const { encContentStream, range } = await getStream(
|
||||||
userId,
|
userId,
|
||||||
id,
|
id,
|
||||||
parseRangeHeader(request.headers.get("Range")),
|
parseRangeHeader(request.headers.get("Range")),
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
stream: encContentStream,
|
stream: encContentStream,
|
||||||
|
status: range ? 206 : 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Accept-Ranges": "bytes",
|
"Accept-Ranges": "bytes",
|
||||||
"Content-Length": (range.end - range.start + 1).toString(),
|
"Content-Length": String(range.end - range.start + 1),
|
||||||
"Content-Type": "application/octet-stream",
|
"Content-Type": "application/octet-stream",
|
||||||
...getContentRangeHeader(range),
|
...getContentRangeHeader(range),
|
||||||
},
|
},
|
||||||
isRangeRequest: !!range,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ locals, params, request }) => {
|
export const GET: RequestHandler = async ({ locals, params, request }) => {
|
||||||
const { stream, headers, isRangeRequest } = await downloadHandler(locals, params, request);
|
const { stream, ...init } = await downloadHandler(locals, params, request);
|
||||||
return new Response(stream as ReadableStream, { status: isRangeRequest ? 206 : 200, headers });
|
return new Response(stream as ReadableStream, init);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const HEAD: RequestHandler = async ({ locals, params, request }) => {
|
export const HEAD: RequestHandler = async ({ locals, params, request }) => {
|
||||||
const { headers, isRangeRequest } = await downloadHandler(locals, params, request);
|
return new Response(null, await downloadHandler(locals, params, request));
|
||||||
return new Response(null, { status: isRangeRequest ? 206 : 200, headers });
|
|
||||||
};
|
};
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
import { error } from "@sveltejs/kit";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { parseRangeHeader, getContentRangeHeader } from "$lib/modules/http";
|
|
||||||
import { authorize } from "$lib/server/modules/auth";
|
|
||||||
import { getFileThumbnailStream } from "$lib/server/services/file";
|
|
||||||
import type { RequestHandler } from "./$types";
|
|
||||||
|
|
||||||
const downloadHandler = async (
|
|
||||||
locals: App.Locals,
|
|
||||||
params: Record<string, string>,
|
|
||||||
request: Request,
|
|
||||||
) => {
|
|
||||||
const { userId } = await authorize(locals, "activeClient");
|
|
||||||
|
|
||||||
const zodRes = z
|
|
||||||
.object({
|
|
||||||
id: z.coerce.number().int().positive(),
|
|
||||||
})
|
|
||||||
.safeParse(params);
|
|
||||||
if (!zodRes.success) error(400, "Invalid path parameters");
|
|
||||||
const { id } = zodRes.data;
|
|
||||||
|
|
||||||
const { encContentStream, range } = await getFileThumbnailStream(
|
|
||||||
userId,
|
|
||||||
id,
|
|
||||||
parseRangeHeader(request.headers.get("Range")),
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
stream: encContentStream,
|
|
||||||
headers: {
|
|
||||||
"Accept-Ranges": "bytes",
|
|
||||||
"Content-Length": (range.end - range.start + 1).toString(),
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
...getContentRangeHeader(range),
|
|
||||||
},
|
|
||||||
isRangeRequest: !!range,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ locals, params, request }) => {
|
|
||||||
const { stream, headers, isRangeRequest } = await downloadHandler(locals, params, request);
|
|
||||||
return new Response(stream as ReadableStream, { status: isRangeRequest ? 206 : 200, headers });
|
|
||||||
};
|
|
||||||
|
|
||||||
export const HEAD: RequestHandler = async ({ locals, params, request }) => {
|
|
||||||
const { headers, isRangeRequest } = await downloadHandler(locals, params, request);
|
|
||||||
return new Response(null, { status: isRangeRequest ? 206 : 200, headers });
|
|
||||||
};
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import { error, text } from "@sveltejs/kit";
|
import { error, text } from "@sveltejs/kit";
|
||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
|
import { ReadableStream } from "stream/web";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { parseContentDigestHeader } from "$lib/modules/http";
|
||||||
import { authorize } from "$lib/server/modules/auth";
|
import { authorize } from "$lib/server/modules/auth";
|
||||||
import { uploadChunk } from "$lib/server/services/upload";
|
import { uploadChunk } from "$lib/server/services/upload";
|
||||||
import type { RequestHandler } from "./$types";
|
import type { RequestHandler } from "./$types";
|
||||||
@@ -15,29 +17,21 @@ export const POST: RequestHandler = async ({ locals, params, request }) => {
|
|||||||
})
|
})
|
||||||
.safeParse(params);
|
.safeParse(params);
|
||||||
if (!zodRes.success) error(400, "Invalid path parameters");
|
if (!zodRes.success) error(400, "Invalid path parameters");
|
||||||
const { id: uploadId, index: chunkIndex } = zodRes.data;
|
const { id: sessionId, index: chunkIndex } = zodRes.data;
|
||||||
|
|
||||||
// Parse Content-Digest header (RFC 9530)
|
const encContentHash = parseContentDigestHeader(request.headers.get("Content-Digest"));
|
||||||
// Expected format: sha-256=:base64hash:
|
if (!encContentHash) {
|
||||||
const contentDigest = request.headers.get("Content-Digest");
|
error(400, "Invalid request headers");
|
||||||
if (!contentDigest) error(400, "Missing Content-Digest header");
|
} else if (!request.body) {
|
||||||
|
|
||||||
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");
|
error(400, "Invalid request body");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert web ReadableStream to Node Readable
|
await uploadChunk(
|
||||||
const nodeReadable = Readable.fromWeb(
|
userId,
|
||||||
request.body as unknown as Parameters<typeof Readable.fromWeb>[0],
|
sessionId,
|
||||||
|
chunkIndex,
|
||||||
|
Readable.fromWeb(request.body as ReadableStream),
|
||||||
|
encContentHash,
|
||||||
);
|
);
|
||||||
|
|
||||||
await uploadChunk(userId, uploadId, chunkIndex, nodeReadable, encChunkHash);
|
|
||||||
|
|
||||||
return text("Chunk uploaded", { headers: { "Content-Type": "text/plain" } });
|
return text("Chunk uploaded", { headers: { "Content-Type": "text/plain" } });
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user