尝试修复非图片文件的重定向问题,并优化了代码结构。

This commit is contained in:
wood chen 2024-09-29 15:17:45 +08:00
parent 0b494e08ac
commit 585f9a52b8
2 changed files with 45 additions and 49 deletions

View File

@ -1,4 +1,4 @@
FROM golang:1.23-bookworm as builder FROM golang:1.23-bookworm AS builder
ARG IMG_PATH=/opt/pics ARG IMG_PATH=/opt/pics
ARG EXHAUST_PATH=/opt/exhaust ARG EXHAUST_PATH=/opt/exhaust

View File

@ -3,21 +3,19 @@ package handler
import ( import (
"net/http" "net/http"
"net/url" "net/url"
"path"
"regexp" "regexp"
"slices"
"strconv"
"strings" "strings"
"webp_server_go/config" "webp_server_go/config"
"webp_server_go/encoder" "webp_server_go/encoder"
"webp_server_go/helper" "webp_server_go/helper"
"slices"
"path"
"strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func Convert(c *fiber.Ctx) error { func Convert(c *fiber.Ctx) error {
// this function need to do: // this function need to do:
// 1. get request path, query string // 1. get request path, query string
@ -61,53 +59,51 @@ func Convert(c *fiber.Ctx) error {
log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery) log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery)
// 非图片清况下302到源文件 // 非图片清况下302到源文件
// //
// //
// //
reqURI := c.Path()
filename := path.Base(reqURI)
// 处理根路径请求 // 处理根路径请求
if reqURI == "/" { if reqURI == "/" {
// 重定向到一个适当的页面或返回一个默认响应 // 重定向到一个适当的页面或返回一个默认响应
return c.SendString("Welcome to WebP Server") return c.SendString("Welcome to WebP Server")
} }
if !isImageFile(filename) { if !isImageFile(filename) {
log.Infof("Non-image file requested: %s, redirecting to original URL", reqURI) log.Infof("Non-image file requested: %s, redirecting to original URL", reqURI)
var redirectURL string var redirectURL string
// 检查是否存在匹配的 IMG_MAP // 检查是否存在匹配的 IMG_MAP
for prefix, target := range config.Config.ImageMap { for prefix, target := range config.Config.ImageMap {
if strings.HasPrefix(reqURI, prefix) { if strings.HasPrefix(reqURI, prefix) {
// 构造重定向 URL // 构造重定向 URL
redirectURL = target + strings.TrimPrefix(reqURI, prefix) redirectURL = target + strings.TrimPrefix(reqURI, prefix)
break break
} }
} }
// 如果没有找到匹配的 IMG_MAP使用默认的处理方式 // 如果没有找到匹配的 IMG_MAP使用默认的处理方式
if redirectURL == "" { if redirectURL == "" {
if proxyMode { if proxyMode {
redirectURL = realRemoteAddr redirectURL = realRemoteAddr
} else { } else {
// 使用完整的请求路径,而不是追加到 ImgPath // 使用完整的请求路径,而不是追加到 ImgPath
redirectURL = path.Join("/", reqURI) redirectURL = path.Join("/", reqURI)
} }
} }
// 确保重定向 URL 不会导致循环 // 确保重定向 URL 不会导致循环
if redirectURL == reqURI || redirectURL == path.Join("/", reqURI) { if redirectURL == reqURI || redirectURL == path.Join("/", reqURI) {
return c.SendStatus(fiber.StatusNotFound) return c.SendStatus(fiber.StatusNotFound)
} }
log.Infof("Redirecting to: %s", redirectURL) log.Infof("Redirecting to: %s", redirectURL)
return c.Redirect(redirectURL, fiber.StatusFound) return c.Redirect(redirectURL, fiber.StatusFound)
} }
// //
// //
if !helper.CheckAllowedType(filename) { if !helper.CheckAllowedType(filename) {
msg := "File extension not allowed! " + filename msg := "File extension not allowed! " + filename
@ -241,14 +237,14 @@ func Convert(c *fiber.Ctx) error {
func isImageFile(filename string) bool { func isImageFile(filename string) bool {
ext := strings.ToLower(path.Ext(filename)) ext := strings.ToLower(path.Ext(filename))
if ext == "" { if ext == "" {
return false return false
} }
ext = ext[1:] // 移除开头的点 ext = ext[1:] // 移除开头的点
allowedTypes := config.Config.AllowedTypes allowedTypes := config.Config.AllowedTypes
if len(allowedTypes) == 1 && allowedTypes[0] == "*" { if len(allowedTypes) == 1 && allowedTypes[0] == "*" {
// 如果允许所有类型,则使用默认的图片类型列表 // 如果允许所有类型,则使用默认的图片类型列表
allowedTypes = config.NewWebPConfig().AllowedTypes allowedTypes = config.NewWebPConfig().AllowedTypes
} }
return slices.Contains(allowedTypes, ext) return slices.Contains(allowedTypes, ext)