196 lines
5.1 KiB
TypeScript
196 lines
5.1 KiB
TypeScript
import { Router } from "@oak/oak";
|
|
|
|
import {
|
|
APIErrorCode,
|
|
APIException,
|
|
type APIResponse,
|
|
type Dump,
|
|
isCreateUrlDumpRequest,
|
|
isUpdateDumpRequest,
|
|
type PaginatedData,
|
|
} from "../model/interfaces.ts";
|
|
|
|
import { authMiddleware } from "../middleware/auth.ts";
|
|
import { parseOptionalAuth } from "../lib/auth.ts";
|
|
import { parsePagination } from "../lib/pagination.ts";
|
|
import {
|
|
createFileDump,
|
|
createUrlDump,
|
|
deleteDump,
|
|
getDump,
|
|
listDumps,
|
|
refreshDumpMetadata,
|
|
replaceFileDump,
|
|
updateDump,
|
|
} from "../services/dump-service.ts";
|
|
|
|
const router = new Router({ prefix: "/api/dumps" });
|
|
|
|
router.post(
|
|
"/",
|
|
authMiddleware,
|
|
async (ctx) => {
|
|
const userId = ctx.state.user.userId;
|
|
const contentType = ctx.request.headers.get("content-type") ?? "";
|
|
|
|
let dump: Dump;
|
|
|
|
if (contentType.includes("multipart/form-data")) {
|
|
const formData = await ctx.request.body.formData();
|
|
const file = formData.get("file");
|
|
const comment = formData.get("comment");
|
|
const isPrivate = formData.get("isPrivate") === "true";
|
|
|
|
if (!(file instanceof File)) {
|
|
throw new APIException(
|
|
APIErrorCode.VALIDATION_ERROR,
|
|
400,
|
|
"A file is required",
|
|
);
|
|
}
|
|
|
|
dump = await createFileDump(
|
|
file,
|
|
typeof comment === "string" && comment ? comment : undefined,
|
|
userId,
|
|
isPrivate,
|
|
);
|
|
} else {
|
|
const body = await ctx.request.body.json();
|
|
|
|
if (!isCreateUrlDumpRequest(body)) {
|
|
throw new APIException(
|
|
APIErrorCode.VALIDATION_ERROR,
|
|
400,
|
|
"Invalid dump data",
|
|
);
|
|
}
|
|
|
|
dump = await createUrlDump(body, userId);
|
|
}
|
|
|
|
const responseBody: APIResponse<Dump> = { success: true, data: dump };
|
|
ctx.response.status = 201;
|
|
ctx.response.body = responseBody;
|
|
},
|
|
);
|
|
|
|
router.get("/:dumpId", async (ctx) => {
|
|
const requestingUserId = await parseOptionalAuth(ctx) ?? undefined;
|
|
const dump = getDump(ctx.params.dumpId, requestingUserId);
|
|
const responseBody: APIResponse<Dump> = { success: true, data: dump };
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/", async (ctx) => {
|
|
const requestingUserId = await parseOptionalAuth(ctx) ?? undefined;
|
|
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
|
const { items, total } = listDumps(page, limit, requestingUserId);
|
|
const responseBody: APIResponse<PaginatedData<Dump>> = {
|
|
success: true,
|
|
data: { items, total, hasMore: page * limit < total },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.put("/:dumpId/file", authMiddleware, async (ctx) => {
|
|
const dumpId = ctx.params.dumpId;
|
|
const userId = ctx.state.user?.userId;
|
|
|
|
const dump = getDump(dumpId, userId);
|
|
if (userId !== dump.userId) {
|
|
throw new APIException(
|
|
APIErrorCode.UNAUTHORIZED,
|
|
403,
|
|
"Not authorized to update dump",
|
|
);
|
|
}
|
|
|
|
const formData = await ctx.request.body.formData();
|
|
const file = formData.get("file");
|
|
const comment = formData.get("comment");
|
|
|
|
if (!(file instanceof File)) {
|
|
throw new APIException(
|
|
APIErrorCode.VALIDATION_ERROR,
|
|
400,
|
|
"A file is required",
|
|
);
|
|
}
|
|
|
|
const updatedDump = await replaceFileDump(
|
|
dumpId,
|
|
file,
|
|
typeof comment === "string" && comment ? comment : undefined,
|
|
);
|
|
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.put("/:dumpId", authMiddleware, async (ctx) => {
|
|
const dumpId = ctx.params.dumpId;
|
|
const userId = ctx.state.user?.userId;
|
|
const body = await ctx.request.body.json();
|
|
|
|
if (!isUpdateDumpRequest(body)) {
|
|
throw new APIException(
|
|
APIErrorCode.VALIDATION_ERROR,
|
|
422,
|
|
"Erroneous user input",
|
|
);
|
|
}
|
|
|
|
const dump = getDump(dumpId, userId);
|
|
|
|
if (userId !== dump.userId) {
|
|
throw new APIException(
|
|
APIErrorCode.UNAUTHORIZED,
|
|
403,
|
|
"Not authorized to update dump",
|
|
);
|
|
}
|
|
|
|
const updatedDump = await updateDump(dumpId, body);
|
|
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.post("/:dumpId/refresh-metadata", authMiddleware, async (ctx) => {
|
|
const dumpId = ctx.params.dumpId;
|
|
const userId = ctx.state.user?.userId;
|
|
const dump = getDump(dumpId, userId);
|
|
|
|
if (userId !== dump.userId) {
|
|
throw new APIException(
|
|
APIErrorCode.UNAUTHORIZED,
|
|
403,
|
|
"Not authorized to update dump",
|
|
);
|
|
}
|
|
|
|
const updatedDump = await refreshDumpMetadata(dumpId);
|
|
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.delete("/:dumpId", authMiddleware, async (ctx) => {
|
|
const dumpId = ctx.params.dumpId;
|
|
const userId = ctx.state.user?.userId;
|
|
const dump = getDump(dumpId, userId);
|
|
|
|
if (userId !== dump.userId) {
|
|
throw new APIException(
|
|
APIErrorCode.UNAUTHORIZED,
|
|
403,
|
|
"Not authorized to delete dump",
|
|
);
|
|
}
|
|
|
|
await deleteDump(dumpId);
|
|
|
|
const responseBody: APIResponse<null> = { success: true, data: null };
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
export default router;
|