From c2945e9fb2c73bc31a239ff6fb1472c96c085af1 Mon Sep 17 00:00:00 2001 From: Chris W Date: Sun, 8 Oct 2023 16:34:26 -0600 Subject: [PATCH] local proxy for og images --- src/app.html | 6 +++- src/routes/[slug]/+page.server.ts | 2 ++ src/routes/[slug]/+page.svelte | 8 ++++- src/routes/images/paste/[id]/+server.ts | 46 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/routes/images/paste/[id]/+server.ts diff --git a/src/app.html b/src/app.html index 4bfaef6..c31ab60 100644 --- a/src/app.html +++ b/src/app.html @@ -4,9 +4,13 @@ + + + + %sveltekit.head% - +
%sveltekit.body%
diff --git a/src/routes/[slug]/+page.server.ts b/src/routes/[slug]/+page.server.ts index 5a52b1d..f38d832 100644 --- a/src/routes/[slug]/+page.server.ts +++ b/src/routes/[slug]/+page.server.ts @@ -1,6 +1,7 @@ import { error } from '@sveltejs/kit'; import type { PageLoad } from './$types'; import { pastes } from '$db/index'; +import { SITE_URL } from '$env/static/private'; export const load: PageLoad = async ({ params }) => { const [id, ext] = params.slug.split('.'); @@ -14,6 +15,7 @@ export const load: PageLoad = async ({ params }) => { // Build the response object const response = { id: paste.id, + url: `${SITE_URL}/${id}.${paste.highlight}`, contents: paste.contents, encrypted: paste.encrypted, highlight: ext || paste.highlight, diff --git a/src/routes/[slug]/+page.svelte b/src/routes/[slug]/+page.svelte index 6b0a037..e94d3e0 100644 --- a/src/routes/[slug]/+page.svelte +++ b/src/routes/[slug]/+page.svelte @@ -82,13 +82,19 @@ Paste69 - Paste {data.id} + + + + + + {#if renderMarkdown}
{@html contents}
{:else} -
 selectAll()} >{@html contents}
+
 selectAll()} >{@html contents}
{/if}
diff --git a/src/routes/images/paste/[id]/+server.ts b/src/routes/images/paste/[id]/+server.ts new file mode 100644 index 0000000..adc5444 --- /dev/null +++ b/src/routes/images/paste/[id]/+server.ts @@ -0,0 +1,46 @@ +import { pastes } from "$db/index"; +import { SITE_URL } from "$env/static/private"; +import { error, type RequestHandler } from "@sveltejs/kit"; + +export const GET: RequestHandler = async ({ params }) => { + const { id } = params; + + const paste = await pastes.findOne({ id }); + + if (!paste) { + throw error(404, 'Paste not found'); + } + + if (paste.encrypted) { + throw error(404, 'Cannot generate image for encrypted paste'); + } + + if (paste.burnAfterReading) { + throw error(404, 'Cannot generate image for burnable paste'); + } + + const code = paste.contents; + + // Grab at most 15 lines of the paste, if there aren't 15 lines, pad the rest with empty strings. + let lines = code.split('\n').slice(0, 15).concat(Array.from({ length: 15 - code.split('\n').length }, () => '')); + + // Truncate each line to 80 characters, and pad the rest with spaces. + lines = lines.map(line => line.slice(0, 70).padEnd(70, ' ')); + + const title = `${SITE_URL}/${id}.${paste.highlight}`; + const url = `https://inkify.0x45.st/generate?code=${encodeURIComponent(lines.join('\n'))}&window_title=${encodeURIComponent(title)}&language=${encodeURIComponent(paste.highlight)}&pad_horiz=5&pad_vert=5`; + + const res = await fetch(url); + const image = res.body; + + if (image) { + return new Response(image, { + headers: { + 'Content-Type': 'image/png', + 'Cache-Control': 'public, max-age=604800, immutable', + }, + }); + } else { + throw error(500, 'Could not generate image'); + } +}; \ No newline at end of file