60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"sort"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/samber/lo"
|
|
"github.com/spf13/viper"
|
|
"go.fifitido.net/ytdl-web/ytdl"
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
func Serve() error {
|
|
engine := ViewsEngine()
|
|
app := fiber.New(fiber.Config{Views: engine})
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
return c.Render("views/index", fiber.Map{
|
|
"BasePath": viper.GetString("base_path"),
|
|
}, "views/layouts/main")
|
|
})
|
|
|
|
app.Get("/download", func(c *fiber.Ctx) error {
|
|
urlBytes, err := url.QueryUnescape(c.Query("url"))
|
|
if err != nil {
|
|
slog.Error("Failed to decode url param", slog.String("error", err.Error()))
|
|
return fiber.ErrBadRequest
|
|
}
|
|
url := string(urlBytes)
|
|
|
|
meta, err := ytdl.GetMetadata(url)
|
|
if err != nil {
|
|
slog.Error("Failed to get metadata", slog.String("error", err.Error()))
|
|
return fiber.ErrInternalServerError
|
|
}
|
|
|
|
formats := lo.Filter(meta.Formats, func(item ytdl.Format, _ int) bool {
|
|
return item.ACodec != "none" && item.VCodec != "none"
|
|
})
|
|
|
|
sort.Slice(formats, func(i, j int) bool {
|
|
return formats[i].Width > formats[j].Width
|
|
})
|
|
|
|
return c.Render("views/download", fiber.Map{
|
|
"BasePath": viper.GetString("base_path"), "Url": url, "Meta": meta, "Formats": formats,
|
|
}, "views/layouts/main")
|
|
})
|
|
|
|
app.Get("/download/stream", func(c *fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
listenAddr := fmt.Sprintf("%s:%d", viper.GetString("listen"), viper.GetInt("port"))
|
|
|
|
return app.Listen(listenAddr)
|
|
}
|