mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 23:16:37 +08:00
图片压缩
This commit is contained in:
@@ -8,4 +8,6 @@ const (
|
||||
detectContentBytes = 512 // http.DetectContentType 需要的最小字节数
|
||||
uploadDirPerm = 0755 // 上传目录权限
|
||||
uploadFilePerm = 0644 // 上传文件权限
|
||||
cacheDirPerm = 0750 // 缓存目录权限 (gosec)
|
||||
cacheFilePerm = 0600 // 缓存文件权限 (gosec)
|
||||
)
|
||||
|
||||
@@ -5,15 +5,22 @@
|
||||
package upload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/oauth"
|
||||
"github.com/Rain-kl/Wavelet/internal/common"
|
||||
"github.com/Rain-kl/Wavelet/internal/db"
|
||||
"github.com/Rain-kl/Wavelet/internal/logger"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -26,6 +33,8 @@ import (
|
||||
// @Tags upload
|
||||
// @Produce octet-stream
|
||||
// @Param id path string true "文件 ID"
|
||||
// @Param compress query string false "是否启用压缩 (传任意非空值代表启用,非图片文件将被忽略)"
|
||||
// @Param level query string false "压缩质量等级 (low, medium, high),默认为 high"
|
||||
// @Success 200 {file} file "成功获取文件内容"
|
||||
// @Failure 400 {object} util.ResponseAny "文件 ID 格式错误"
|
||||
// @Failure 401 {object} util.ResponseAny "未登录"
|
||||
@@ -33,24 +42,16 @@ import (
|
||||
// @Failure 500 {object} util.ResponseAny "服务内部错误"
|
||||
// @Router /f/{id} [get]
|
||||
func ServeFileByID(c *gin.Context) {
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("Content-Security-Policy", "sandbox")
|
||||
|
||||
idStr := c.Param("id")
|
||||
uploadID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
upload, err := getUploadRecordByID(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid upload ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var upload model.Upload
|
||||
if err := db.DB(c.Request.Context()).
|
||||
Where("id = ? AND status IN (?, ?)", uploadID, model.UploadStatusPending, model.UploadStatusUsed).
|
||||
First(&upload).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if _, ok := err.(*strconv.NumError); ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid upload ID"})
|
||||
return
|
||||
}
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -61,6 +62,87 @@ func ServeFileByID(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ServeUpload(c, upload)
|
||||
}
|
||||
|
||||
// getUploadRecordByID 从请求路径参数中解析文件 ID 并从数据库中检索处于 Pending 或 Used 状态的上传记录。
|
||||
// 同时会自动设置通用的安全响应头。
|
||||
func getUploadRecordByID(c *gin.Context) (*model.Upload, error) {
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("Content-Security-Policy", "sandbox")
|
||||
|
||||
idStr := c.Param("id")
|
||||
uploadID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var upload model.Upload
|
||||
if err := db.DB(c.Request.Context()).
|
||||
Where("id = ? AND status IN (?, ?)", uploadID, model.UploadStatusPending, model.UploadStatusUsed).
|
||||
First(&upload).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &upload, nil
|
||||
}
|
||||
|
||||
// ServeUpload 将已存在的文件内容读取并流式响应给客户端,支持本地和 S3/CDN 驱动,并可选支持 WebP 图片压缩与本地缓存。
|
||||
func ServeUpload(c *gin.Context, upload *model.Upload) {
|
||||
compressStr := c.Query("compress")
|
||||
isImage := strings.HasPrefix(strings.ToLower(upload.MimeType), "image/") || isImageExtension(strings.ToLower(upload.Extension))
|
||||
|
||||
if compressStr == "" || !isImage {
|
||||
serveOriginal(c, upload)
|
||||
return
|
||||
}
|
||||
|
||||
// Map level parameter to standard options
|
||||
level := strings.ToLower(c.Query("level"))
|
||||
if level != "low" && level != "medium" && level != "high" {
|
||||
level = "high"
|
||||
}
|
||||
|
||||
// Local cache path for the compressed webp image
|
||||
cachePath := filepath.Join("uploads", "cache", fmt.Sprintf("compressed_%d_%s.webp", upload.ID, level))
|
||||
|
||||
// Check if the compressed file already exists in cache
|
||||
if _, err := os.Stat(cachePath); err == nil {
|
||||
c.Header("Content-Type", "image/webp")
|
||||
c.File(cachePath)
|
||||
return
|
||||
}
|
||||
|
||||
// Cache miss: retrieve original file content
|
||||
origBytes, err := getOriginalFileBytes(c.Request.Context(), upload)
|
||||
if err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "failed to retrieve original file bytes for compression: %v", err)
|
||||
serveOriginal(c, upload)
|
||||
return
|
||||
}
|
||||
|
||||
// Compress to WebP
|
||||
webpBytes, err := CompressImageToWebP(bytes.NewReader(origBytes), level)
|
||||
if err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "failed to compress image to WebP: %v", err)
|
||||
serveOriginal(c, upload)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure cache directory exists and write cached file
|
||||
if err := os.MkdirAll(filepath.Dir(cachePath), cacheDirPerm); err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "failed to create cache directory: %v", err)
|
||||
} else if err := os.WriteFile(cachePath, webpBytes, cacheFilePerm); err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "failed to write compressed cache file: %v", err)
|
||||
}
|
||||
|
||||
// Serve compressed WebP
|
||||
c.Header("Content-Type", "image/webp")
|
||||
c.Data(http.StatusOK, "image/webp", webpBytes)
|
||||
}
|
||||
|
||||
// serveOriginal 原始文件的流式响应逻辑
|
||||
func serveOriginal(c *gin.Context, upload *model.Upload) {
|
||||
if upload.StorageDriver == "local" || (upload.StorageDriver == "" && !storage.IsEnabled()) {
|
||||
c.File(upload.FilePath)
|
||||
return
|
||||
@@ -86,6 +168,27 @@ func ServeFileByID(c *gin.Context) {
|
||||
c.DataFromReader(http.StatusOK, obj.ContentLength, obj.ContentType, obj.Body, nil)
|
||||
}
|
||||
|
||||
// getOriginalFileBytes 获取原始文件所有字节
|
||||
func getOriginalFileBytes(ctx context.Context, upload *model.Upload) ([]byte, error) {
|
||||
if upload.StorageDriver == "local" || (upload.StorageDriver == "" && !storage.IsEnabled()) {
|
||||
return os.ReadFile(upload.FilePath)
|
||||
}
|
||||
|
||||
// Retrieve file from S3 (via CDN if configured)
|
||||
obj, err := storage.GetObjectViaCache(ctx, upload.FilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Cachefile
|
||||
if obj.CachePath != "" {
|
||||
return os.ReadFile(obj.CachePath)
|
||||
}
|
||||
|
||||
defer func() { _ = obj.Body.Close() }()
|
||||
return io.ReadAll(obj.Body)
|
||||
}
|
||||
|
||||
// checkFileAccessPermission 校验文件是否可以被当前请求访问
|
||||
func checkFileAccessPermission(c *gin.Context, uploadType string) error {
|
||||
var sc model.SystemConfig
|
||||
|
||||
@@ -5,10 +5,15 @@
|
||||
package upload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/common"
|
||||
@@ -195,3 +200,122 @@ func TestGetDistinctUploadTypes(t *testing.T) {
|
||||
t.Errorf("expected only custom_type_xyz in types list, got: %v", resp.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageCompression(t *testing.T) {
|
||||
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
|
||||
defer cleanup()
|
||||
|
||||
// Ensure uploads dir is cleaned up
|
||||
defer func() {
|
||||
_ = os.RemoveAll("uploads")
|
||||
}()
|
||||
|
||||
// Create test user
|
||||
user := model.User{
|
||||
ID: 555,
|
||||
Username: "compress_tester",
|
||||
IsActive: true,
|
||||
}
|
||||
dbConn.Create(&user)
|
||||
|
||||
// Create a 1x1 pixel PNG image
|
||||
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
||||
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255})
|
||||
var pngBuf bytes.Buffer
|
||||
if err := png.Encode(&pngBuf, img); err != nil {
|
||||
t.Fatalf("failed to encode test png: %v", err)
|
||||
}
|
||||
|
||||
_ = os.MkdirAll("uploads", 0755)
|
||||
filePath := "uploads/test_image.png"
|
||||
if err := os.WriteFile(filePath, pngBuf.Bytes(), 0644); err != nil {
|
||||
t.Fatalf("failed to write test png: %v", err)
|
||||
}
|
||||
|
||||
// Save upload record to DB
|
||||
uploadRecord := model.Upload{
|
||||
ID: 3001,
|
||||
UserID: user.ID,
|
||||
FileName: "test_image.png",
|
||||
FilePath: filePath,
|
||||
FileSize: int64(pngBuf.Len()),
|
||||
MimeType: "image/png",
|
||||
Extension: "png",
|
||||
StorageDriver: "local",
|
||||
Type: "avatar", // Whitelisted by default
|
||||
Status: model.UploadStatusUsed,
|
||||
}
|
||||
dbConn.Create(&uploadRecord)
|
||||
|
||||
// Setup Router
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/f/:id", ServeFileByID)
|
||||
|
||||
t.Run("serve original file without compress parameter", func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/f/3001", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
// Content-Type should be image/png (default local serving type)
|
||||
if w.Header().Get("Content-Type") != "image/png" {
|
||||
t.Errorf("expected Content-Type image/png, got %s", w.Header().Get("Content-Type"))
|
||||
}
|
||||
if len(w.Body.Bytes()) != pngBuf.Len() {
|
||||
t.Errorf("expected body size %d, got %d", pngBuf.Len(), len(w.Body.Bytes()))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("serve compressed WebP file with compress=true", func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/f/3001?compress=true&level=medium", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. Body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
// Content-Type should be image/webp
|
||||
if w.Header().Get("Content-Type") != "image/webp" {
|
||||
t.Errorf("expected Content-Type image/webp, got %s", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
// Check if local cache file was created
|
||||
cachePath := filepath.Join("uploads", "cache", "compressed_3001_medium.webp")
|
||||
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
|
||||
t.Errorf("expected cached webp file to be created at %s, but it doesn't exist", cachePath)
|
||||
}
|
||||
|
||||
// Subsequent request should hit the cache (modify the cached file to verify)
|
||||
testBytes := []byte("cached webp content")
|
||||
if err := os.WriteFile(cachePath, testBytes, 0644); err != nil {
|
||||
t.Fatalf("failed to write test bytes to cache: %v", err)
|
||||
}
|
||||
|
||||
w2 := httptest.NewRecorder()
|
||||
r.ServeHTTP(w2, req)
|
||||
if w2.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w2.Code)
|
||||
}
|
||||
if string(w2.Body.Bytes()) != "cached webp content" {
|
||||
t.Errorf("expected cached content, got %s", string(w2.Body.Bytes()))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("serve compressed with default quality high", func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/f/3001?compress=true", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
// Check if local cache file with "high" was created
|
||||
cachePath := filepath.Join("uploads", "cache", "compressed_3001_high.webp")
|
||||
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
|
||||
t.Errorf("expected cached webp file to be created at %s for default level", cachePath)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -174,6 +174,8 @@ func UploadFile(c *gin.Context) {
|
||||
// @Tags upload
|
||||
// @Produce octet-stream
|
||||
// @Param id path string true "文件 ID"
|
||||
// @Param compress query string false "是否启用压缩 (传任意非空值代表启用,非图片文件将被忽略)"
|
||||
// @Param level query string false "压缩质量等级 (low, medium, high),默认为 high"
|
||||
// @Security SessionCookie
|
||||
// @Success 200 {file} file "成功下载文件"
|
||||
// @Failure 400 {object} util.ResponseAny "参数错误"
|
||||
@@ -181,52 +183,36 @@ func UploadFile(c *gin.Context) {
|
||||
// @Failure 500 {object} util.ResponseAny "服务内部错误"
|
||||
// @Router /api/v1/upload/download/{id} [get]
|
||||
func DownloadFile(c *gin.Context) {
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("Content-Security-Policy", "sandbox")
|
||||
|
||||
ctx := c.Request.Context()
|
||||
idStr := c.Param("id")
|
||||
uploadID, err := strconv.ParseUint(idStr, 10, 64)
|
||||
upload, err := getUploadRecordByID(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, util.Err(ErrInvalidFileID))
|
||||
return
|
||||
}
|
||||
|
||||
var upload model.Upload
|
||||
if err := db.DB(ctx).Where("id = ? AND status IN (?, ?)", uploadID, model.UploadStatusPending, model.UploadStatusUsed).First(&upload).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if _, ok := err.(*strconv.NumError); ok {
|
||||
c.JSON(http.StatusOK, util.Err(ErrInvalidFileID))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, util.Err(ErrQueryUploadRecordFailed))
|
||||
return
|
||||
}
|
||||
|
||||
fileName := upload.FileName
|
||||
compressStr := c.Query("compress")
|
||||
isImage := strings.HasPrefix(strings.ToLower(upload.MimeType), "image/") || isImageExtension(strings.ToLower(upload.Extension))
|
||||
|
||||
if compressStr != "" && isImage {
|
||||
ext := filepath.Ext(fileName)
|
||||
if ext != "" {
|
||||
fileName = strings.TrimSuffix(fileName, ext) + ".webp"
|
||||
} else {
|
||||
fileName += ".webp"
|
||||
}
|
||||
}
|
||||
|
||||
// 设置下载 Attachment 响应头 (支持 UTF-8 中文文件名转义)
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename*=UTF-8''%s", url.PathEscape(upload.FileName)))
|
||||
c.Header("Content-Type", upload.MimeType)
|
||||
c.Header("Content-Length", strconv.FormatInt(upload.FileSize, 10))
|
||||
|
||||
// 根据存储驱动类型提供流式文件服务
|
||||
if upload.StorageDriver == "local" || (upload.StorageDriver == "" && !storage.IsEnabled()) {
|
||||
c.File(upload.FilePath)
|
||||
return
|
||||
}
|
||||
|
||||
// 从 S3/CDN 加载并返回
|
||||
obj, err := storage.GetObjectViaCache(ctx, upload.FilePath)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if obj.CachePath != "" {
|
||||
c.File(obj.CachePath)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() { _ = obj.Body.Close() }()
|
||||
_, _ = io.Copy(c.Writer, obj.Body)
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename*=UTF-8''%s", url.PathEscape(fileName)))
|
||||
ServeUpload(c, upload)
|
||||
}
|
||||
|
||||
// BatchDownloadFiles 批量打包 ZIP 下载接口
|
||||
|
||||
@@ -371,8 +371,8 @@ func TestDownloadFile(t *testing.T) {
|
||||
t.Errorf("expected Content-Disposition header %q, got %q", expectedDisp, contentDisp)
|
||||
}
|
||||
|
||||
if w.Header().Get("Content-Type") != "text/plain" {
|
||||
t.Errorf("expected Content-Type text/plain, got %s", w.Header().Get("Content-Type"))
|
||||
if !strings.HasPrefix(w.Header().Get("Content-Type"), "text/plain") {
|
||||
t.Errorf("expected Content-Type starting with text/plain, got %s", w.Header().Get("Content-Type"))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -5,9 +5,18 @@
|
||||
package upload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif" // Register GIF decoder for image.Decode
|
||||
_ "image/jpeg" // Register JPEG decoder for image.Decode
|
||||
_ "image/png" // Register PNG decoder for image.Decode
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/deepteams/webp"
|
||||
_ "golang.org/x/image/webp" // Register WebP decoder for image.Decode
|
||||
)
|
||||
|
||||
const maxS3KeyLength = 1024
|
||||
@@ -32,3 +41,38 @@ func ValidateS3Key(key string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompressImageToWebP decodes an image from srcReader and encodes it into WebP format
|
||||
// using the specified quality level (low -> 60, medium -> 75, high -> 85).
|
||||
func CompressImageToWebP(srcReader io.Reader, qualityLevel string) ([]byte, error) {
|
||||
// Decode the image
|
||||
img, format, err := image.Decode(srcReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image (format: %s): %w", format, err)
|
||||
}
|
||||
|
||||
// Determine quality
|
||||
var quality float32
|
||||
switch strings.ToLower(qualityLevel) {
|
||||
case "low":
|
||||
quality = 60
|
||||
case "medium":
|
||||
quality = 75
|
||||
case "high", "":
|
||||
quality = 85
|
||||
default:
|
||||
quality = 85
|
||||
}
|
||||
|
||||
// Encode to WebP
|
||||
var buf bytes.Buffer
|
||||
err = webp.Encode(&buf, img, &webp.EncoderOptions{
|
||||
Quality: quality,
|
||||
Method: 4, // Default method
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to encode WebP: %w", err)
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user