Add files via upload

This commit is contained in:
QWQLwToo
2026-01-28 13:25:18 +08:00
committed by GitHub
parent 02f66ec447
commit 7030903eb6
77 changed files with 21573 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
package config
import (
"os"
"path/filepath"
)
type Config struct {
DataDir string
DatabasePath string
UploadDir string
JWTSecret string
}
func New(dataDir string) *Config {
// 确保data目录存在
os.MkdirAll(dataDir, 0755)
// 创建上传目录
uploadDir := filepath.Join(dataDir, "uploads")
os.MkdirAll(uploadDir, 0755)
// 从环境变量获取JWT密钥,如果没有则使用默认值
jwtSecret := os.Getenv("JWT_SECRET")
if jwtSecret == "" {
jwtSecret = "your-secret-key-change-in-production"
}
return &Config{
DataDir: dataDir,
DatabasePath: filepath.Join(dataDir, "home.db"),
UploadDir: uploadDir,
JWTSecret: jwtSecret,
}
}
+58
View File
@@ -0,0 +1,58 @@
package config
import (
"encoding/json"
"os"
"path/filepath"
)
// FooterYearConfig 用于存储底部版权年份配置
type FooterYearConfig struct {
Start string `json:"start"`
End string `json:"end"`
}
// footerYearConfigPath 返回年份配置文件路径
func (c *Config) footerYearConfigPath() string {
return filepath.Join(c.DataDir, "footer_year.json")
}
// LoadFooterYear 读取底部年份配置(文件不存在时返回空配置而不是报错)
func (c *Config) LoadFooterYear() (*FooterYearConfig, error) {
path := c.footerYearConfigPath()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// 没有配置文件时返回空配置
return &FooterYearConfig{}, nil
}
return nil, err
}
var cfg FooterYearConfig
if err := json.Unmarshal(data, &cfg); err != nil {
// 解析失败时返回空配置,但把错误也返回方便日志排查
return &FooterYearConfig{}, err
}
return &cfg, nil
}
// SaveFooterYear 保存底部年份配置
func (c *Config) SaveFooterYear(cfg *FooterYearConfig) error {
if cfg == nil {
cfg = &FooterYearConfig{}
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
path := c.footerYearConfigPath()
if err := os.WriteFile(path, data, 0644); err != nil {
return err
}
return nil
}
+85
View File
@@ -0,0 +1,85 @@
package config
import (
"encoding/json"
"os"
"path/filepath"
)
// RotatingTextsConfig 轮换文本配置
type RotatingTextsConfig struct {
Texts []string `json:"texts"`
}
// rotatingTextsConfigPath 返回轮换文本配置文件路径
func (c *Config) rotatingTextsConfigPath() string {
return filepath.Join(c.DataDir, "rotating_texts.json")
}
// LoadRotatingTexts 加载轮换文本配置
func (c *Config) LoadRotatingTexts() (*RotatingTextsConfig, error) {
path := c.rotatingTextsConfigPath()
// 如果文件不存在,返回默认值
if _, err := os.Stat(path); os.IsNotExist(err) {
defaultTexts := &RotatingTextsConfig{
Texts: []string{
"你好鸭,欢迎来到我的主页!!",
"随时可以联系我,期待与你交流。",
"愿你历尽千帆,归来仍是少年。",
"梦想还是要有的,万一实现了呢?",
"I hope you have a happy day every day.",
},
}
return defaultTexts, nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg RotatingTextsConfig
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, err
}
// 确保至少有默认文本
if len(cfg.Texts) == 0 {
cfg.Texts = []string{
"你好鸭,欢迎来到我的主页!!",
"随时可以联系我,期待与你交流。",
"愿你历尽千帆,归来仍是少年。",
"梦想还是要有的,万一实现了呢?",
"I hope you have a happy day every day.",
}
}
return &cfg, nil
}
// SaveRotatingTexts 保存轮换文本配置
func (c *Config) SaveRotatingTexts(cfg *RotatingTextsConfig) error {
path := c.rotatingTextsConfigPath()
// 限制最多8条文本
if len(cfg.Texts) > 8 {
cfg.Texts = cfg.Texts[:8]
}
// 过滤空文本
filteredTexts := make([]string, 0, len(cfg.Texts))
for _, text := range cfg.Texts {
if text != "" {
filteredTexts = append(filteredTexts, text)
}
}
cfg.Texts = filteredTexts
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
+56
View File
@@ -0,0 +1,56 @@
package config
import (
"encoding/json"
"os"
"path/filepath"
)
// VisitTimerConfig 用于存储访问时间显示配置
type VisitTimerConfig struct {
ShowVisitTimer bool `json:"showVisitTimer"`
}
// visitTimerConfigPath 返回访问时间配置文件路径
func (c *Config) visitTimerConfigPath() string {
return filepath.Join(c.DataDir, "visit_timer.json")
}
// LoadVisitTimer 读取访问时间显示配置(文件不存在时返回默认配置)
func (c *Config) LoadVisitTimer() (*VisitTimerConfig, error) {
path := c.visitTimerConfigPath()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// 没有配置文件时返回默认配置(显示)
return &VisitTimerConfig{ShowVisitTimer: true}, nil
}
return nil, err
}
var cfg VisitTimerConfig
if err := json.Unmarshal(data, &cfg); err != nil {
// 解析失败时返回默认配置
return &VisitTimerConfig{ShowVisitTimer: true}, err
}
return &cfg, nil
}
// SaveVisitTimer 保存访问时间显示配置
func (c *Config) SaveVisitTimer(cfg *VisitTimerConfig) error {
if cfg == nil {
cfg = &VisitTimerConfig{ShowVisitTimer: true}
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
path := c.visitTimerConfigPath()
if err := os.WriteFile(path, data, 0644); err != nil {
return err
}
return nil
}