Bugfixes, Kundendaten, Datenbank Anpassung
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
// NOTE: Öffentliche API benötigt OpenApiMeta. Das Package bräuchte momentan noch einen extra Server, deshalb nehmen wir es momentan noch nicht mit rein.
|
||||
//import { OpenApiMeta } from "trpc-openapi";
|
||||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
||||
import { APIRoute } from "astro";
|
||||
import { t } from "../../lib/trpc/context";
|
||||
import { v1Router } from "#lib/trpc/procedures/v1";
|
||||
|
||||
export const appRouter = t.router({
|
||||
v1: v1Router,
|
||||
});
|
||||
|
||||
export const all: APIRoute = ({ request }) => {
|
||||
return fetchRequestHandler({
|
||||
req: request,
|
||||
endpoint: "/api",
|
||||
router: appRouter,
|
||||
createContext: async ({ req }) => {
|
||||
const ip = req.headers.get("x-forwarded-for");
|
||||
const authorization = req.headers.get("authorization") || null;
|
||||
|
||||
return {
|
||||
authorization,
|
||||
ip: ip?.toString() || "",
|
||||
req: req,
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export function tRPCCaller(request: Request) {
|
||||
const { authorization } = {
|
||||
authorization: request.headers.get("authorization") || "",
|
||||
};
|
||||
const createCaller = t.createCallerFactory(appRouter);
|
||||
return createCaller({
|
||||
authorization,
|
||||
req: request,
|
||||
ip: request.headers.get("x-forwarded-for") || "",
|
||||
});
|
||||
}
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
@@ -1,46 +0,0 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import {
|
||||
MissingEntityError,
|
||||
error,
|
||||
} from "src/lib/APIResponse";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import { prisma } from "@ibcornelsen/database";
|
||||
|
||||
export const get: APIRoute = async ({ url }) => {
|
||||
const body = url.searchParams
|
||||
const uid = body.get("uid")
|
||||
|
||||
if (!body.has("uid") || !uid) {
|
||||
return error(["Missing 'uid' in request body."])
|
||||
}
|
||||
|
||||
const image = await prisma.gebaeudeBilder.findUnique({
|
||||
where: {
|
||||
uid
|
||||
},
|
||||
select: {
|
||||
uid: true,
|
||||
kategorie: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!image) {
|
||||
return MissingEntityError("image")
|
||||
}
|
||||
|
||||
// Check if the image exists on disk
|
||||
const location = path.join("/persistent/uploads/images", `${image.uid}.jpg`);
|
||||
|
||||
if (!fs.existsSync(location)) {
|
||||
return MissingEntityError("image")
|
||||
}
|
||||
|
||||
const data = fs.readFileSync(location);
|
||||
|
||||
return new Response(data, {
|
||||
headers: {
|
||||
"Content-Type": "image/jpeg"
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,131 +0,0 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import {
|
||||
ActionFailedError,
|
||||
MissingEntityError,
|
||||
error,
|
||||
success,
|
||||
} from "src/lib/APIResponse";
|
||||
import * as jimp from "jimp";
|
||||
import { z } from "zod";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import { prisma } from "@ibcornelsen/database";
|
||||
|
||||
const ImageUploadChecker = z.object({
|
||||
data: z.string(),
|
||||
name: z.string(),
|
||||
gebaeude_uid: z.string().optional(),
|
||||
kategorie: z.string(),
|
||||
});
|
||||
|
||||
export const get: APIRoute = async ({ url }) => {
|
||||
const body = url.searchParams
|
||||
const uid = body.get("uid")
|
||||
|
||||
if (!body.has("uid") || !uid) {
|
||||
return error(["Missing 'uid' in request body."])
|
||||
}
|
||||
|
||||
const image = await prisma.gebaeudeBilder.findUnique({
|
||||
where: {
|
||||
uid
|
||||
},
|
||||
select: {
|
||||
uid: true,
|
||||
kategorie: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!image) {
|
||||
return MissingEntityError("image")
|
||||
}
|
||||
|
||||
// Check if the image exists on disk
|
||||
const location = path.join("/persistent/uploads/images", `${image.uid}.jpg`);
|
||||
|
||||
if (!fs.existsSync(location)) {
|
||||
return MissingEntityError("image")
|
||||
}
|
||||
|
||||
const data = fs.readFileSync(location, { encoding: "base64" });
|
||||
|
||||
return success(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Speichert ein Bild auf unserem Server ab und gibt die UID des Bildes zurück
|
||||
* @param param0
|
||||
* @returns
|
||||
*/
|
||||
export const put: APIRoute = async ({ request }) => {
|
||||
const body: z.infer<typeof ImageUploadChecker> = await request.json();
|
||||
|
||||
const validation = ImageUploadChecker.safeParse(body);
|
||||
|
||||
if (!validation.success) {
|
||||
return error(validation.error.issues);
|
||||
}
|
||||
|
||||
const image = Buffer.from(body.data, "base64url");
|
||||
|
||||
let jimpResult;
|
||||
try {
|
||||
jimpResult = await jimp.read(image);
|
||||
} catch (e) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
let gebaeude;
|
||||
if (!body.gebaeude_uid) {
|
||||
gebaeude = await prisma.gebaeudeStammdaten.create({
|
||||
data: {},
|
||||
select: {
|
||||
uid: true,
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!gebaeude) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
} else {
|
||||
gebaeude = await prisma.gebaeudeStammdaten.findUnique({
|
||||
where: {
|
||||
uid: body.gebaeude_uid
|
||||
}
|
||||
})
|
||||
|
||||
if (!gebaeude) {
|
||||
return MissingEntityError("gebaeude");
|
||||
}
|
||||
}
|
||||
|
||||
const result = await prisma.gebaeudeBilder.create({
|
||||
data: {
|
||||
gebaeude_stammdaten_id: gebaeude.id,
|
||||
kategorie: body.kategorie
|
||||
},
|
||||
select: {
|
||||
uid: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!result) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
const location = path.join("/persistent/uploads", `${result.uid}.jpg`);
|
||||
|
||||
const buffer = await jimpResult.getBufferAsync(jimp.MIME_JPEG)
|
||||
|
||||
if (buffer.length > 3_000_000) {
|
||||
jimpResult.quality(75).write(location);
|
||||
} else {
|
||||
jimpResult.write(location);
|
||||
}
|
||||
|
||||
return success({
|
||||
uid: result.uid,
|
||||
gebaeude_uid: gebaeude.uid,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user