From 926218fa347edb94e055e275567c030b90cc1113 Mon Sep 17 00:00:00 2001
From: thiloho <123883702+thiloho@users.noreply.github.com>
Date: Sun, 25 Aug 2024 16:31:12 +0200
Subject: [PATCH] Set favicon and logo on output
---
.../20240825133549_adjust_upload_function.sql | 74 +++++++++++++++++++
.../src/lib/templates/blog/BlogArticle.svelte | 4 +-
.../src/lib/templates/blog/BlogIndex.svelte | 4 +-
.../lib/templates/blog/common/BlogHead.svelte | 9 ++-
.../lib/templates/blog/common/BlogNav.svelte | 2 +-
.../[websiteId]/publish/+page.server.ts | 34 ++++++++-
web-app/template-styles/common-styles.css | 2 +-
7 files changed, 121 insertions(+), 8 deletions(-)
create mode 100644 rest-api/db/migrations/20240825133549_adjust_upload_function.sql
diff --git a/rest-api/db/migrations/20240825133549_adjust_upload_function.sql b/rest-api/db/migrations/20240825133549_adjust_upload_function.sql
new file mode 100644
index 0000000..b15e618
--- /dev/null
+++ b/rest-api/db/migrations/20240825133549_adjust_upload_function.sql
@@ -0,0 +1,74 @@
+-- migrate:up
+CREATE OR REPLACE FUNCTION api.upload_file (BYTEA, OUT file_id UUID)
+AS $$
+DECLARE
+ _headers JSON := CURRENT_SETTING('request.headers', TRUE)::JSON;
+ _website_id UUID := (_headers ->> 'x-website-id')::UUID;
+ _mimetype TEXT := _headers ->> 'x-mimetype';
+ _original_filename TEXT := _headers ->> 'x-original-filename';
+ _allowed_mimetypes TEXT[] := ARRAY['image/png', 'image/jpeg', 'image/webp'];
+ _max_file_size INT := 5 * 1024 * 1024;
+BEGIN
+ IF OCTET_LENGTH($1) = 0 THEN
+ RAISE invalid_parameter_value
+ USING message = 'No file data was provided';
+ END IF;
+ IF _mimetype IS NULL OR _mimetype NOT IN (
+ SELECT
+ UNNEST(_allowed_mimetypes)) THEN
+ RAISE invalid_parameter_value
+ USING message = 'Invalid MIME type. Allowed types are: png, jpg, webp';
+ END IF;
+ IF OCTET_LENGTH($1) > _max_file_size THEN
+ RAISE program_limit_exceeded
+ USING message = FORMAT('File size exceeds the maximum limit of %s MB', _max_file_size / (1024 * 1024));
+ END IF;
+ INSERT INTO internal.media (website_id, blob, mimetype, original_name)
+ VALUES (_website_id, $1, _mimetype, _original_filename)
+ RETURNING
+ id INTO file_id;
+END;
+$$
+LANGUAGE plpgsql
+SECURITY DEFINER;
+
+GRANT EXECUTE ON FUNCTION api.upload_file (BYTEA) TO authenticated_user;
+
+-- migrate:down
+
+DROP FUNCTION api.upload_file(BYTEA);
+
+CREATE FUNCTION api.upload_file (BYTEA, OUT file_id UUID)
+AS $$
+DECLARE
+ _headers JSON := CURRENT_SETTING('request.headers', TRUE)::JSON;
+ _website_id UUID := (_headers ->> 'x-website-id')::UUID;
+ _mimetype TEXT := _headers ->> 'x-mimetype';
+ _original_filename TEXT := _headers ->> 'x-original-filename';
+ _allowed_mimetypes TEXT[] := ARRAY['image/png', 'image/jpeg', 'image/webp'];
+ _max_file_size INT := 5 * 1024 * 1024;
+BEGIN
+ IF OCTET_LENGTH($1) = 0 THEN
+ RAISE invalid_parameter_value
+ USING message = 'No file data was provided';
+ END IF;
+ IF _mimetype IS NULL OR _mimetype NOT IN (
+ SELECT
+ UNNEST(_allowed_mimetypes)) THEN
+ RAISE invalid_parameter_value
+ USING message = 'Invalid MIME type. Allowed types are: png, svg, jpg, webp';
+ END IF;
+ IF OCTET_LENGTH($1) > _max_file_size THEN
+ RAISE program_limit_exceeded
+ USING message = FORMAT('File size exceeds the maximum limit of %s MB', _max_file_size / (1024 * 1024));
+ END IF;
+ INSERT INTO internal.media (website_id, blob, mimetype, original_name)
+ VALUES (_website_id, $1, _mimetype, _original_filename)
+ RETURNING
+ id INTO file_id;
+END;
+$$
+LANGUAGE plpgsql
+SECURITY DEFINER;
+
+GRANT EXECUTE ON FUNCTION api.upload_file (BYTEA) TO authenticated_user;
\ No newline at end of file
diff --git a/web-app/src/lib/templates/blog/BlogArticle.svelte b/web-app/src/lib/templates/blog/BlogArticle.svelte
index 3ce2ebf..9e076d1 100644
--- a/web-app/src/lib/templates/blog/BlogArticle.svelte
+++ b/web-app/src/lib/templates/blog/BlogArticle.svelte
@@ -4,6 +4,7 @@
import BlogFooter from "./common/BlogFooter.svelte";
const {
+ favicon,
title,
logoType,
logo,
@@ -12,6 +13,7 @@
publicationDate,
footerAdditionalText
}: {
+ favicon: string;
title: string;
logoType: "text" | "image";
logo: string;
@@ -22,7 +24,7 @@
} = $props();
-