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.

feat(share): add OG image preview and download function

HoshinoSuzumi38b9d26654b8
diff --git a/app/components/photo/ShareModal.vue b/app/components/photo/ShareModal.vue
index 817b8c77..60b40e4c 100644
--- a/app/components/photo/ShareModal.vue
+++ b/app/components/photo/ShareModal.vue
@@ -30,6 +30,75 @@ const shareTextAndUrl = computed(() => {
return `${shareText.value}\n${shareUrl.value}`
})
+// OG Image URL and loading state
+const ogImageLoading = ref(true)
+const ogImageError = ref(false)
+const loadingTimer = ref<NodeJS.Timeout | null>(null)
+
+const ogImageUrl = computed(() => {
+ if (typeof window !== 'undefined') {
+ // Add timestamp to prevent caching issues
+ const timestamp = Date.now()
+ return `${window.location.origin}/__og-image__/image/${props.photo.id}/og.png?t=${timestamp}`
+ }
+ return ''
+})
+
+// Reset loading state when photo changes or modal opens
+const resetLoadingState = () => {
+ ogImageLoading.value = true
+ ogImageError.value = false
+
+ // Clear existing timer
+ if (loadingTimer.value) {
+ clearTimeout(loadingTimer.value)
+ }
+
+ // Set a timeout to handle cases where onload/onerror never fires
+ loadingTimer.value = setTimeout(() => {
+ if (ogImageLoading.value) {
+ ogImageLoading.value = false
+ ogImageError.value = true
+ }
+ }, 10000) // 10 second timeout
+}
+
+// Reset loading state when photo changes
+watch(() => props.photo.id, resetLoadingState)
+
+// Reset loading state when modal opens
+watch(() => props.isOpen, (newValue) => {
+ if (newValue) {
+ resetLoadingState()
+ }
+})
+
+// Handle image load events
+const handleOgImageLoad = () => {
+ if (loadingTimer.value) {
+ clearTimeout(loadingTimer.value)
+ loadingTimer.value = null
+ }
+ ogImageLoading.value = false
+ ogImageError.value = false
+}
+
+const handleOgImageError = () => {
+ if (loadingTimer.value) {
+ clearTimeout(loadingTimer.value)
+ loadingTimer.value = null
+ }
+ ogImageLoading.value = false
+ ogImageError.value = true
+}
+
+// Cleanup on unmount
+onUnmounted(() => {
+ if (loadingTimer.value) {
+ clearTimeout(loadingTimer.value)
+ }
+})
+
// Social media share functions
const shareToTwitter = () => {
const text = encodeURIComponent(shareText.value)
@@ -110,6 +179,37 @@ const nativeShare = async () => {
}
}
+// Download OG Image
+const downloadOgImage = async () => {
+ try {
+ const response = await fetch(ogImageUrl.value)
+ const blob = await response.blob()
+ const url = window.URL.createObjectURL(blob)
+ const link = document.createElement('a')
+ link.href = url
+ link.download = `${props.photo.title || 'photo'}-og.png`
+ document.body.appendChild(link)
+ link.click()
+ document.body.removeChild(link)
+ window.URL.revokeObjectURL(url)
+
+ toast.add({
+ title: $t('ui.action.share.success.ogImageDownloaded'),
+ color: 'success',
+ icon: 'tabler:download',
+ duration: 3000,
+ })
+ } catch (error) {
+ toast.add({
+ title: $t('ui.action.share.error.ogImageDownloadFailed'),
+ description: (error as Error)?.message || 'Unknown error',
+ color: 'error',
+ icon: 'tabler:x',
+ duration: 3000,
+ })
+ }
+}
+
// Check if native share is available
const canNativeShare = computed(() => {
return typeof window !== 'undefined' && navigator.share
@@ -265,6 +365,74 @@ defineShortcuts({
/>
</div>
</div>
+
+ <!-- OG Image Preview -->
+ <div
+ class="rounded-lg border border-neutral-200/50 bg-neutral-50/50 p-3 dark:border-neutral-700/50 dark:bg-neutral-800/50"
+ >
+ <div class="flex items-center justify-between mb-2">
+ <label
+ class="block text-xs font-medium text-neutral-600 dark:text-neutral-400"
+ >
+ {{ $t('ui.action.share.ogImage.title') }}
+ </label>
+ <UButton
+ size="xs"
+ variant="ghost"
+ color="neutral"
+ icon="tabler:download"
+ @click="downloadOgImage"
+ >
+ {{ $t('ui.action.share.actions.downloadOgImage') }}
+ </UButton>
+ </div>
+ <div class="relative rounded-md bg-neutral-100/50 dark:bg-neutral-700/50 overflow-hidden">
+ <!-- Loading indicator -->
+ <div
+ v-if="ogImageLoading"
+ class="flex items-center justify-center bg-neutral-100/50 dark:bg-neutral-700/50 aspect-[2/1]"
+ >
+ <div class="flex flex-col items-center gap-2">
+ <Icon
+ name="tabler:loader-2"
+ class="size-6 text-neutral-500 animate-spin"
+ />
+ <span class="text-xs text-neutral-500 dark:text-neutral-400">
+ {{ $t('ui.action.share.ogImage.loading') }}
+ </span>
+ </div>
+ </div>
+
+ <!-- Error state -->
+ <div
+ v-else-if="ogImageError"
+ class="flex items-center justify-center bg-neutral-100/50 dark:bg-neutral-700/50 aspect-[2/1]"
+ >
+ <div class="flex flex-col items-center gap-2">
+ <Icon
+ name="tabler:photo-off"
+ class="size-6 text-neutral-400"
+ />
+ <span class="text-xs text-neutral-500 dark:text-neutral-400">
+ {{ $t('ui.action.share.ogImage.loadError') }}
+ </span>
+ </div>
+ </div>
+
+ <!-- OG Image -->
+ <img
+ v-show="!ogImageLoading && !ogImageError"
+ :key="`og-image-${props.photo.id}-${Date.now()}`"
+ :src="ogImageUrl"
+ :alt="$t('ui.action.share.ogImage.alt')"
+ class="w-full h-auto aspect-[2/1] object-cover rounded"
+ loading="eager"
+ @load="handleOgImageLoad"
+ @error="handleOgImageError"
+ />
+ </div>
+ </div>
+
<!-- Social Platforms Grid -->
<div class="grid grid-cols-3 gap-3">
<button
@@ -295,5 +463,4 @@ defineShortcuts({
</template>
<style scoped>
-/* 确保模态框在最顶层 */
</style>
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index 67e3e38c..85cff862 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -71,16 +71,26 @@
"copyImage": "Copy Image",
"downloadImage": "Download Original",
"generateShareImage": "Generate Share Image",
- "shareUrl": "Share URL"
+ "shareUrl": "Share URL",
+ "downloadOgImage": "Download Preview"
},
"success": {
"linkCopied": "Link copied to clipboard",
- "imageCopied": "Image copied to clipboard"
+ "imageCopied": "Image copied to clipboard",
+ "ogImageDownloaded": "Preview image downloaded successfully"
},
"error": {
"copyNotSupported": "Copy not supported in this browser",
"shareFailed": "Share failed",
- "nativeShareFailed": "Native share failed"
+ "nativeShareFailed": "Native share failed",
+ "linkCopyFailed": "Failed to copy link",
+ "ogImageDownloadFailed": "Failed to download preview image"
+ },
+ "ogImage": {
+ "title": "Share Preview",
+ "alt": "Photo share preview image",
+ "loading": "Loading...",
+ "loadError": "Failed to load"
},
"fallback": {
"photoTitle": "Beautiful Photo"
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index 0ebdc7d7..243f766f 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -71,16 +71,26 @@
"copyImage": "画像をコピー",
"downloadImage": "オリジナルをダウンロード",
"generateShareImage": "共有画像を生成",
- "shareUrl": "共有URL"
+ "shareUrl": "共有URL",
+ "downloadOgImage": "プレビューをダウンロード"
},
"success": {
"linkCopied": "リンクをクリップボードにコピーしました",
- "imageCopied": "画像をクリップボードにコピーしました"
+ "imageCopied": "画像をクリップボードにコピーしました",
+ "ogImageDownloaded": "プレビュー画像のダウンロードが完了しました"
},
"error": {
"copyNotSupported": "このブラウザではコピー機能がサポートされていません",
"shareFailed": "共有に失敗しました",
- "nativeShareFailed": "ネイティブ共有に失敗しました"
+ "nativeShareFailed": "ネイティブ共有に失敗しました",
+ "linkCopyFailed": "リンクのコピーに失敗しました",
+ "ogImageDownloadFailed": "プレビュー画像のダウンロードに失敗しました"
+ },
+ "ogImage": {
+ "title": "共有プレビュー",
+ "alt": "写真共有プレビュー画像",
+ "loading": "読み込み中...",
+ "loadError": "読み込みに失敗しました"
},
"fallback": {
"photoTitle": "美しい写真"
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index 94dcf450..fbd202ba 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -75,16 +75,26 @@
"copyImage": "复制图片",
"downloadImage": "下载原图",
"generateShareImage": "生成分享图片",
- "shareUrl": "分享链接"
+ "shareUrl": "分享链接",
+ "downloadOgImage": "下载预览图"
},
"success": {
"linkCopied": "链接已复制到剪贴板",
- "imageCopied": "图片已复制到剪贴板"
+ "imageCopied": "图片已复制到剪贴板",
+ "ogImageDownloaded": "预览图下载成功"
},
"error": {
"copyNotSupported": "当前浏览器不支持复制功能",
"shareFailed": "分享失败",
- "nativeShareFailed": "原生分享失败"
+ "nativeShareFailed": "原生分享失败",
+ "linkCopyFailed": "链接复制失败",
+ "ogImageDownloadFailed": "预览图下载失败"
+ },
+ "ogImage": {
+ "title": "分享预览图",
+ "alt": "照片分享预览图",
+ "loading": "加载中...",
+ "loadError": "加载失败"
},
"fallback": {
"photoTitle": "美丽的照片"
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index c316137d..74619066 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -75,16 +75,26 @@
"copyImage": "複製圖片",
"downloadImage": "下載原圖",
"generateShareImage": "產生分享圖片",
- "shareUrl": "分享連結"
+ "shareUrl": "分享連結",
+ "downloadOgImage": "下載預覽圖"
},
"success": {
"linkCopied": "連結已複製到剪貼簿",
- "imageCopied": "圖片已複製到剪貼簿"
+ "imageCopied": "圖片已複製到剪貼簿",
+ "ogImageDownloaded": "預覽圖下載成功"
},
"error": {
"copyNotSupported": "目前瀏覽器不支援複製功能",
"shareFailed": "分享失敗",
- "nativeShareFailed": "原生分享失敗"
+ "nativeShareFailed": "原生分享失敗",
+ "linkCopyFailed": "連結複製失敗",
+ "ogImageDownloadFailed": "預覽圖下載失敗"
+ },
+ "ogImage": {
+ "title": "分享預覽圖",
+ "alt": "相片分享預覽圖",
+ "loading": "載入中...",
+ "loadError": "載入失敗"
},
"fallback": {
"photoTitle": "美麗的相片"
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index e5b65ab9..5c958320 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -75,16 +75,26 @@
"copyImage": "複製圖片",
"downloadImage": "下載原圖",
"generateShareImage": "產生分享圖片",
- "shareUrl": "分享連結"
+ "shareUrl": "分享連結",
+ "downloadOgImage": "下載預覽圖"
},
"success": {
"linkCopied": "連結已複製到剪貼簿",
- "imageCopied": "圖片已複製到剪貼簿"
+ "imageCopied": "圖片已複製到剪貼簿",
+ "ogImageDownloaded": "預覽圖下載成功"
},
"error": {
"copyNotSupported": "目前瀏覽器不支援複製功能",
"shareFailed": "分享失敗",
- "nativeShareFailed": "原生分享失敗"
+ "nativeShareFailed": "原生分享失敗",
+ "linkCopyFailed": "連結複製失敗",
+ "ogImageDownloadFailed": "預覽圖下載失敗"
+ },
+ "ogImage": {
+ "title": "分享預覽圖",
+ "alt": "相片分享預覽圖",
+ "loading": "載入中...",
+ "loadError": "載入失敗"
},
"fallback": {
"photoTitle": "美麗的相片"