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,26 @@
<script lang="ts">
export let heading: string;
export let price: number;
export let href: string;
</script>
<div class="rpt_plan">
<div class="rpt_title">
<img
src="/images/icon_wohnhaus_plan_01.png"
class="rpt_icon rpt_icon_0"
alt="Energieausweis erstellen"
/>
{heading}
</div>
<div class="rpt_head">
<div class="rpt_recurrence">inkl. 19% MwSt</div>
<div class="rpt_price">
<span class="rpt_currency"></span>{price}
</div>
</div>
<slot></slot>
<a target="_self" {href} class="rpt_foot"
>jetzt sofort Energieausweis online erstellen</a
>
</div>

View File

@@ -60,7 +60,7 @@
const dataURL = canvas.toDataURL("image/jpeg", 0.8);
fetch("/api/image", {
fetch("/api/image.json", {
method: "PUT",
body: JSON.stringify({
data: dataURL.split(';base64,')[1],

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

View File

@@ -2,6 +2,7 @@
import { BoxWithHeading } from "@ibcornelsen/ui";
import Widget from "../components/Widget.svelte";
import Layout from "../layouts/Layout.astro";
import FeatureCard from "~/components/FeatureCard.svelte";
---
<Layout title="Energieausweis online erstellen - Online Energieausweis">
@@ -105,21 +106,7 @@ import Layout from "../layouts/Layout.astro";
</div>
<hr />
<div class="grid grid-cols-3 gap-4">
<div class="rpt_plan">
<div class="rpt_title">
<img
src="/images/icon_wohnhaus_plan_01.png"
class="rpt_icon rpt_icon_0"
alt="Energieausweis erstellen"
/>
Verbrauchsausweis<br />Wohngebäude
</div>
<div class="rpt_head">
<div class="rpt_recurrence">inkl. 19% MwSt</div>
<div class="rpt_price">
<span class="rpt_currency">€</span>45
</div>
</div>
<FeatureCard heading="Verbrauchsausweis Wohngebäude" price={45} href="/verbrauchsausweis">
<div class="rpt_feature">Unsere Leistungen:</div>
<div class="rpt_feature">Prüfung durch Diplom Ingenieur</div>
<div class="rpt_feature">Energieausweis Vorschau als PDF</div>
@@ -127,29 +114,9 @@ import Layout from "../layouts/Layout.astro";
<div class="rpt_feature">Registrierung beim DiBt</div>
<div class="rpt_feature">Bearbeitung innhalb 24h</div>
<div class="rpt_feature">rechtssicher nach aktueller EnEV</div>
<div class="rpt_feature">telefonische Beratung</div><a
target="_self"
href="/energieausweis-erstellen/verbrauchsausweis-erstellen.php"
class="rpt_foot">jetzt sofort Energieausweis online erstellen</a
>
</div>
<div class="rpt_plan">
<div class="rpt_title rpt_title_1">
<img
src="/images/icon_wohnhaus_plan_01.png"
class="rpt_icon rpt_icon_1"
alt="Energieausweis erstellen"
/>
Bedarfsausweis<br />Wohngebäude
</div>
<div class="rpt_head rpt_head_1">
<div class="rpt_recurrence rpt_recurrence_1">
inkl. 19% MwSt
</div>
<div class="rpt_price rpt_price_1">
<span class="rpt_currency">€</span>75
</div>
</div>
<div class="rpt_feature">telefonische Beratung</div>
</FeatureCard>
<FeatureCard heading="Bedarfsausweis Wohngebäude" price={75} href="/bedarfsausweis">
<div class="rpt_feature">Unsere Leistungen:</div>
<div class="rpt_feature">Prüfung durch Diplom Ingenieur</div>
<div class="rpt_feature">Energieausweis Vorschau als PDF</div>
@@ -157,29 +124,9 @@ import Layout from "../layouts/Layout.astro";
<div class="rpt_feature">Registrierung beim DiBt</div>
<div class="rpt_feature">Bearbeitung innhalb 24h</div>
<div class="rpt_feature">rechtssicher nach aktueller EnEV</div>
<div class="rpt_feature rpt_feature_1-7">
telefonische Beratung
</div><a
target="_self"
href="/energieausweis-erstellen/bedarfsausweis-erstellen.php"
class="rpt_foot">jetzt sofort Energieausweis online erstellen</a
>
</div>
<div class="rpt_plan">
<div class="rpt_title rpt_title_2">
<img
src="/images/icon_gewerbe_plan_01.png"
class="rpt_icon rpt_icon_2"
alt="Energieausweis erstellen"
/>
Verbrauchsausweis<br />Gewerbe
</div>
<div class="rpt_head rpt_head_2">
<div class="rpt_recurrence">inkl. 19% MwSt</div>
<div class="rpt_price">
<span class="rpt_currency">€</span>65
</div>
</div>
<div class="rpt_feature">telefonische Beratung</div>
</FeatureCard>
<FeatureCard heading="Verbrauchsausweis Gewerbe" price={65} href="/verbrauchsausweis-gewerbe">
<div class="rpt_feature">Unsere Leistungen:</div>
<div class="rpt_feature">Prüfung durch Diplom Ingenieur</div>
<div class="rpt_feature">Energieausweis Vorschau als PDF</div>
@@ -187,12 +134,8 @@ import Layout from "../layouts/Layout.astro";
<div class="rpt_feature">Registrierung beim DiBt</div>
<div class="rpt_feature">Bearbeitung innhalb 24h</div>
<div class="rpt_feature">rechtssicher nach aktueller EnEV</div>
<div class="rpt_feature">telefonische Beratung</div><a
target="_self"
href="/energieausweis-erstellen/verbrauchsausweis-gewerbe-erstellen.php"
class="rpt_foot">jetzt sofort Energieausweis online erstellen</a
>
</div>
<div class="rpt_feature">telefonische Beratung</div>
</FeatureCard>
</div>
<hr />