refactor: extract image-helpers (#2261)

This commit is contained in:
Ted Liang
2025-12-09 09:19:49 +11:00
committed by GitHub
parent 3bd34964cd
commit c5032d0c43
12 changed files with 69 additions and 56 deletions
@@ -1,9 +1,9 @@
import satori from 'satori';
import sharp from 'sharp';
import { P, match } from 'ts-pattern';
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
import { getRecipientOrSenderByShareLinkSlug } from '@documenso/lib/server-only/document/get-recipient-or-sender-by-share-link-slug';
import { svgToPng } from '@documenso/lib/utils/images/svg-to-png';
import type { Route } from './+types/share.$slug.opengraph';
@@ -181,8 +181,7 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
},
);
// Convert SVG to PNG using sharp
const pngBuffer = await sharp(Buffer.from(svg)).toFormat('png').toBuffer();
const pngBuffer = await svgToPng(svg.toString());
return new Response(pngBuffer, {
headers: {
@@ -1,6 +1,5 @@
import sharp from 'sharp';
import { getFileServerSide } from '@documenso/lib/universal/upload/get-file.server';
import { loadLogo } from '@documenso/lib/utils/images/logo';
import { prisma } from '@documenso/prisma';
import type { Route } from './+types/branding.logo.organisation.$orgId';
@@ -63,16 +62,12 @@ export async function loader({ params }: Route.LoaderArgs) {
);
}
const img = await sharp(file)
.toFormat('png', {
quality: 80,
})
.toBuffer();
const { content, contentType } = await loadLogo(file);
return new Response(Buffer.from(img), {
return new Response(content, {
headers: {
'Content-Type': 'image/png',
'Content-Length': img.length.toString(),
'Content-Type': contentType,
'Content-Length': content.length.toString(),
// Stale while revalidate for 1 hours to 24 hours
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=86400',
},
@@ -1,7 +1,6 @@
import sharp from 'sharp';
import { getTeamSettings } from '@documenso/lib/server-only/team/get-team-settings';
import { getFileServerSide } from '@documenso/lib/universal/upload/get-file.server';
import { loadLogo } from '@documenso/lib/utils/images/logo';
import type { Route } from './+types/branding.logo.team.$teamId';
@@ -56,16 +55,12 @@ export async function loader({ params }: Route.LoaderArgs) {
);
}
const img = await sharp(file)
.toFormat('png', {
quality: 80,
})
.toBuffer();
const { content, contentType } = await loadLogo(file);
return new Response(img, {
return new Response(content, {
headers: {
'Content-Type': 'image/png',
'Content-Length': img.length.toString(),
'Content-Type': contentType,
'Content-Length': content.length.toString(),
// Stale while revalidate for 1 hours to 24 hours
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=86400',
},
-1
View File
@@ -70,7 +70,6 @@
"remeda": "^2.32.0",
"remix-themes": "^2.0.4",
"satori": "^0.18.3",
"sharp": "0.34.5",
"tailwindcss": "^3.4.18",
"ts-pattern": "^5.9.0",
"ua-parser-js": "^1.0.41",