package route import ( "leech/config" "leech/html" "net/url" "os" "path" "sort" "strings" "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) if err != nil { return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) } c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) if req == "" { keys := make([]string, 0, len(config.Config.ServeDirs)) for k := range config.Config.ServeDirs { keys = append(keys, k) } sort.Slice(keys, func(i, j int) bool { return i < j }) entries := make([]html.Entry, 0, len(config.Config.ServeDirs)) for _, key := range keys { dirEntrySize, err := RecursivelyGetSize(config.Config.ServeDirs[key]) if err != nil { return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) } entries = append(entries, html.Entry{ Name: key, Size: dirEntrySize, IsDir: true, }) } return c.SendString(html.FileListPage(req, entries)) } else { pathSlice := strings.Split(req, ".") pathBase, ok := config.Config.ServeDirs[pathSlice[0]] if ok { pathSlice[0] = pathBase completePath := path.Join(pathSlice...) files, err := os.ReadDir(completePath) if err != nil { return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) } entries := []html.Entry{} for _, f := range files { dirEntryInfo, err := f.Info() 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: dirEntrySize, IsDir: f.IsDir(), }) } return c.SendString(html.FileListPage(req, entries)) } else { return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!") } } }