Unterlagen und weitere Fehler
This commit is contained in:
@@ -17,6 +17,9 @@ export const PUT = defineApiRoute({
|
||||
}).merge(z.object({
|
||||
nachweis_id: UUidWithPrefix
|
||||
})),
|
||||
output: z.object({
|
||||
id: UUidWithPrefix
|
||||
}),
|
||||
headers: authorizationHeaders,
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
|
||||
@@ -2,13 +2,13 @@ import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma } from "#lib/server/prisma.js";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "zod";
|
||||
import { fileURLToPath } from "url";
|
||||
import { writeFileSync } from "fs";
|
||||
import { PERSISTENT_DIR } from "#lib/server/constants.js";
|
||||
import { UnterlageSchema } from "src/generated/zod/unterlage.js";
|
||||
import { generatePrefixedId } from "#lib/db.js";
|
||||
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
|
||||
import { UUidWithPrefix } from "#components/Ausweis/types.js";
|
||||
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { s3Client } from "#lib/s3.js";
|
||||
import mime from "mime"
|
||||
|
||||
export const PUT = defineApiRoute({
|
||||
input: UnterlageSchema.omit({
|
||||
@@ -20,31 +20,37 @@ export const PUT = defineApiRoute({
|
||||
output: z.object({
|
||||
id: UUidWithPrefix
|
||||
}),
|
||||
async fetch({ data, name, kategorie, mime }, ctx, user) {
|
||||
if (mime !== "application/pdf" && mime !== "image/png" && mime !== "image/jpeg") {
|
||||
async fetch({ data, name, kategorie, mime: mimeType }, ctx, user) {
|
||||
const extension = mime.getExtension(mimeType);
|
||||
if (!extension) {
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Nicht unterstützter mimetype, unterstützt werden 'image/jpeg', 'image/png', 'application/pdf'."
|
||||
message: "Mime Type wird nicht unterstützt."
|
||||
})
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(data, "base64");
|
||||
|
||||
const id = generatePrefixedId(6, VALID_UUID_PREFIXES.Unterlage)
|
||||
|
||||
await prisma.unterlage.create({
|
||||
data: {
|
||||
id,
|
||||
kategorie: kategorie,
|
||||
mime,
|
||||
mime: mimeType,
|
||||
name
|
||||
}
|
||||
});
|
||||
|
||||
const filePath = fileURLToPath(new URL(`${PERSISTENT_DIR}/unterlagen/${id}`, import.meta.url));
|
||||
const buffer = Buffer.from(data, "base64");
|
||||
|
||||
try {
|
||||
writeFileSync(filePath, buffer)
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: "ibc-unterlagen",
|
||||
Key: `${id}.${extension}`,
|
||||
Body: buffer,
|
||||
ACL: "private"
|
||||
})
|
||||
|
||||
const response = await s3Client.send(command)
|
||||
} catch(e) {
|
||||
// Unterlage wurde nicht gespeichert, wir löschen den Eintrag wieder
|
||||
await prisma.unterlage.delete({
|
||||
|
||||
Reference in New Issue
Block a user