Initial commit
This commit is contained in:
37
server/api/drops/[id]/finalize.post.ts
Normal file
37
server/api/drops/[id]/finalize.post.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user