feat: report dir size correctly

This commit is contained in:
Louis Dalibard 2024-04-27 17:38:55 +02:00
parent 18b264f0d2
commit c9f7233da6
2 changed files with 31 additions and 1 deletions

BIN
leech

Binary file not shown.

View File

@ -12,6 +12,29 @@ import (
"github.com/gofiber/fiber/v2"
)
func RecursivelyGetSize(completePath string) (int64, error) {
files, err := os.ReadDir(completePath)
if err != nil {
return -1, err
}
var sum int64
for _, f := range files {
dirEntryInfo, err := f.Info()
if err != nil {
return -1, err
}
dirEntrySize := dirEntryInfo.Size()
if f.IsDir() {
dirEntrySize, err = RecursivelyGetSize(path.Join(completePath, f.Name()))
if err != nil {
return -1, err
}
}
sum += dirEntrySize
}
return sum, nil
}
func HandleList(c *fiber.Ctx) error {
encodedReq := c.Params("req")
req, err := url.QueryUnescape(encodedReq)
@ -54,9 +77,16 @@ func HandleList(c *fiber.Ctx) error {
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal server error!")
}
dirEntrySize := dirEntryInfo.Size()
if f.IsDir() {
dirEntrySize, err = RecursivelyGetSize(path.Join(completePath, f.Name()))
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
}
entries = append(entries, html.Entry{
Name: f.Name(),
Size: dirEntryInfo.Size(),
Size: dirEntrySize,
IsDir: f.IsDir(),
})
}