From 96ca68b35ce25e4e2d4160cd6c23b1081c08a503 Mon Sep 17 00:00:00 2001 From: Elijah Date: Sat, 23 May 2026 17:27:09 -0700 Subject: [PATCH] feat(api): recursively calculate directory sizes for public share folders and display on UI --- backend/handlers/sharing.go | 24 ++++++++++++++++++++++-- frontend/src/app/share/[id]/page.tsx | 6 +++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/handlers/sharing.go b/backend/handlers/sharing.go index 8df9572..e492b45 100644 --- a/backend/handlers/sharing.go +++ b/backend/handlers/sharing.go @@ -229,9 +229,25 @@ func (h *ShareHandler) GetPublicShare(c *fiber.Ctx) error { mimeType = "application/octet-stream" } + getDirSize := func(p string) int64 { + var s int64 + filepath.Walk(p, func(_ string, i os.FileInfo, err error) error { + if err == nil && !i.IsDir() { + s += i.Size() + } + return nil + }) + return s + } + + size := info.Size() + if info.IsDir() { + size = getDirSize(fullPath) + } + response := fiber.Map{ "name": info.Name(), // Use info.Name() instead of base of original path - "size": info.Size(), + "size": size, "is_dir": info.IsDir(), "mime_type": mimeType, } @@ -249,9 +265,13 @@ func (h *ShareHandler) GetPublicShare(c *fiber.Ctx) error { if eMime == "" { eMime = "application/octet-stream" } + entrySize := entryInfo.Size() + if entry.IsDir() { + entrySize = getDirSize(filepath.Join(fullPath, entry.Name())) + } files = append(files, fiber.Map{ "name": entry.Name(), - "size": entryInfo.Size(), + "size": entrySize, "is_dir": entry.IsDir(), "mime_type": eMime, }) diff --git a/frontend/src/app/share/[id]/page.tsx b/frontend/src/app/share/[id]/page.tsx index e327536..02a0263 100644 --- a/frontend/src/app/share/[id]/page.tsx +++ b/frontend/src/app/share/[id]/page.tsx @@ -247,7 +247,7 @@ export default function PublicSharePage({ params }: { params: Promise<{ id: stri className="flex items-center gap-2 px-5 py-2.5 rounded-xl font-medium transition-all hover:bg-white/10 bg-white/5 border border-white/10 text-white shadow-lg" > - Download ZIP + Download ZIP ({formatSize(fileInfo?.size || 0)}) )} @@ -287,7 +287,7 @@ export default function PublicSharePage({ params }: { params: Promise<{ id: stri style={{ background: 'linear-gradient(135deg, #6366f1, #8b5cf6)' }} > - Download File + Download File ({formatSize(fileInfo?.size || 0)}) ) : ( @@ -326,7 +326,7 @@ export default function PublicSharePage({ params }: { params: Promise<{ id: stri - {f.is_dir ? '--' : formatSize(f.size)} + {formatSize(f.size)}