如果本地文件存在,直接输出

This commit is contained in:
2024-12-31 07:07:03 +08:00
parent 7799298282
commit 7eb3974abb

View File

@@ -490,6 +490,7 @@ func main() {
reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`) reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`)
matches := reg.FindStringSubmatch(r.URL.Path) matches := reg.FindStringSubmatch(r.URL.Path)
if len(matches) != 4 { if len(matches) != 4 {
log.Println("URL 格式错误", r.URL.Path)
http.Error(w, "URL 格式错误", http.StatusNotFound) http.Error(w, "URL 格式错误", http.StatusNotFound)
return return
} }
@@ -524,7 +525,7 @@ func main() {
// 如果本地文件存在,直接输出 // 如果本地文件存在,直接输出
filePath := filepath.Join("data/webp", r.URL.Path) filePath := filepath.Join("data/webp", r.URL.Path)
if _, err := os.Stat(filePath); err != nil { if _, err := os.Stat(filePath); err == nil {
http.ServeFile(w, r, filePath) http.ServeFile(w, r, filePath)
return return
} }