Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(gallery): restore SaaS OG photo backgrounds

Timothy Yin48d5f507da06
diff --git a/packages/gallery/app/components/OgImage/Home.takumi.vue b/packages/gallery/app/components/OgImage/Home.takumi.vue
index 7cf78bad..d6c30386 100644
--- a/packages/gallery/app/components/OgImage/Home.takumi.vue
+++ b/packages/gallery/app/components/OgImage/Home.takumi.vue
@@ -1,4 +1,6 @@
<script setup lang="ts">
+import { resolveShareThumbnail } from '#gallery/utils/open-graph'
+
const props = defineProps<{
appTitle?: string
slogan?: string
@@ -18,7 +20,12 @@ const countLabel = computed(() => {
if (typeof props.photoCount !== 'number') return ''
return $i18n.t('plural.photo', { count: props.photoCount }, props.photoCount)
})
-const covers = computed(() => (props.thumbnails || []).slice(0, 3))
+const capabilities = useEntryCapabilities()
+const covers = computed(() =>
+ (props.thumbnails || [])
+ .slice(0, 3)
+ .map((url) => resolveShareThumbnail(url, capabilities.edition)),
+)
</script>
<template>
@@ -62,7 +69,7 @@ const covers = computed(() => (props.thumbnails || []).slice(0, 3))
<div class="og-home__frame">
<img
v-if="covers[0]"
- :src="'/thumb/' + encodeURIComponent(covers[0] || '')"
+ :src="covers[0]"
class="og-home__image"
/>
<div
@@ -76,7 +83,7 @@ const covers = computed(() => (props.thumbnails || []).slice(0, 3))
<div class="og-home__frame og-home__tall">
<img
v-if="covers[1]"
- :src="'/thumb/' + encodeURIComponent(covers[1] || '')"
+ :src="covers[1]"
class="og-home__image"
/>
<div
@@ -90,7 +97,7 @@ const covers = computed(() => (props.thumbnails || []).slice(0, 3))
<div class="og-home__frame">
<img
v-if="covers[2]"
- :src="'/thumb/' + encodeURIComponent(covers[2] || '')"
+ :src="covers[2]"
class="og-home__image"
/>
<div
diff --git a/packages/gallery/app/components/OgImage/Photo.takumi.vue b/packages/gallery/app/components/OgImage/Photo.takumi.vue
index b02375ce..47702490 100644
--- a/packages/gallery/app/components/OgImage/Photo.takumi.vue
+++ b/packages/gallery/app/components/OgImage/Photo.takumi.vue
@@ -2,6 +2,7 @@
import { Icon as IconifyIcon, addIcon } from '@iconify/vue'
import { init as initIconBundle } from '#build/nuxt-icon-client-bundle'
import { formatCameraInfo } from '#gallery/utils/camera'
+import { resolveShareThumbnail } from '#gallery/utils/open-graph'
initIconBundle(addIcon)
@@ -45,10 +46,9 @@ const title = computed(() =>
const description = computed(() =>
(photo ? photo.description || '' : resolvedAppTitle.value).slice(0, 200),
)
+const capabilities = useEntryCapabilities()
const thumbnailUrl = computed(() =>
- photo?.thumbnailKey && photo.thumbnailUrl
- ? `/thumb/${encodeURIComponent(photo.thumbnailUrl)}`
- : undefined,
+ resolveShareThumbnail(photo?.thumbnailUrl, capabilities.edition),
)
</script>
diff --git a/packages/gallery/app/utils/open-graph.ts b/packages/gallery/app/utils/open-graph.ts
index ad9413e4..31f97579 100644
--- a/packages/gallery/app/utils/open-graph.ts
+++ b/packages/gallery/app/utils/open-graph.ts
@@ -1,5 +1,18 @@
const OG_AVATAR_FALLBACK = '/web-app-manifest-512x512.png'
+/** Keep media authorization at the entry's existing thumbnail endpoint. */
+export function resolveShareThumbnail(
+ value: string | null | undefined,
+ edition: 'self-hosted' | 'cloud' | 'site',
+): string | undefined {
+ if (!value) return undefined
+ // Only Self-hosted supplies the Sharp-backed JPEG proxy. Edge entries serve
+ // their thumbnails through Site-scoped media routes instead.
+ return edition === 'self-hosted'
+ ? `/thumb/${encodeURIComponent(value)}`
+ : value
+}
+
export function cleanShareText(value: unknown, maxLength: number): string {
if (typeof value !== 'string') return ''
return value.replace(/\s+/g, ' ').trim().slice(0, maxLength)
diff --git a/tests/og-thumbnail.test.ts b/tests/og-thumbnail.test.ts
new file mode 100644
index 00000000..8f976c53
--- /dev/null
+++ b/tests/og-thumbnail.test.ts
@@ -0,0 +1,99 @@
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import test from 'node:test'
+import vm from 'node:vm'
+import { parse, compileScript } from 'vue/compiler-sfc'
+import { renderToString } from 'vue/server-renderer'
+import * as vue from 'vue'
+import ts from 'typescript'
+import { resolveShareThumbnail } from '../packages/gallery/app/utils/open-graph.ts'
+
+type Edition = 'self-hosted' | 'cloud' | 'site'
+
+async function render(
+ template: 'Photo' | 'Home',
+ edition: Edition,
+ props: Record<string, unknown>,
+) {
+ const filename = `packages/gallery/app/components/OgImage/${template}.takumi.vue`
+ const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })
+ const source = compileScript(descriptor, {
+ id: 'og-thumbnail',
+ inlineTemplate: true,
+ }).content
+ const code = ts.transpileModule(source, {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2022,
+ },
+ }).outputText
+ const exports: { default?: vue.Component } = {}
+ vm.runInNewContext(code, {
+ exports,
+ computed: vue.computed,
+ useEntryCapabilities: () => ({ edition }),
+ useNuxtApp: () => ({ $i18n: { t: (key: string) => key } }),
+ useLocationLabel: () => () => '',
+ getSetting: () => '',
+ require: (name: string) => {
+ if (name === 'vue') return vue
+ if (name === '#gallery/utils/open-graph') return { resolveShareThumbnail }
+ if (name === '#gallery/utils/camera')
+ return { formatCameraInfo: () => '' }
+ if (name === '#build/nuxt-icon-client-bundle') return { init: () => {} }
+ if (name === '@iconify/vue')
+ return { Icon: () => vue.h('span'), addIcon: () => {} }
+ throw new Error(`Unexpected import: ${name}`)
+ },
+ })
+ assert.ok(exports.default)
+ return renderToString(vue.createSSRApp(exports.default, props))
+}
+
+for (const edition of ['site', 'cloud', 'self-hosted'] as const) {
+ test(`${edition}: photo OG renders the thumbnail and gradient without internal storage keys`, async () => {
+ const thumbnailUrl =
+ edition === 'self-hosted'
+ ? '/storage/thumbnail.webp'
+ : '/api/media/photo-id/thumbnail'
+ const html = await render('Photo', edition, {
+ photo: { id: 'photo-id', title: 'Example', thumbnailUrl },
+ appTitle: 'Gallery',
+ })
+ const expected =
+ edition === 'self-hosted'
+ ? '/thumb/%2Fstorage%2Fthumbnail.webp'
+ : thumbnailUrl
+ assert.ok(html.includes(`src="${expected}"`))
+ assert.ok(html.includes('og-photo__gradient'))
+ assert.ok(html.includes('Example'))
+ })
+
+ test(`${edition}: home OG uses the same entry media route`, async () => {
+ const url = '/api/media/photo-id/thumbnail'
+ const html = await render('Home', edition, { thumbnails: [url] })
+ assert.ok(html.includes(`src="${resolveShareThumbnail(url, edition)}"`))
+ })
+}
+
+test('photo OG without a thumbnail keeps its text fallback', async () => {
+ const html = await render('Photo', 'site', {
+ photo: { id: 'missing', title: 'No thumbnail', thumbnailUrl: null },
+ })
+ assert.ok(html.includes('No thumbnail'))
+ assert.ok(!html.includes('<img'))
+ assert.ok(!html.includes('og-photo__gradient'))
+})
+
+test('self-hosted external and protected thumbnails keep using the authorized JPEG proxy', () => {
+ for (const url of [
+ 'https://media.example/photo.webp',
+ '/image/__photo__/protected/thumbnail',
+ ]) {
+ assert.equal(
+ resolveShareThumbnail(url, 'self-hosted'),
+ `/thumb/${encodeURIComponent(url)}`,
+ )
+ }
+ assert.equal(resolveShareThumbnail(undefined, 'site'), undefined)
+})