mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 10:17:17 +08:00
feat: add on-demand folder size calculation in file manager (#1201)
* Initial plan * feat: add folder size calculation feature with "Calculate" link - Backend: Add Size API to calculate directory size - Backend: Modify formatDir to return empty size for directories - Frontend: Show "Calculate" link for directories instead of size - Frontend: Add loading spinner during calculation - Frontend: Cache calculated sizes until path changes - Add translations for Calculate and Failed to calculate size Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * fix: 优化 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
This commit is contained in:
@@ -403,6 +403,7 @@ func (route *Http) Register(r *chi.Mux) {
|
||||
r.Get("/download", route.file.Download)
|
||||
r.Post("/remote_download", route.file.RemoteDownload)
|
||||
r.Get("/info", route.file.Info)
|
||||
r.Get("/size", route.file.Size)
|
||||
r.Post("/permission", route.file.Permission)
|
||||
r.Post("/compress", route.file.Compress)
|
||||
r.Post("/un_compress", route.file.UnCompress)
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/leonelquinteros/gotext"
|
||||
"github.com/libtnb/chix"
|
||||
"github.com/libtnb/utils/file"
|
||||
"github.com/spf13/cast"
|
||||
|
||||
"github.com/acepanel/panel/internal/app"
|
||||
"github.com/acepanel/panel/internal/biz"
|
||||
@@ -327,6 +328,37 @@ func (s *FileService) Info(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// Size 计算大小
|
||||
func (s *FileService) Size(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := Bind[request.FilePath](r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := stdos.Stat(req.Path)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
if !info.IsDir() {
|
||||
// 如果不是目录,直接返回文件大小
|
||||
Success(w, chix.M{
|
||||
"size": tools.FormatBytes(float64(info.Size())),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 计算目录大小
|
||||
output, err := shell.Execf("du -sb '%s' | awk '{print $1}'", req.Path)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, tools.FormatBytes(cast.ToFloat64(output)))
|
||||
}
|
||||
|
||||
func (s *FileService) Permission(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := Bind[request.FilePermission](r)
|
||||
if err != nil {
|
||||
@@ -458,10 +490,15 @@ func (s *FileService) formatDir(base string, entries []stdos.DirEntry) []any {
|
||||
}
|
||||
|
||||
stat := info.Sys().(*syscall.Stat_t)
|
||||
// 对于目录,size 返回空字符串,需要用户手动计算
|
||||
size := ""
|
||||
if !info.IsDir() {
|
||||
size = tools.FormatBytes(float64(info.Size()))
|
||||
}
|
||||
paths = append(paths, map[string]any{
|
||||
"name": info.Name(),
|
||||
"full": filepath.Join(base, info.Name()),
|
||||
"size": tools.FormatBytes(float64(info.Size())),
|
||||
"size": size,
|
||||
"mode_str": info.Mode().String(),
|
||||
"mode": fmt.Sprintf("%04o", info.Mode().Perm()),
|
||||
"owner": os.GetUser(stat.Uid),
|
||||
|
||||
Reference in New Issue
Block a user