fix(upload): clean multipart temporary files
parent
733fdd69a6
commit
d1691cbc26
|
|
@ -0,0 +1,19 @@
|
|||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// MultipartFormCleanup 在请求结束后删除 multipart 解析写入系统临时目录的文件。
|
||||
// 入参:Gin 请求上下文 c;出参:无。未解析 multipart 表单或仅使用内存的请求不会执行删除。
|
||||
// 该中间件应在全部路由之前注册,以覆盖成功、失败和 panic 恢复后的上传请求。
|
||||
func MultipartFormCleanup() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
// Go 在 ParseMultipartForm 落盘后会设置 MultipartForm。
|
||||
// RemoveAll 仅删除其自动创建的 multipart 临时文件,不影响已持久化的业务文件。
|
||||
if c.Request != nil && c.Request.MultipartForm != nil {
|
||||
_ = c.Request.MultipartForm.RemoveAll()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// TestMultipartFormCleanupRemovesDiskTemporaryFiles 验证超过内存阈值的上传在响应后不遗留 multipart 临时文件。
|
||||
// 入参:2 MiB multipart 文件与 1 字节的 Gin 内存阈值,确保标准库将上传内容写入临时目录。
|
||||
// 出参:处理时发现 multipart 临时文件;响应完成后专用临时目录为空。
|
||||
func TestMultipartFormCleanupRemovesDiskTemporaryFiles(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
tempDir := t.TempDir()
|
||||
t.Setenv("TMPDIR", tempDir)
|
||||
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
part, err := writer.CreateFormFile("file", "large.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("create multipart file = %v", err)
|
||||
}
|
||||
if _, err = part.Write(bytes.Repeat([]byte("a"), 2*1024*1024)); err != nil {
|
||||
t.Fatalf("write multipart file = %v", err)
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
t.Fatalf("close multipart writer = %v", err)
|
||||
}
|
||||
|
||||
engine := gin.New()
|
||||
engine.MaxMultipartMemory = 1
|
||||
engine.Use(MultipartFormCleanup())
|
||||
temporaryFileFound := false
|
||||
engine.POST("/upload", func(c *gin.Context) {
|
||||
if _, formErr := c.FormFile("file"); formErr != nil {
|
||||
t.Errorf("read uploaded file = %v", formErr)
|
||||
return
|
||||
}
|
||||
entries, readErr := os.ReadDir(tempDir)
|
||||
if readErr != nil {
|
||||
t.Errorf("read temporary directory = %v", readErr)
|
||||
return
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasPrefix(entry.Name(), "multipart-") {
|
||||
temporaryFileFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
|
||||
request := httptest.NewRequest(http.MethodPost, "/upload", &body)
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
response := httptest.NewRecorder()
|
||||
engine.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNoContent {
|
||||
t.Fatalf("response status = %d, want %d", response.Code, http.StatusNoContent)
|
||||
}
|
||||
if !temporaryFileFound {
|
||||
t.Fatal("multipart temporary file was not created during request")
|
||||
}
|
||||
remaining, err := filepath.Glob(filepath.Join(tempDir, "multipart-*"))
|
||||
if err != nil {
|
||||
t.Fatalf("find remaining multipart files = %v", err)
|
||||
}
|
||||
if len(remaining) != 0 {
|
||||
t.Fatalf("remaining multipart temporary files = %v, want none", remaining)
|
||||
}
|
||||
}
|
||||
|
|
@ -98,6 +98,8 @@ type RouterParams struct {
|
|||
func NewRouter(params RouterParams) *gin.Engine {
|
||||
r := gin.New()
|
||||
r.ContextWithFallback = true
|
||||
// 清理 FormFile/MultipartForm 解析产生的 multipart 临时文件,避免容器 /tmp 持续增长。
|
||||
r.Use(middleware.MultipartFormCleanup())
|
||||
|
||||
// Trusted proxies: gin defaults to trusting ALL proxies, which makes
|
||||
// c.ClientIP() honor a client-supplied X-Forwarded-For. Public, unauthed
|
||||
|
|
|
|||
Loading…
Reference in New Issue