Bilder
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import { authorizationMiddleware, maybeAuthorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma, BildSchema } from "#lib/server/prisma";
|
||||
import { prisma } from "#lib/server/prisma.js";
|
||||
import { defineApiRoute, APIError } from "astro-typesafe-api/server";
|
||||
import { z } from "astro:content";
|
||||
import { fileURLToPath } from "url";
|
||||
import isBase64 from "is-base64";
|
||||
import { writeFileSync } from "fs"
|
||||
import { UUidWithPrefix } from "#components/Ausweis/types.js";
|
||||
import { BildSchema } from "src/generated/zod/bild.js";
|
||||
import sharp from "sharp"
|
||||
|
||||
export const PUT = defineApiRoute({
|
||||
input: BildSchema.pick({
|
||||
@@ -41,14 +43,13 @@ export const PUT = defineApiRoute({
|
||||
},
|
||||
});
|
||||
|
||||
const filePath = fileURLToPath(new URL(`../../../persistent/images/${bild.uid}.webp`, import.meta.url));
|
||||
const filePath = fileURLToPath(new URL(`../../../persistent/images/${bild.uid}.jpg`, import.meta.url));
|
||||
|
||||
try {
|
||||
// Wir optimieren das Bild und konvertieren es in WebP
|
||||
// TODO: Sharp scheint nicht zu funktionieren, wir müssen das nochmal testen
|
||||
// const optimizedBuffer = await sharp(buffer).webp({ quality: 80 }).toArray();
|
||||
// Wir optimieren das Bild und konvertieren es in JPEG
|
||||
const optimizedBuffer = await sharp(buffer).jpeg({ quality: 80 }).toBuffer();
|
||||
|
||||
writeFileSync(filePath, buffer)
|
||||
writeFileSync(filePath, optimizedBuffer)
|
||||
} catch(e) {
|
||||
// Bild wurde nicht gespeichert, wir löschen den Eintrag wieder
|
||||
await prisma.bild.delete({
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { BildSchema, prisma } from "#lib/server/prisma";
|
||||
import { prisma } from "#lib/server/prisma.js";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "zod";
|
||||
import isBase64 from "is-base64";
|
||||
import { fileURLToPath } from "url";
|
||||
import { writeFileSync } from "fs";
|
||||
import { UUidWithPrefix } from "#components/Ausweis/types.js";
|
||||
import sharp from "sharp"
|
||||
import { BildSchema } from "src/generated/zod/bild.js";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
input: BildSchema.pick({
|
||||
@@ -65,14 +67,14 @@ export const PATCH = defineApiRoute({
|
||||
});
|
||||
}
|
||||
|
||||
const filePath = fileURLToPath(new URL(`../../../../../persistent/images/${image.uid}.webp`, import.meta.url));
|
||||
const filePath = fileURLToPath(new URL(`../../../../../persistent/images/${image.uid}.jpg`, import.meta.url));
|
||||
|
||||
try {
|
||||
// Wir optimieren das Bild und konvertieren es in WebP
|
||||
// TODO: Sharp scheint nicht zu funktionieren, wir müssen das nochmal testen
|
||||
// const optimizedBuffer = await sharp(buffer).webp({ quality: 80 }).toArray();
|
||||
const optimizedBuffer = await sharp(buffer).jpeg({ quality: 80 }).toBuffer();
|
||||
|
||||
writeFileSync(filePath, buffer)
|
||||
writeFileSync(filePath, optimizedBuffer)
|
||||
} catch(e) {
|
||||
// Und geben einen Fehler zurück
|
||||
throw new APIError({
|
||||
|
||||
39
src/pages/bilder/[uid].jpg.ts
Normal file
39
src/pages/bilder/[uid].jpg.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { prisma } from "#lib/server/prisma.js";
|
||||
import { APIRoute } from "astro";
|
||||
import * as fs from "fs";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
export const GET: APIRoute = async (Astro) => {
|
||||
const { uid } = Astro.params;
|
||||
|
||||
const image = await prisma.bild.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
},
|
||||
});
|
||||
|
||||
if (!image) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const path = fileURLToPath(
|
||||
new URL(`../../../persistent/images/${image.uid}.jpg`, import.meta.url)
|
||||
);
|
||||
|
||||
if (!fs.existsSync(path)) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const buffer = fs.readFileSync(path);
|
||||
|
||||
return new Response(buffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "image/jpeg",
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -1,38 +0,0 @@
|
||||
|
||||
import { prisma } from "#lib/server/prisma";
|
||||
import { APIRoute } from "astro";
|
||||
import * as fs from "fs";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
export const GET: APIRoute = async (Astro) => {
|
||||
const { uid } = Astro.params
|
||||
|
||||
const image = await prisma.bild.findUnique({
|
||||
where: {
|
||||
uid
|
||||
}
|
||||
})
|
||||
|
||||
if (!image) {
|
||||
return new Response(null, {
|
||||
status: 404
|
||||
})
|
||||
}
|
||||
|
||||
const path = fileURLToPath(new URL(`../../../persistent/images/${image.uid}.webp`, import.meta.url))
|
||||
|
||||
if (!fs.existsSync(path)) {
|
||||
return new Response(null, {
|
||||
status: 404
|
||||
})
|
||||
}
|
||||
|
||||
const buffer = fs.readFileSync(path)
|
||||
|
||||
return new Response(buffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "image/webp"
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user