Files
fanqie-go/internal/fqapi/manga_proxy.go
T
chenxin 5bcc8a962e ♻️ refactor(handlers): status 与 review 页面模板化
- HTML 提取至 templates/status.html、templates/review.html(go:embed 内嵌)
- status.go 1022→271 行、review.go 1281→438 行,动态数据全部经 html/template 按上下文自动转义
- 评论循环/作者徽章/贴纸/图片条件渲染翻译为模板 range/if;emojiMap 以 template.JS 注入
- 与旧版渲染输出实测对比:22 条评论结构完全一致;差异仅为 JS 上下文安全转义(& 等)与 html/template 剥除 CSS 注释,均无功能影响
- 其余文件为 gofmt 顺手格式化
2026-09-30 00:25:09 +08:00

72 lines
1.7 KiB
Go

package fqapi
import (
"encoding/hex"
"fmt"
"time"
)
// HandleMangaImageProxy 处理漫画图片代理请求
func HandleMangaImageProxy(itemID string, index int) ([]byte, string, error) {
// 从缓存获取图片信息
cacheKey := fmt.Sprintf("%s_%d", itemID, index)
mangaCacheMutex.RLock()
imageInfo, exists := mangaImageCache[cacheKey]
mangaCacheMutex.RUnlock()
if !exists {
return nil, "", fmt.Errorf("图片信息不存在,请先调用 /manga 接口")
}
// 检查缓存是否过期(5分钟)
if time.Since(imageInfo.CachedAt) > 5*time.Minute {
// 清理过期缓存
mangaCacheMutex.Lock()
delete(mangaImageCache, cacheKey)
mangaCacheMutex.Unlock()
return nil, "", fmt.Errorf("图片信息已过期,请重新调用 /manga 接口")
}
// 下载加密图片
encryptedData, err := downloadImageWithRetry(imageInfo.PicURL, 2)
if err != nil {
return nil, "", fmt.Errorf("下载图片失败: %w", err)
}
// 解密图片
keyBytes, err := hex.DecodeString(imageInfo.EncryptKey)
if err != nil {
return nil, "", fmt.Errorf("密钥解码失败: %w", err)
}
decryptedData, err := decryptImage(encryptedData, keyBytes)
if err != nil {
return nil, "", fmt.Errorf("解密失败: %w", err)
}
// 识别格式
format := getImageFormat(decryptedData, imageInfo.PicURL)
// 返回图片数据和MIME类型
mimeType := fmt.Sprintf("image/%s", format)
if format == "jpg" {
mimeType = "image/jpeg"
}
return decryptedData, mimeType, nil
}
// CleanExpiredMangaCache 清理过期的漫画图片缓存
func CleanExpiredMangaCache() {
mangaCacheMutex.Lock()
defer mangaCacheMutex.Unlock()
now := time.Now()
for key, info := range mangaImageCache {
if now.Sub(info.CachedAt) > 5*time.Minute {
delete(mangaImageCache, key)
}
}
}