Add basic extraction methods

This commit is contained in:
Zoe Roux 2024-01-17 18:17:06 +01:00
parent 43ba1bb449
commit 30cb171612
3 changed files with 92 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"net/http"
"strconv"
@ -208,6 +209,46 @@ func GetInfo(c echo.Context) error {
return c.JSON(http.StatusOK, ret)
}
// Get attachments
//
// Get a specific attachment
//
// Path: /:sha/attachment/:name
func GetAttachment(c echo.Context) error {
sha := c.Param("sha")
name := c.Param("name")
if err := SanitizePath(sha); err != nil {
return err
}
if err := SanitizePath(name); err != nil {
return err
}
path := fmt.Sprintf("%s/%s/att/%s", src.GetMetadataPath(), sha, name)
return c.File(path)
}
// Get subtitle
//
// Get a specific subtitle
//
// Path: /:sha/sub/:name
func GetSubtitle(c echo.Context) error {
sha := c.Param("sha")
name := c.Param("name")
if err := SanitizePath(sha); err != nil {
return err
}
if err := SanitizePath(name); err != nil {
return err
}
path := fmt.Sprintf("%s/%s/sub/%s", src.GetMetadataPath(), sha, name)
return c.File(path)
}
type Handler struct {
transcoder *src.Transcoder
}
@ -231,6 +272,8 @@ func main() {
e.GET("/:resource/:slug/:quality/:chunk", h.GetVideoSegment)
e.GET("/:resource/:slug/audio/:audio/:chunk", h.GetAudioSegment)
e.GET("/:resource/:slug/info", GetInfo)
e.GET("/:sha/attachment/:name", GetAttachment)
e.GET("/:sha/sub/:name", GetSubtitle)
e.Logger.Fatal(e.Start(":7666"))
}

42
transcoder/src/extract.go Normal file
View File

@ -0,0 +1,42 @@
package src
import (
"fmt"
"os"
"os/exec"
)
func GetMetadataPath() string {
out := os.Getenv("GOCODER_METADATA_ROOT")
if out == "" {
return "/metadata"
}
return out
}
func extract(path string, sha string, subs *[]Subtitle) {
fmt.Printf("Extract subs and fonts for %s", path)
cmd := exec.Command(
"ffmpeg",
"-dump_attachment:t", "",
"-i", path,
)
cmd.Dir = fmt.Sprintf("/%s/%s/att/", GetMetadataPath(), sha)
for _, sub := range *subs {
if ext := sub.Extension; ext != nil {
cmd.Args = append(
cmd.Args,
"-map", fmt.Sprintf("0:s:%d", sub.Index),
"-c:s", "copy",
fmt.Sprintf("/%s/%s/sub/%d.%s", GetMetadataPath(), sha, sub.Index, *ext),
)
}
}
fmt.Printf("Starting extraction with the command: %s", cmd)
cmd.Stdout = nil
err := cmd.Run()
if err != nil {
fmt.Println("Error starting ffmpeg extract:", err)
}
}

View File

@ -56,6 +56,13 @@ func GetPath(resource string, slug string) (string, error) {
return ret.Path, nil
}
func SanitizePath(path string) error {
if strings.Contains(path, "/") || strings.Contains(path, "..") {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid parameter. Can't contains path delimiters or ..")
}
return nil
}
func GetClientId(c echo.Context) (string, error) {
key := c.Request().Header.Get("X-CLIENT-ID")
if key == "" {