增加批量上传

This commit is contained in:
zhx 2026-06-02 18:23:45 +08:00
parent 1b7bc6c917
commit 10a3df3a5a
3 changed files with 34 additions and 0 deletions

View File

@ -63,6 +63,10 @@ func (h *Handler) UploadImage(c *gin.Context) {
h.uploadObject(c, imageUploadPolicy)
}
func (h *Handler) UploadFilesBatch(c *gin.Context) {
h.uploadObjects(c, fileUploadPolicy)
}
func (h *Handler) UploadImagesBatch(c *gin.Context) {
h.uploadObjects(c, imageUploadPolicy)
}

View File

@ -187,6 +187,28 @@ func TestUploadImagesBatchUploadsSeriallyAndLimitsCount(t *testing.T) {
}
}
func TestUploadFilesBatchAllowsAnyMaterialExtension(t *testing.T) {
uploader := &fakeUploader{}
handler := New(uploader, "admin", fakeAudit{})
router := ginRouter(handler)
body, contentType := multipartBatchUploadBody(t, []string{"gift.mp4", "gift.json"})
req := httptest.NewRequest(http.MethodPost, "/upload-files-batch", body)
req.Header.Set("Content-Type", contentType)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
if len(uploader.keys) != 2 {
t.Fatalf("expected 2 uploads, got %d", len(uploader.keys))
}
if !strings.HasPrefix(uploader.keys[0], "admin/files/7/") || !strings.HasSuffix(uploader.keys[0], ".mp4") {
t.Fatalf("unexpected first key: %s", uploader.keys[0])
}
}
func ginRouter(handler *Handler) http.Handler {
router := gin.New()
router.Use(func(c *gin.Context) {
@ -195,6 +217,7 @@ func ginRouter(handler *Handler) http.Handler {
c.Next()
})
router.POST("/upload-image", handler.UploadImage)
router.POST("/upload-files-batch", handler.UploadFilesBatch)
router.POST("/upload-images-batch", handler.UploadImagesBatch)
return router
}
@ -229,6 +252,12 @@ func multipartBatchUploadBody(t *testing.T, filenames []string) (*bytes.Buffer,
if strings.HasSuffix(filename, ".pag") {
contentType = "application/octet-stream"
payload = []byte("PAG animated asset")
} else if strings.HasSuffix(filename, ".mp4") {
contentType = "video/mp4"
payload = []byte("mp4 animated asset")
} else if strings.HasSuffix(filename, ".json") {
contentType = "application/json"
payload = []byte(`{"asset":true}`)
}
part, err := writer.CreatePart(map[string][]string{
"Content-Disposition": {`form-data; name="files"; filename="` + filename + `"`},

View File

@ -13,5 +13,6 @@ func RegisterRoutes(protected *gin.RouterGroup, h *Handler) {
protected.POST("/admin/files/upload", middleware.RequirePermission("upload:create"), h.UploadFile)
protected.POST("/admin/files/image/upload", middleware.RequirePermission("upload:create"), h.UploadImage)
protected.POST("/admin/files/batch-upload", middleware.RequirePermission("upload:create"), h.UploadFilesBatch)
protected.POST("/admin/files/image/batch-upload", middleware.RequirePermission("upload:create"), h.UploadImagesBatch)
}