45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fanqie/internal/fqapi"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// HandleMangaImageProxy 处理漫画图片代理请求
|
|
func HandleMangaImageProxy(w http.ResponseWriter, r *http.Request) {
|
|
// 从URL路径提取参数: /manga/image/{itemID}/{index}
|
|
path := strings.TrimPrefix(r.URL.Path, "/manga/image/")
|
|
parts := strings.Split(path, "/")
|
|
|
|
if len(parts) != 2 {
|
|
http.Error(w, "Invalid URL format. Expected: /manga/image/{itemID}/{index}", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
itemID := parts[0]
|
|
index, err := strconv.Atoi(parts[1])
|
|
if err != nil {
|
|
http.Error(w, "Invalid index parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 获取解密后的图片数据
|
|
imageData, mimeType, err := fqapi.HandleMangaImageProxy(itemID, index)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to get image: %s", err.Error()), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// 设置响应头
|
|
w.Header().Set("Content-Type", mimeType)
|
|
w.Header().Set("Cache-Control", "public, max-age=3600") // 缓存1小时
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(imageData)))
|
|
|
|
// 返回图片数据
|
|
w.Write(imageData)
|
|
}
|
|
|