From b85312785d0ecbfb084e5afa1864d1be62c4b8f6 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sun, 18 Feb 2024 19:14:22 +0100 Subject: [PATCH] docs: Add example on how to automatically store mimetype in files table Resolves https://github.com/PostgREST/postgrest-docs/issues/612 --- docs/how-tos/providing-images-for-img.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/how-tos/providing-images-for-img.rst b/docs/how-tos/providing-images-for-img.rst index 03176376d2e..95d1b07f3d6 100644 --- a/docs/how-tos/providing-images-for-img.rst +++ b/docs/how-tos/providing-images-for-img.rst @@ -81,9 +81,11 @@ First, in addition to the minimal example, we need to store the media types and .. code-block:: postgres alter table files - add column type text, + add column type text generated always as (byteamagic_mime(substr(blob, 0, 4100))) stored, add column name text; +This uses the :code:`byteamagic_mime()` function from the `pg_byteamagic extension `_ to automatically generate the type in the :code:`files` table. To guess the type of a file, it's generally enough to look at the beginning of the file, which is more efficient. + Next, we set modify the function to set the content type and filename. We use this opportunity to configure some basic, client-side caching. For production, you probably want to configure additional caches, e.g. on the :ref:`reverse proxy `.