feat: batch delete
This commit is contained in:
-133
@@ -1,9 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -307,134 +305,3 @@ func deleteHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"deleted": name})
|
||||
audit(c, "delete", gin.H{"file": name, "size": info.Size(), "result": "ok"})
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 批量下载: POST /download-zip (body: {"files":[...]} 或 query ?files=a&files=b)
|
||||
// 流式 archive/zip 输出, 不写临时文件. 缺失/路径非法/超量统一 4xx 兜底.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
batchMaxFiles = 100
|
||||
batchNameLimit = 200
|
||||
)
|
||||
|
||||
type batchRequest struct {
|
||||
Files []string `json:"files"`
|
||||
}
|
||||
|
||||
func batchDownloadHandler(c *gin.Context) {
|
||||
// 1. 收集 filenames: body JSON 优先, fallback 到 query ?files=
|
||||
var req batchRequest
|
||||
var names []string
|
||||
if c.Request.ContentLength > 0 && strings.HasPrefix(c.GetHeader("Content-Type"), "application/json") {
|
||||
if err := c.ShouldBindJSON(&req); err == nil && len(req.Files) > 0 {
|
||||
names = req.Files
|
||||
}
|
||||
}
|
||||
if names == nil {
|
||||
names = c.QueryArray("files")
|
||||
}
|
||||
if len(names) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no files specified"})
|
||||
audit(c, "batch_download", gin.H{"result": "no_files"})
|
||||
return
|
||||
}
|
||||
if len(names) > batchMaxFiles {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": fmt.Sprintf("too many files (max %d)", batchMaxFiles),
|
||||
})
|
||||
audit(c, "batch_download", gin.H{
|
||||
"count": len(names), "result": "too_many",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 校验每个文件名, 检查存在性
|
||||
type entry struct {
|
||||
path string
|
||||
size int64
|
||||
}
|
||||
var packed []entry
|
||||
miss := []string{} // 非 nil, JSON 输出 [] 而非 null
|
||||
for _, raw := range names {
|
||||
if len(raw) > batchNameLimit {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "filename too long (max 200 bytes)"})
|
||||
audit(c, "batch_download", gin.H{
|
||||
"count": len(names), "result": "too_long",
|
||||
})
|
||||
return
|
||||
}
|
||||
name, ok := validateFilename(raw)
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid filename: " + raw})
|
||||
audit(c, "batch_download", gin.H{
|
||||
"file": raw, "result": "invalid_filename",
|
||||
})
|
||||
return
|
||||
}
|
||||
p := filepath.Join(*uploadDir, name)
|
||||
info, err := os.Stat(p)
|
||||
if os.IsNotExist(err) || (err == nil && info.IsDir()) {
|
||||
miss = append(miss, name)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("[batch_download] stat failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
|
||||
audit(c, "batch_download", gin.H{
|
||||
"file": name, "result": "stat_failed",
|
||||
})
|
||||
return
|
||||
}
|
||||
packed = append(packed, entry{path: p, size: info.Size()})
|
||||
}
|
||||
|
||||
if len(packed) == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "no files available",
|
||||
"miss": miss,
|
||||
})
|
||||
audit(c, "batch_download", gin.H{
|
||||
"count": len(names), "packed": 0, "miss": miss,
|
||||
"result": "all_miss",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 流式 zip. WriteHeader 必须先于 body 写出, 之后 io.Copy 直写 c.Writer.
|
||||
zipName := "files-" + time.Now().Format("20060102-150405") + ".zip"
|
||||
c.Header("Content-Type", "application/zip")
|
||||
c.Header("Content-Disposition", `attachment; filename="`+zipName+`"`)
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Writer.WriteHeader(http.StatusOK)
|
||||
|
||||
zw := zip.NewWriter(c.Writer)
|
||||
now := time.Now()
|
||||
for _, e := range packed {
|
||||
fw, err := zw.Create(filepath.Base(e.path))
|
||||
if err != nil {
|
||||
log.Printf("[batch_download] zip create failed: %v", err)
|
||||
continue
|
||||
}
|
||||
f, err := os.Open(e.path)
|
||||
if err != nil {
|
||||
log.Printf("[batch_download] open failed: %v", err)
|
||||
continue
|
||||
}
|
||||
if _, err := io.Copy(fw, f); err != nil {
|
||||
log.Printf("[batch_download] copy failed: %v", err)
|
||||
}
|
||||
f.Close()
|
||||
_ = os.Chtimes(e.path, now, now) // 下载即续期, 与单文件一致
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
log.Printf("[batch_download] zip close failed: %v", err)
|
||||
}
|
||||
|
||||
audit(c, "batch_download", gin.H{
|
||||
"count": len(names),
|
||||
"packed": len(packed),
|
||||
"miss": miss,
|
||||
"result": "ok",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user