feat: better url handling

This commit is contained in:
Louis Dalibard 2024-04-28 00:45:42 +02:00
parent d38556bc38
commit bce825031f
4 changed files with 12 additions and 9 deletions

View File

@ -83,7 +83,7 @@ func FileListPage(req string, entries []Entry) string {
<meta property="og:type" content="website" /> <meta property="og:type" content="website" />
<meta property="og:url" content="https://leech.ontake.dev" /> <meta property="og:url" content="https://leech.ontake.dev" />
<meta property="og:title" content="%s | leech.ontake.dev" /> <meta property="og:title" content="%s | leech.ontake.dev" />
<meta name="viewport" content="width=device-width, initial-scale=2" /> <meta name="viewport" content="width=device-width, initial-scale=0.1" />
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'> <meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>
</head> </head>
<body> <body>
@ -110,16 +110,16 @@ func FileListPage(req string, entries []Entry) string {
if dirEntry.IsDir { if dirEntry.IsDir {
icon = "/assets/images/diricon.png" icon = "/assets/images/diricon.png"
} }
link := "/" + req + "." + dirEntry.Name link := "/" + req + "/" + dirEntry.Name
if dirEntry.Name == ".." { if dirEntry.Name == ".." {
splitReq := strings.Split(req, ".") splitReq := strings.Split(req, "/")
link = "/" + strings.Join(splitReq[:len(splitReq)-1], ".") link = "/" + strings.Join(splitReq[:len(splitReq)-1], "/")
} }
if req == "" { if req == "" {
link = "/" + req + dirEntry.Name link = "/" + req + dirEntry.Name
} }
if !dirEntry.IsDir { if !dirEntry.IsDir {
link = "/serve/" + strings.ReplaceAll(req, ".", "/") + "/" + dirEntry.Name link = "/serve/" + req + "/" + dirEntry.Name
} }
body += fmt.Sprintf(`<a href="%s"><div class="entry"><img src="%s"></img><div class="entry-name">%s</div><div class="entry-size">%s</div></div></a>`, link, icon, dirEntry.Name, formattedSize) body += fmt.Sprintf(`<a href="%s"><div class="entry"><img src="%s"></img><div class="entry-name">%s</div><div class="entry-size">%s</div></div></a>`, link, icon, dirEntry.Name, formattedSize)
} }

BIN
leech

Binary file not shown.

View File

@ -21,8 +21,6 @@ func main() {
app.Use(logger.New(logger.Config{ app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
})) }))
app.Get("/:req", route.HandleList)
app.Get("/", route.HandleList)
app.Use("/assets", filesystem.New(filesystem.Config{ app.Use("/assets", filesystem.New(filesystem.Config{
Root: http.FS(assetsEmbed), Root: http.FS(assetsEmbed),
@ -34,5 +32,7 @@ func main() {
app.Static("/serve/"+url.QueryEscape(dirName), dirToServe) app.Static("/serve/"+url.QueryEscape(dirName), dirToServe)
} }
app.Use("/", route.HandleList)
app.Listen(config.Config.Host) app.Listen(config.Config.Host)
} }

View File

@ -1,6 +1,7 @@
package route package route
import ( import (
"fmt"
"leech/config" "leech/config"
"leech/html" "leech/html"
"net/url" "net/url"
@ -36,7 +37,8 @@ func RecursivelyGetSize(completePath string) (int64, error) {
} }
func HandleList(c *fiber.Ctx) error { func HandleList(c *fiber.Ctx) error {
encodedReq := c.Params("req") encodedReq := c.Path()[1:]
fmt.Println(encodedReq)
req, err := url.QueryUnescape(encodedReq) req, err := url.QueryUnescape(encodedReq)
if err != nil { if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
@ -66,8 +68,9 @@ func HandleList(c *fiber.Ctx) error {
return c.SendString(html.FileListPage(req, entries)) return c.SendString(html.FileListPage(req, entries))
} else { } else {
pathSlice := strings.Split(req, ".") pathSlice := strings.Split(req, "/")
pathBase, ok := config.Config.ServeDirs[pathSlice[0]] pathBase, ok := config.Config.ServeDirs[pathSlice[0]]
fmt.Println(pathSlice[0])
if ok { if ok {
pathSlice[0] = pathBase pathSlice[0] = pathBase
completePath := path.Join(pathSlice...) completePath := path.Join(pathSlice...)