leech/route/route.go

135 lines
3.6 KiB
Go
Raw Normal View History

2024-04-27 12:16:43 +02:00
package route
import (
2024-06-10 21:36:26 +02:00
"errors"
2024-04-27 12:16:43 +02:00
"leech/config"
"leech/html"
2024-06-10 21:36:26 +02:00
"leech/thumbnail"
2024-04-27 13:58:01 +02:00
"net/url"
2024-04-27 12:16:43 +02:00
"os"
"path"
"sort"
"strings"
"github.com/gofiber/fiber/v2"
)
2024-04-27 17:38:55 +02:00
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
}
2024-04-27 12:16:43 +02:00
func HandleList(c *fiber.Ctx) error {
2024-04-28 00:45:42 +02:00
encodedReq := c.Path()[1:]
2024-04-27 13:58:01 +02:00
req, err := url.QueryUnescape(encodedReq)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
2024-04-27 12:16:43 +02:00
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
if req == "" {
2024-04-27 12:40:07 +02:00
keys := make([]string, 0, len(config.Config.ServeDirs))
for k := range config.Config.ServeDirs {
2024-04-27 12:16:43 +02:00
keys = append(keys, k)
}
2024-04-28 12:16:49 +02:00
sort.Strings(keys)
2024-04-27 12:16:43 +02:00
2024-04-27 12:40:07 +02:00
entries := make([]html.Entry, 0, len(config.Config.ServeDirs))
2024-04-27 12:16:43 +02:00
for _, key := range keys {
2024-04-27 22:56:05 +02:00
dirEntrySize, err := RecursivelyGetSize(config.Config.ServeDirs[key])
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
2024-04-27 12:16:43 +02:00
entries = append(entries, html.Entry{
Name: key,
2024-04-27 22:56:05 +02:00
Size: dirEntrySize,
2024-04-27 12:16:43 +02:00
IsDir: true,
})
}
return c.SendString(html.FileListPage(req, entries))
} else {
2024-04-28 00:45:42 +02:00
pathSlice := strings.Split(req, "/")
2024-04-27 12:40:07 +02:00
pathBase, ok := config.Config.ServeDirs[pathSlice[0]]
2024-04-27 12:16:43 +02:00
if ok {
pathSlice[0] = pathBase
completePath := path.Join(pathSlice...)
files, err := os.ReadDir(completePath)
if err != nil {
2024-04-27 13:50:34 +02:00
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
2024-04-27 12:16:43 +02:00
}
entries := []html.Entry{}
for _, f := range files {
dirEntryInfo, err := f.Info()
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal server error!")
}
2024-04-27 17:38:55 +02:00
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())
}
}
2024-04-27 12:16:43 +02:00
entries = append(entries, html.Entry{
Name: f.Name(),
2024-04-27 17:38:55 +02:00
Size: dirEntrySize,
2024-04-27 12:16:43 +02:00
IsDir: f.IsDir(),
})
}
return c.SendString(html.FileListPage(req, entries))
} else {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
}
}
}
2024-06-10 21:36:26 +02:00
func HandleThumb(c *fiber.Ctx) error {
encodedReq := c.Path()[1:]
req, err := url.QueryUnescape(encodedReq)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
pathSlice := strings.Split(req, "/")
pathSlice = pathSlice[1:len(pathSlice)]
pathBase, ok := config.Config.ServeDirs[pathSlice[0]]
if ok {
pathSlice[0] = pathBase
completePath := path.Join(pathSlice...)
if file_info, err := os.Stat(completePath); err == nil {
if file_info.IsDir() {
return c.Status(fiber.StatusUnauthorized).SendString("Sorry that's a directory!")
}
thumbnail.GetThumbnail(c, completePath)
} else if errors.Is(err, os.ErrNotExist) {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
} else {
// Schrodinger: file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
}
} else {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
}
return nil
}