存储到data
This commit is contained in:
		
							
								
								
									
										24
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -7,6 +7,8 @@ import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -520,6 +522,13 @@ func main() {
 | 
			
		||||
	http.HandleFunc("/webp/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
 | 
			
		||||
		// 如果本地文件存在,直接输出
 | 
			
		||||
		filePath := filepath.Join("data/webp", r.URL.Path)
 | 
			
		||||
		if _, err := os.Stat(filePath); err != nil {
 | 
			
		||||
			http.ServeFile(w, r, filePath)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		reg := regexp.MustCompile(`^/webp/([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+).(jpg|jpeg|png|webp)$`)
 | 
			
		||||
		matches := reg.FindStringSubmatch(r.URL.Path)
 | 
			
		||||
		if len(matches) != 8 {
 | 
			
		||||
@@ -547,6 +556,21 @@ func main() {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 将生成的 WebP 文件保存到本地
 | 
			
		||||
		err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("创建文件目录失败:", err)
 | 
			
		||||
			w.WriteHeader(http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = os.WriteFile(filePath, data, 0644)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("保存文件失败:", err)
 | 
			
		||||
			w.WriteHeader(http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 返回 WebP 图片数据
 | 
			
		||||
		w.Header().Set("Content-Type", "image/webp")
 | 
			
		||||
		w.Header().Set("Cache-Control", "max-age=31536000")
 | 
			
		||||
		w.Write(data)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user