Add handler for proxy endpoint
This commit is contained in:
parent
eeedf5e10a
commit
de8a4d9e80
42
web/serve.go
42
web/serve.go
|
@ -29,6 +29,9 @@ func Serve() error {
|
|||
return fiber.ErrBadRequest
|
||||
}
|
||||
url := string(urlBytes)
|
||||
if len(url) < 1 {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
meta, err := ytdl.GetMetadata(url)
|
||||
if err != nil {
|
||||
|
@ -49,8 +52,43 @@ func Serve() error {
|
|||
}, "views/layouts/main")
|
||||
})
|
||||
|
||||
app.Get("/download/stream", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
app.Get("/download/proxy", 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)
|
||||
if len(url) < 1 {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
formatId := c.Query("format")
|
||||
if len(formatId) < 1 {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
meta, err := ytdl.GetMetadata(url)
|
||||
if err != nil {
|
||||
slog.Error("Failed to get metadata", slog.String("error", err.Error()))
|
||||
return fiber.ErrInternalServerError
|
||||
}
|
||||
|
||||
format, ok := lo.Find(meta.Formats, func(format ytdl.Format) bool {
|
||||
return format.FormatID == formatId
|
||||
})
|
||||
if !ok {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-%s.%s\"", meta.ID, format.Resolution, format.Ext))
|
||||
if format.Filesize != nil {
|
||||
c.Set("Content-Length", fmt.Sprint(*format.Filesize))
|
||||
} else if format.FilesizeApprox != nil {
|
||||
c.Set("Content-Length", fmt.Sprint(*format.FilesizeApprox))
|
||||
}
|
||||
|
||||
return ytdl.Stream(c.Response().BodyWriter(), url, format)
|
||||
})
|
||||
|
||||
listenAddr := fmt.Sprintf("%s:%d", viper.GetString("listen"), viper.GetInt("port"))
|
||||
|
|
|
@ -252,7 +252,7 @@ type Format struct {
|
|||
Filesize *int `json:"filesize"`
|
||||
|
||||
// An estimate for the number of bytes
|
||||
FilesizeApprox int `json:"filesize_approx"`
|
||||
FilesizeApprox *int `json:"filesize_approx"`
|
||||
|
||||
// The protocol that will be used for the actual
|
||||
// download, lower-case. One of "http", "https" or
|
||||
|
|
|
@ -17,7 +17,7 @@ func GetMetadata(url string) (Metdata, error) {
|
|||
cmd.Stdout = &out
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return Metdata{}, nil
|
||||
return Metdata{}, err
|
||||
}
|
||||
|
||||
var meta Metdata
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package ytdl
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Stream(wr io.Writer, url string, format Format) error {
|
||||
cmd := exec.Command(
|
||||
"yt-dlp",
|
||||
"-o", "-",
|
||||
"-f", format.FormatID,
|
||||
"--merge-output-format", "mkv",
|
||||
url,
|
||||
)
|
||||
|
||||
cmd.Stdout = wr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue