Add Image API

This commit is contained in:
Moritz Utcke
2023-07-10 19:34:39 +05:00
parent 82db459b4e
commit de47be96d6
14 changed files with 251 additions and 75 deletions

View 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);
};

View 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"
}
});
};

View File

@@ -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