feat: add zip download for folders in file manager
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/utils"
|
||||
|
||||
@@ -492,3 +493,50 @@ func (fc *FileController) DownloadFile(c *gin.Context) {
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
c.File(fullPath)
|
||||
}
|
||||
|
||||
func (fc *FileController) DownloadZip(c *gin.Context) {
|
||||
paths := c.QueryArray("path")
|
||||
if len(paths) == 0 || c.ContentType() == "application/json" {
|
||||
var req struct {
|
||||
Paths []string `json:"paths"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err == nil && len(paths) == 0 {
|
||||
paths = req.Paths
|
||||
}
|
||||
}
|
||||
|
||||
if len(paths) == 0 {
|
||||
utils.BadRequest(c, "path参数必填")
|
||||
return
|
||||
}
|
||||
|
||||
validatedAbsPaths := make([]string, 0, len(paths))
|
||||
for _, path := range paths {
|
||||
fullPath, safe := fc.checkPath(path, false)
|
||||
if !safe {
|
||||
utils.Forbidden(c, "访问被拒绝")
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(fullPath); err != nil {
|
||||
utils.NotFound(c, "文件不存在")
|
||||
return
|
||||
}
|
||||
|
||||
validatedAbsPaths = append(validatedAbsPaths, fullPath)
|
||||
}
|
||||
|
||||
fileName := "baihu-export-" + time.Now().Format("20060102-150405") + ".zip"
|
||||
if len(validatedAbsPaths) == 1 {
|
||||
fileName = filepath.Base(validatedAbsPaths[0]) + ".zip"
|
||||
}
|
||||
|
||||
c.Header("Content-Description", "File Transfer")
|
||||
c.Header("Content-Transfer-Encoding", "binary")
|
||||
c.Header("Content-Disposition", "attachment; filename="+fileName)
|
||||
c.Header("Content-Type", "application/zip")
|
||||
|
||||
if err := utils.CreateZip(c.Writer, validatedAbsPaths); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user