feat: add favicon settings to site configuration

- Add Favicon field to SiteSetting model with FaviconIsURL() helper
- Implement favicon upload and management in site settings page
- Support both external URL and local file upload for favicon
- Add favicon display in page header with automatic URL detection
- Add i18n translations for favicon settings (EN/ZH)
- Update middleware and helpers to pass favicon data to templates
- Include migration script for existing databases

Users can now customize their site's favicon from /admin/settings/site
by either providing an external URL or uploading a local image file
(recommended formats: .ico, .png, .svg, 16x16 or 32x32 pixels).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 20:09:05 +08:00
co-authored by Claude Fable 5
parent da7a39c1c8
commit b600eac21c
9 changed files with 193 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
# Favicon 设置功能更新
## 概述
在站点设置页面 (`/admin/settings/site`) 添加了 Favicon(网站图标)设置功能。
## 更改的文件
### 1. 数据库模型 (models/site_setting.go)
- 添加了 `Favicon` 字段到 `SiteSetting` 结构体
- 添加了 `FaviconIsURL()` 方法来判断 Favicon 是外链还是本地文件
### 2. 处理器 (handlers/settings.go)
- `SiteSettingsPage`: 添加 `SiteFaviconIsURL` 到模板数据
- `SiteSettingsSave`: 添加 Favicon 上传和保存逻辑,支持:
- 外链 URL 设置
- 本地文件上传(.ico, .png, .svg
- 清除当前 Favicon
### 3. 模板 (templates/admin/settings_site.html)
- 添加 Favicon 设置表单区域
- 显示当前 Favicon 预览
- 支持 URL 输入和文件上传
- 添加清除 Favicon 的复选框
### 4. 国际化 (i18n/i18n.go)
添加了以下翻译键:
- `settings_favicon`: Favicon / 网站图标(Favicon
- `settings_favicon_url`: Favicon URL (external link) / Favicon 链接(外链地址)
- `settings_favicon_current`: Current favicon / 当前 Favicon
- `settings_favicon_hint`: 格式提示
- `settings_favicon_clear`: Remove current favicon / 移除当前 Favicon
### 5. 中间件 (middleware/auth.go)
-`SetUserContext` 中添加 `site_favicon``site_favicon_is_url` 到上下文
### 6. 辅助函数 (handlers/helpers.go)
-`DefaultData` 中添加 `SiteFavicon``SiteFaviconIsURL` 到模板数据
### 7. 页面模板 (templates/layouts/base.html)
-`<head>` 中添加 Favicon 的 `<link>` 标签
- 根据设置自动使用外链或本地文件
## 数据库迁移
如果数据库已存在,需要手动添加 `favicon` 字段:
```sql
ALTER TABLE site_settings ADD COLUMN favicon VARCHAR(512) DEFAULT '';
```
或者运行提供的迁移脚本:
```bash
sqlite3 data/blog.db < scripts/add_favicon_field.sql
```
**注意**: GORM 的 `AutoMigrate` 会在下次启动时自动添加新字段,无需手动执行 SQL。
## 使用说明
1. 访问 `/admin/settings/site` 页面
2. 在 "网站图标(Favicon" 部分:
- 方式一:输入外链 URL(如 CDN 链接)
- 方式二:上传本地图片文件(推荐 .ico, .png 或 .svg 格式,32x32 或 16x16 像素)
3. 点击 "保存" 按钮
4. Favicon 将自动应用到网站所有页面
## 文件存储
- 上传的 Favicon 文件保存在 `{storage_path}/logos/` 目录下
- 文件名固定为 `favicon.{ext}`(如 favicon.ico, favicon.png
- 每次上传新文件时会自动删除旧文件
## 技术细节
### 支持的格式
- ICO (.ico) - 传统格式,兼容性最好
- PNG (.png) - 现代浏览器支持
- SVG (.svg) - 矢量格式,适合高分辨率显示
### 优先级
1. 如果同时设置了 URL 和上传文件,URL 优先
2. 如果勾选 "移除当前 Favicon",会删除现有设置
### 缓存
- Favicon 设置存储在数据库中
- 通过 `models.RefreshConfigCache()` 刷新内存缓存
- 更改后立即生效,无需重启服务