38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import { createError, defineEventHandler, getRouterParam } from "h3";
|
|
import { requireAuth } from "../../../utils/auth";
|
|
import { dropStore, DropStoreError } from "../../../utils/drop-store";
|
|
import { readBounded, requireContentType } from "../../../utils/http";
|
|
import { validateManifestBodyLength } from "../../../../shared/utils/drop-contract";
|
|
export default defineEventHandler(async (event) => {
|
|
requireAuth(event);
|
|
requireContentType(event, "application/octet-stream");
|
|
const body = await readBounded(event, 65552);
|
|
try {
|
|
validateManifestBodyLength(body.byteLength);
|
|
} catch {
|
|
throw createError({ statusCode: 400, statusMessage: "Invalid manifest." });
|
|
}
|
|
const id = String(getRouterParam(event, "id") || "").toUpperCase();
|
|
try {
|
|
const result = await dropStore.finalize(id, body);
|
|
return {
|
|
id,
|
|
expiresAt: new Date(result.expiresAt!).toISOString(),
|
|
maxReads: result.maxReads,
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof DropStoreError) {
|
|
const status =
|
|
error.code === "not-found"
|
|
? 404
|
|
: error.code === "expired"
|
|
? 410
|
|
: error.code === "incomplete"
|
|
? 409
|
|
: 409;
|
|
throw createError({ statusCode: status, statusMessage: error.message });
|
|
}
|
|
throw error;
|
|
}
|
|
});
|