- 新增 respondError:JSON 安全编码,客户端错误返回 400,服务端错误返回 500 - 收编 book/content/media/category 中 13 处 fmt.Fprintf 内联 JSON,修复错误信息含引号/换行时产出非法 JSON 的问题 - search/detail/catalog/multi-detail/content/multi-content/item-summary/audio/video 补必需参数校验,缺参返回 400 - search 的 offset/tab_type 非数字时返回 400,不再静默置 0 - 补齐 multi-detail/multi-content 的请求统计计数 - 新增 error_response_test.go:3 个测试锁定错误响应行为 - openapi.yaml 新增 ServerError 响应组件,15 个业务接口补 400/500 响应定义 BREAKING CHANGE: 业务接口错误不再返回 HTTP 200,缺参返回 400,上游失败返回 500
137 lines
3.2 KiB
Go
137 lines
3.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fanqie/internal/fqapi"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// HandleAudio 音频处理
|
|
func HandleAudio(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "audio")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
params, err := validateParams(r, "item_ids")
|
|
if err != nil {
|
|
respondError(w, err.Error(), true)
|
|
return
|
|
}
|
|
|
|
toneID := r.URL.Query().Get("tone_id")
|
|
output := r.URL.Query().Get("output")
|
|
|
|
result, err := fqapi.HandleAudio(params["item_ids"], toneID, output)
|
|
if err != nil {
|
|
respondError(w, "获取音频失败: "+err.Error(), false)
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleAudioProxy 音频代理处理
|
|
func HandleAudioProxy(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total")
|
|
|
|
// 从URL路径提取key: /audio/proxy/{key}
|
|
path := r.URL.Path
|
|
key := strings.TrimPrefix(path, "/audio/proxy/")
|
|
if key == "" || key == path {
|
|
http.Error(w, "缺少音频key", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 调用代理处理函数
|
|
data, contentType, err := fqapi.HandleAudioProxy(key)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 设置正确的Content-Type
|
|
if contentType != "" {
|
|
w.Header().Set("Content-Type", contentType)
|
|
} else {
|
|
w.Header().Set("Content-Type", "audio/mpeg")
|
|
}
|
|
|
|
// 直接播放,不强制下载
|
|
// 浏览器会根据Content-Type决定是播放还是下载
|
|
w.Write(data)
|
|
}
|
|
|
|
// HandleVideo 视频处理
|
|
func HandleVideo(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "video")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
params, err := validateParams(r, "item_ids")
|
|
if err != nil {
|
|
respondError(w, err.Error(), true)
|
|
return
|
|
}
|
|
|
|
result, err := fqapi.HandleVideo(params["item_ids"])
|
|
if err != nil {
|
|
respondError(w, "获取视频失败: "+err.Error(), false)
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleManga 漫画处理
|
|
func HandleManga(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "manga")
|
|
|
|
itemID := r.URL.Query().Get("item_ids")
|
|
if itemID == "" {
|
|
itemID = r.URL.Query().Get("item_id")
|
|
}
|
|
if itemID == "" {
|
|
respondError(w, "缺少 item_ids 参数", true)
|
|
return
|
|
}
|
|
|
|
// 检查是否要返回HTML格式
|
|
showHTML := r.URL.Query().Get("show_html") == "1"
|
|
|
|
// 检查输出模式参数(优先级高于配置文件)
|
|
// 支持:output=url / output=proxy / output=data_uri
|
|
outputMode := r.URL.Query().Get("output")
|
|
|
|
result, err := fqapi.HandleGetManga(itemID, showHTML, outputMode)
|
|
if err != nil {
|
|
respondError(w, "获取漫画失败: "+err.Error(), false)
|
|
return
|
|
}
|
|
|
|
if showHTML {
|
|
// 返回HTML格式
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
if contentMap, ok := result.(map[string]interface{}); ok {
|
|
if content, ok := contentMap["content"].(string); ok {
|
|
fmt.Fprint(w, content)
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprint(w, `<p>未找到漫画内容</p>`)
|
|
} else {
|
|
// 返回JSON格式
|
|
jsonData, err := json.Marshal(map[string]interface{}{
|
|
"code": 0,
|
|
"msg": "success",
|
|
"data": result,
|
|
})
|
|
if err != nil {
|
|
respondError(w, "JSON编码失败: "+err.Error(), false)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
fmt.Fprint(w, string(jsonData))
|
|
}
|
|
}
|
|
|