From 83ebdcbf217a320c27cadea653e1cd9eeabac3b4 Mon Sep 17 00:00:00 2001 From: Furkan Erdem <50502021+log101@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:23:10 +0300 Subject: [PATCH] Feat/image-optimization-with-astro * feat: add astro sharp service * feat: add content type metadata --- src/pages/api/content/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/pages/api/content/index.ts b/src/pages/api/content/index.ts index 39cedad..f7d60c6 100644 --- a/src/pages/api/content/index.ts +++ b/src/pages/api/content/index.ts @@ -2,6 +2,7 @@ import { createClient } from "@supabase/supabase-js" import type { APIRoute } from "astro" import { createKysely } from "@vercel/postgres-kysely" import { customAlphabet } from "nanoid" +import sharpService from "astro/assets/services/sharp" import type { Database } from "@/lib/db" @@ -37,11 +38,20 @@ export const POST: APIRoute = async ({ request }) => { const randomImageId = nanoid() - const imageName = `${image.name.replace(/\.[^/.]+$/, "")}${randomImageId}.jpg` + const imageName = `${image.name.replace(/\.[^/.]+$/, "")}${randomImageId}.webp` - const { error } = await supabase.storage.from("images").upload(`public/${imageName}`, image, { + const imageBuf = await image.arrayBuffer() + + const { data } = await sharpService.transform( + new Uint8Array(imageBuf), + { src: imageName }, + { domains: [], remotePatterns: [], service: { entrypoint: "", config: { limitInputPixels: false } } } + ) + + const { error } = await supabase.storage.from("images").upload(`public/${imageName}`, data, { cacheControl: "3600", - upsert: false + upsert: false, + contentType: "image/webp" }) if (error) {