非图片文件会302重定向到原始链接

This commit is contained in:
wood chen 2024-09-29 00:47:33 +08:00
parent 9629b19b9a
commit 34092a41b0

View File

@ -16,6 +16,8 @@ import (
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
@ -59,6 +61,17 @@ 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)
// 新增:检查是否为图片文件
if !isImageFile(filename) {
log.Infof("Non-image file requested: %s, redirecting to original URL", filename)
if proxyMode {
return c.Redirect(realRemoteAddr, 302)
} else {
return c.Redirect(reqURIwithQuery, 302)
}
}
if !helper.CheckAllowedType(filename) { if !helper.CheckAllowedType(filename) {
msg := "File extension not allowed! " + filename msg := "File extension not allowed! " + filename
log.Warn(msg) log.Warn(msg)
@ -186,3 +199,15 @@ func Convert(c *fiber.Ctx) error {
c.Set("X-Compression-Rate", helper.GetCompressionRate(rawImageAbs, finalFilename)) c.Set("X-Compression-Rate", helper.GetCompressionRate(rawImageAbs, finalFilename))
return c.SendFile(finalFilename) return c.SendFile(finalFilename)
} }
// 新增:检查文件是否为图片的辅助函数
func isImageFile(filename string) bool {
ext := strings.ToLower(path.Ext(filename))
allowedExtensions := []string{"jpg","png","jpeg","gif","bmp","svg","heic","nef",".webp",".tiff"}
for _, allowedExt := range allowedExtensions {
if ext == allowedExt {
return true
}
}
return false
}