From 3a7c57288190db5832d85c8cf43207f888056429 Mon Sep 17 00:00:00 2001 From: vastsa Date: Mon, 16 Nov 2026 15:45:01 +0800 Subject: [PATCH] feat: support custom background image (vastsa/FileCodeBox#546) --- src/composables/index.ts | 1 + src/composables/useSiteBackground.ts | 23 +++++++++++++++ src/i18n/locales/en-US.ts | 3 ++ src/i18n/locales/zh-CN.ts | 3 ++ src/utils/site-background.ts | 14 ++++++++++ src/views/RetrievewFileView.vue | 15 ++++++++-- src/views/SendFileView.vue | 15 ++++++++-- src/views/manage/SystemSettingsView.vue | 24 ++++++++++++++++ tests/site-background.test.mjs | 37 +++++++++++++++++++++++++ 9 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 src/composables/useSiteBackground.ts create mode 100644 src/utils/site-background.ts create mode 100644 tests/site-background.test.mjs diff --git a/src/composables/index.ts b/src/composables/index.ts index 476810b..c29ac82 100644 --- a/src/composables/index.ts +++ b/src/composables/index.ts @@ -15,3 +15,4 @@ export { useMcpSettings } from './useMcpSettings' export { useTheme } from './useTheme' export { provideAdminHeaderActions, useAdminHeaderActions } from './useAdminHeaderActions' export type { AdminHeaderAction, AdminHeaderConfig } from './useAdminHeaderActions' +export { useSiteBackground } from './useSiteBackground' diff --git a/src/composables/useSiteBackground.ts b/src/composables/useSiteBackground.ts new file mode 100644 index 0000000..bb39cc6 --- /dev/null +++ b/src/composables/useSiteBackground.ts @@ -0,0 +1,23 @@ +import { computed, type CSSProperties } from 'vue' +import { storeToRefs } from 'pinia' +import { useConfigStore } from '@/stores/configStore' +import { normalizeBackgroundUrl } from '@/utils/site-background' + +export function useSiteBackground() { + const { config } = storeToRefs(useConfigStore()) + + const backgroundUrl = computed(() => normalizeBackgroundUrl(config.value.background)) + const hasBackground = computed(() => backgroundUrl.value !== '') + const backgroundStyle = computed(() => + hasBackground.value + ? { + backgroundImage: `url("${backgroundUrl.value}")`, + backgroundSize: 'cover', + backgroundPosition: 'center', + backgroundRepeat: 'no-repeat' + } + : {} + ) + + return { backgroundUrl, hasBackground, backgroundStyle } +} diff --git a/src/i18n/locales/en-US.ts b/src/i18n/locales/en-US.ts index b95de3d..25890e7 100644 --- a/src/i18n/locales/en-US.ts +++ b/src/i18n/locales/en-US.ts @@ -751,6 +751,9 @@ export default { passwordNote: 'Leave empty to keep current', keywords: 'SEO Keywords', themeSelection: 'Theme', + backgroundImage: 'Background Image', + backgroundImagePlaceholder: 'https://example.com/background.jpg', + backgroundImageHelp: 'Shown behind the send and retrieve pages. Use a full http(s) image URL, or leave empty for the default background.', notificationSettings: 'Notifications', notificationTitle: 'Announcement Title', notificationContent: 'Announcement Content', diff --git a/src/i18n/locales/zh-CN.ts b/src/i18n/locales/zh-CN.ts index f137885..6efbb8a 100644 --- a/src/i18n/locales/zh-CN.ts +++ b/src/i18n/locales/zh-CN.ts @@ -782,6 +782,9 @@ export default { passwordNote: '不修改请留空', keywords: 'SEO 关键词', themeSelection: '界面主题', + backgroundImage: '背景图片', + backgroundImagePlaceholder: 'https://example.com/background.jpg', + backgroundImageHelp: '用于发送页和取件页背景,需填写完整的 http(s) 图片地址,留空则使用默认背景。', robotsFile: 'Robots.txt 配置', notificationSettings: '通知设置', notificationTitle: '公告标题', diff --git a/src/utils/site-background.ts b/src/utils/site-background.ts new file mode 100644 index 0000000..f732694 --- /dev/null +++ b/src/utils/site-background.ts @@ -0,0 +1,14 @@ +const UNSAFE_CHARS = /["'()\\;\s<>]/ + +/** 仅接受 http(s) 完整 URL 或站内绝对路径(兼容旧配置),并拒绝可能逃逸 CSS url() 的字符。 */ +export function normalizeBackgroundUrl(value: unknown): string { + const url = String(value ?? '').trim() + if (!url || UNSAFE_CHARS.test(url)) return '' + if (url.startsWith('/') && !url.startsWith('//')) return url + try { + const parsed = new URL(url) + return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? url : '' + } catch { + return '' + } +} diff --git a/src/views/RetrievewFileView.vue b/src/views/RetrievewFileView.vue index 4fe1664..d440eeb 100644 --- a/src/views/RetrievewFileView.vue +++ b/src/views/RetrievewFileView.vue @@ -1,8 +1,18 @@