refactor: 合并 NSFW 数据到 media_info 表

This commit is contained in:
2025-07-26 21:05:51 +08:00
parent 95e6297076
commit 59b0206cf1
12 changed files with 159 additions and 463 deletions
+36
View File
@@ -6,6 +6,9 @@ package api
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/dustin/go-humanize"
"github.com/gin-gonic/gin"
@@ -34,6 +37,7 @@ type mediaListReq struct {
Codec string `json:"codec"`
Format string `json:"format"`
Resolution string `json:"resolution"`
NsfwOnly interface{} `json:"nsfwOnly"`
SortField string `json:"sortField"`
SortOrder string `json:"sortOrder"`
}
@@ -65,6 +69,7 @@ func (c *MediaController) List(ctx *gin.Context) {
Codec: req.Codec,
Format: req.Format,
Resolution: req.Resolution,
NsfwOnly: req.NsfwOnly == true, // interface{} 转 bool:只有明确的 true 才算
Page: req.Page,
PageSize: req.PageSize,
SortField: req.SortField,
@@ -101,6 +106,37 @@ func (c *MediaController) Stats(ctx *gin.Context) {
Success(ctx, st)
}
// Preview 处理 GET /api/media/preview?path=xxx:预览图片文件。
// 只允许预览图片类型(jpeg/jpg/png/gif/webp/bmp),其他类型返回 403。
func (c *MediaController) Preview(ctx *gin.Context) {
path := strings.TrimSpace(ctx.Query("path"))
if path == "" {
Error(ctx, http.StatusBadRequest, "缺少 path 参数")
return
}
// 检查文件是否存在
info, err := os.Stat(path)
if err != nil {
Error(ctx, http.StatusNotFound, "文件不存在: "+path)
return
}
// 只允许预览图片
ext := strings.ToLower(filepath.Ext(info.Name()))
allowedExts := map[string]bool{
".jpg": true, ".jpeg": true, ".png": true,
".gif": true, ".webp": true, ".bmp": true,
}
if !allowedExts[ext] {
Error(ctx, http.StatusForbidden, "仅支持预览图片文件")
return
}
// 返回图片文件
ctx.File(path)
}
// toMediaView 给一条数据库记录补上展示用的派生字段。
func toMediaView(m store.MediaInfo) mediaView {
v := mediaView{MediaInfo: m}