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(og-image): og thumb image load error when using local storage

Timothy Yinf2e52dfdeebe
diff --git a/app/pages/[...slug].vue b/app/pages/[...slug].vue
index 57a6f56d..281269a0 100644
--- a/app/pages/[...slug].vue
+++ b/app/pages/[...slug].vue
@@ -28,7 +28,7 @@ defineOgImageComponent('Photo', {
: config.public.app.slogan,
thumbnailJpegUrl:
currentPhoto.value && currentPhoto.value.thumbnailKey
- ? `/thumb/${currentPhoto.value.thumbnailKey}`
+ ? `/thumb/${encodeURIComponent(currentPhoto.value.thumbnailUrl || '')}`
: undefined,
photo: currentPhoto.value || undefined,
})
diff --git a/nuxt.config.ts b/nuxt.config.ts
index 60530a9e..acfabac7 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -153,7 +153,6 @@ export default defineNuxtConfig({
ogImage: {
fonts: ['Rubik:400', 'Rubik:700', 'Noto+Sans+SC:400', 'Noto+Sans+SC:700'],
- googleFontMirror: true,
},
dayjs: {
diff --git a/server/routes/thumb/[...key].get.ts b/server/routes/thumb/[...key].get.ts
deleted file mode 100644
index 23ebbc10..00000000
--- a/server/routes/thumb/[...key].get.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import sharp from 'sharp'
-
-export default eventHandler(async (event) => {
- const { storageProvider } = useStorageProvider(event)
- const key = getRouterParam(event, 'key')
-
- if (!key) {
- throw createError({ statusCode: 400, statusMessage: 'Invalid key' })
- }
-
- const photo = await storageProvider.get(`${key}`)
- if (!photo) {
- throw createError({ statusCode: 404, statusMessage: 'Photo not found' })
- }
- logger.chrono.info('Convert thumb to jpeg from key', key)
- const sharpInst = sharp(photo).rotate()
- return await sharpInst.jpeg({ quality: 85 }).toBuffer()
-})
diff --git a/server/routes/thumb/[...thumbnailUrl].get.ts b/server/routes/thumb/[...thumbnailUrl].get.ts
new file mode 100644
index 00000000..ea5bcf36
--- /dev/null
+++ b/server/routes/thumb/[...thumbnailUrl].get.ts
@@ -0,0 +1,30 @@
+import sharp from 'sharp'
+
+export default eventHandler(async (event) => {
+ const { storageProvider } = useStorageProvider(event)
+
+ let url = getRouterParam(event, 'thumbnailUrl')
+
+ if (!url) {
+ throw createError({ statusCode: 400, statusMessage: 'Invalid thumbnailUrl' })
+ }
+
+ url = decodeURIComponent(url)
+
+ if (storageProvider.config?.provider === 'local' && url.startsWith('/storage/')) {
+ const scheme = event.node.req.headers['x-forwarded-proto'] || 'http'
+ url = `${scheme}://${event.node.req.headers.host}${url}`
+ }
+
+ const photo = await fetch(url)
+ .then((res) => {
+ if (!res.ok) {
+ throw createError({ statusCode: 404, statusMessage: 'Photo not found' })
+ }
+ return res.arrayBuffer()
+ })
+ .then((buf) => Buffer.from(buf))
+
+ const sharpInst = sharp(photo).rotate()
+ return await sharpInst.jpeg({ quality: 85 }).toBuffer()
+})