Add Image API
This commit is contained in:
25
src/pages/api/building/images.json.ts
Normal file
25
src/pages/api/building/images.json.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import {
|
||||
MissingEntityError,
|
||||
error,
|
||||
success,
|
||||
} from "src/lib/APIResponse";
|
||||
import { db } from "src/lib/shared";
|
||||
|
||||
export const get: APIRoute = async ({ request }) => {
|
||||
const body = Object.fromEntries(new URLSearchParams(request.url.split("?")[1]))
|
||||
|
||||
if (!body.uid) {
|
||||
return error(["Missing 'uid' in request body."])
|
||||
}
|
||||
|
||||
const gebaeude = await db("gebaeude").where({ uid: body.uid }).first();
|
||||
|
||||
if (!gebaeude) {
|
||||
return MissingEntityError("gebaeude")
|
||||
}
|
||||
|
||||
const images = await db("gebaeude_bilder").where({ gebaeude_id: gebaeude.id }).select("uid", "kategorie");
|
||||
|
||||
return success(images);
|
||||
};
|
||||
45
src/pages/api/image.jpg.ts
Normal file
45
src/pages/api/image.jpg.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import {
|
||||
MissingEntityError,
|
||||
error,
|
||||
} from "src/lib/APIResponse";
|
||||
import { z } from "zod";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import { db } from "src/lib/shared";
|
||||
|
||||
const ImageUploadChecker = z.object({
|
||||
data: z.string(),
|
||||
name: z.string(),
|
||||
gebaeude_uid: z.string().optional(),
|
||||
kategorie: z.string(),
|
||||
});
|
||||
|
||||
export const get: APIRoute = async ({ request }) => {
|
||||
const body = Object.fromEntries(new URLSearchParams(request.url.split("?")[1]))
|
||||
|
||||
if (!body.uid) {
|
||||
return error(["Missing 'uid' in request body."])
|
||||
}
|
||||
|
||||
const image = await db("gebaeude_bilder").where({ uid: body.uid }).select("uid", "kategorie").first();
|
||||
|
||||
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"
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import * as jimp from "jimp";
|
||||
import { z } from "zod";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import { db } from "src/lib/shared";
|
||||
|
||||
const ImageUploadChecker = z.object({
|
||||
@@ -17,6 +18,31 @@ const ImageUploadChecker = z.object({
|
||||
kategorie: z.string(),
|
||||
});
|
||||
|
||||
export const get: APIRoute = async ({ request }) => {
|
||||
const body = Object.fromEntries(new URLSearchParams(request.url.split("?")[1]))
|
||||
|
||||
if (!body.uid) {
|
||||
return error(["Missing 'uid' in request body."])
|
||||
}
|
||||
|
||||
const image = await db("gebaeude_bilder").where({ uid: body.uid }).select("uid", "kategorie").first();
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user