mirror of
https://github.com/vastsa/FileCodeBoxFronted.git
synced 2026-11-24 21:58:01 +08:00
feat: support custom background image (vastsa/FileCodeBox#546)
This commit is contained in:
1 parent
2d6173e0d4
commit
3a7c572881
9 files changed
+131
-4
No files matched your search
@@ -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'
|
||||
@@ -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<CSSProperties>(() =>
|
||||
hasBackground.value
|
||||
? {
|
||||
backgroundImage: `url("${backgroundUrl.value}")`,
|
||||
backgroundSize: 'cover',
|
||||
backgroundPosition: 'center',
|
||||
backgroundRepeat: 'no-repeat'
|
||||
}
|
||||
: {}
|
||||
)
|
||||
|
||||
return { backgroundUrl, hasBackground, backgroundStyle }
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -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: '公告标题',
|
||||
|
||||
@@ -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 ''
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,18 @@
|
||||
<template>
|
||||
<div
|
||||
class="relative flex min-h-screen flex-col items-center justify-center overflow-hidden p-4 font-sans transition-colors duration-700 sm:p-8"
|
||||
:class="isDarkMode ? 'bg-zinc-950 text-zinc-100' : 'bg-[#f5f5f7] text-zinc-900'"
|
||||
:class="[
|
||||
isDarkMode ? 'text-zinc-100' : 'text-zinc-900',
|
||||
hasBackground ? '' : isDarkMode ? 'bg-zinc-950' : 'bg-[#f5f5f7]'
|
||||
]"
|
||||
:style="backgroundStyle"
|
||||
>
|
||||
<div
|
||||
v-if="hasBackground"
|
||||
class="pointer-events-none absolute inset-0"
|
||||
:class="isDarkMode ? 'bg-zinc-950/45' : 'bg-white/20'"
|
||||
aria-hidden="true"
|
||||
></div>
|
||||
<div class="relative z-10 w-full max-w-md">
|
||||
<div
|
||||
class="relative overflow-hidden rounded-[2rem] border backdrop-blur-3xl transition-all duration-500 sm:rounded-[2.5rem]"
|
||||
@@ -84,9 +94,10 @@ import FileRecordList from '@/components/common/FileRecordList.vue'
|
||||
import ContentPreviewModal from '@/components/common/ContentPreviewModal.vue'
|
||||
import MediaPreviewModal from '@/components/common/MediaPreviewModal.vue'
|
||||
import { useRetrieveFlow } from '@/composables'
|
||||
import { useInjectedDarkMode } from '@/composables'
|
||||
import { useInjectedDarkMode, useSiteBackground } from '@/composables'
|
||||
|
||||
const isDarkMode = useInjectedDarkMode()
|
||||
const { hasBackground, backgroundStyle } = useSiteBackground()
|
||||
const { t } = useI18n()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
<template>
|
||||
<div
|
||||
class="relative flex min-h-screen flex-col items-center justify-center overflow-hidden p-4 font-sans transition-colors duration-700 sm:p-8"
|
||||
:class="isDarkMode ? 'bg-zinc-950 text-zinc-100' : 'bg-[#f5f5f7] text-zinc-900'"
|
||||
:class="[
|
||||
isDarkMode ? 'text-zinc-100' : 'text-zinc-900',
|
||||
hasBackground ? '' : isDarkMode ? 'bg-zinc-950' : 'bg-[#f5f5f7]'
|
||||
]"
|
||||
:style="backgroundStyle"
|
||||
@paste.prevent="handlePaste"
|
||||
>
|
||||
<div
|
||||
v-if="hasBackground"
|
||||
class="pointer-events-none absolute inset-0"
|
||||
:class="isDarkMode ? 'bg-zinc-950/45' : 'bg-white/20'"
|
||||
aria-hidden="true"
|
||||
></div>
|
||||
<div class="relative z-10 w-full max-w-md">
|
||||
<div
|
||||
class="relative overflow-hidden rounded-[2rem] border backdrop-blur-3xl transition-all duration-500 sm:rounded-[2.5rem]"
|
||||
@@ -191,10 +201,11 @@ import TextInputArea from '@/components/common/TextInputArea.vue'
|
||||
import SideDrawer from '@/components/common/SideDrawer.vue'
|
||||
import SentRecordList from '@/components/common/SentRecordList.vue'
|
||||
import SentRecordDetailModal from '@/components/common/SentRecordDetailModal.vue'
|
||||
import { useInjectedDarkMode, useSendFlow } from '@/composables'
|
||||
import { useInjectedDarkMode, useSendFlow, useSiteBackground } from '@/composables'
|
||||
import { ROUTES } from '@/constants'
|
||||
|
||||
const isDarkMode = useInjectedDarkMode()
|
||||
const { hasBackground, backgroundStyle } = useSiteBackground()
|
||||
const { t } = useI18n()
|
||||
const router = useRouter()
|
||||
const {
|
||||
|
||||
@@ -216,6 +216,30 @@ onMounted(() => {
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label
|
||||
for="site-background"
|
||||
class="block text-sm font-medium"
|
||||
:class="[isDarkMode ? 'text-zinc-300' : 'text-zinc-700']"
|
||||
>
|
||||
{{ t('manage.settings.backgroundImage') }}
|
||||
</label>
|
||||
<input
|
||||
id="site-background"
|
||||
type="url"
|
||||
v-model.trim="config.background"
|
||||
:placeholder="t('manage.settings.backgroundImagePlaceholder')"
|
||||
class="w-full rounded-md shadow-sm px-4 py-2.5 transition-all duration-200 ease-in-out border focus:ring-2 focus:ring-zinc-500 focus:border-zinc-500 outline-none"
|
||||
:class="[
|
||||
isDarkMode
|
||||
? 'border-white/10 bg-zinc-950/45 text-white placeholder-zinc-500 hover:border-white/20'
|
||||
: 'border-zinc-200/80 bg-white/80 text-zinc-950 placeholder-zinc-400 hover:border-zinc-300'
|
||||
]"
|
||||
/>
|
||||
<p class="mt-2 text-xs" :class="[isDarkMode ? 'text-zinc-400' : 'text-zinc-500']">
|
||||
{{ t('manage.settings.backgroundImageHelp') }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label
|
||||
class="block text-sm font-medium"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { Buffer } from 'node:buffer'
|
||||
import { test } from 'node:test'
|
||||
import { URL } from 'node:url'
|
||||
import ts from 'typescript'
|
||||
|
||||
const source = readFileSync(new URL('../src/utils/site-background.ts', import.meta.url), 'utf8')
|
||||
const { outputText } = ts.transpileModule(source, {
|
||||
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext }
|
||||
})
|
||||
const { normalizeBackgroundUrl } = await import(
|
||||
`data:text/javascript;base64,${Buffer.from(outputText).toString('base64')}`
|
||||
)
|
||||
|
||||
test('accepts http(s) and same-origin absolute paths', () => {
|
||||
assert.equal(normalizeBackgroundUrl(' https://example.com/bg.jpg '), 'https://example.com/bg.jpg')
|
||||
assert.equal(normalizeBackgroundUrl('http://example.com/a.png?x=1'), 'http://example.com/a.png?x=1')
|
||||
assert.equal(normalizeBackgroundUrl('/assets/bg.webp'), '/assets/bg.webp')
|
||||
})
|
||||
|
||||
test('rejects empty, non-http and CSS breakout values', () => {
|
||||
for (const value of [
|
||||
'',
|
||||
null,
|
||||
undefined,
|
||||
'javascript:alert(1)',
|
||||
'data:image/png;base64,AAAA',
|
||||
'//evil.example/bg.jpg',
|
||||
'https://example.com/a.jpg")',
|
||||
"https://example.com/a'.jpg",
|
||||
'https://example.com/a b.jpg',
|
||||
'relative/bg.jpg'
|
||||
]) {
|
||||
assert.equal(normalizeBackgroundUrl(value), '', String(value))
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user