add api/list route

This commit is contained in:
Simon Vieille 2022-08-22 11:33:31 +02:00
parent 707246bb75
commit 2c059efbfb
Signed by: deblan
GPG Key ID: 579388D585F70417
1 changed files with 97 additions and 15 deletions

112
main.go
View File

@ -6,9 +6,11 @@ import (
"os"
"path/filepath"
"github.com/h2non/filetype"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/urfave/cli/v2"
"strings"
)
var Commands = []*cli.Command{}
@ -17,6 +19,11 @@ var version = "dev"
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "debug",
},
},
Commands: []*cli.Command{
{
Name: "serve",
@ -47,17 +54,24 @@ func main() {
Action: func(ctx *cli.Context) error {
listen := ctx.String("listen")
port := ctx.Int64("port")
directory := ctx.String("directory")
url := ctx.String("url")
directory := strings.TrimSuffix(ctx.String("directory"), "/")
url := strings.TrimSuffix(ctx.String("api-url"), "/")
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
if ctx.Bool("debug") {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
}
e.GET("/api/list", func(ctx echo.Context) error {
return apiList(ctx, directory, url)
})
e.GET("/api/stream", func(ctx echo.Context) error {
return apiStream(ctx, directory, url)
})
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", listen, port)))
return nil
@ -72,39 +86,107 @@ func main() {
}
type File struct {
Name string `json:"name"`
Url string `json:"url"`
Name string `json:"name"`
Url string `json:"url"`
Path string `json:"-"`
RelativePath string `json:"-"`
Head []byte `json:"-"`
ContentType string `json:"-"`
}
type InternalError struct {
func (f *File) GenerateHeadAndContentType() {
fo, _ := os.Open(f.Path)
head := make([]byte, 261)
fo.Read(head)
f.Head = head
f.ContentType = http.DetectContentType(head)
fo.Close()
}
func (f File) IsSupported() bool {
return filetype.IsVideo(f.Head)
}
type ApiError struct {
Error string `json:"error"`
}
func apiList(ctx echo.Context, directory, url string) error {
func getFiles(directory, url string) ([]File, error) {
files := []File{}
absoluteRootPath, err := filepath.Abs(directory)
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil, err
}
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if info.IsDir() {
return nil
}
basename := string(info.Name())
relativePath := strings.Replace(path, absoluteRootPath, "", 1)
file := File{
Name: string(info.Name()),
Url: fmt.Sprintf("%s/api/file?path=%s", url, path),
Name: basename,
Path: path,
RelativePath: relativePath,
Url: fmt.Sprintf("%s/api/stream?path=%s", url, relativePath),
}
files = append(files, file)
file.GenerateHeadAndContentType()
if file.IsSupported() {
files = append(files, file)
}
return nil
})
if err != nil {
return ctx.JSON(http.StatusInternalServerError, InternalError{Error: fmt.Sprintf("%s", err)})
return nil, err
}
return ctx.JSON(http.StatusOK, files)
return files, nil
}
func apiStream(e echo.Context, directory, url string) error {
files, err := getFiles(directory, url)
path := e.QueryParam("path")
if err != nil {
return e.JSON(http.StatusInternalServerError, ApiError{Error: fmt.Sprintf("%s", err)})
}
fmt.Printf("%+v\n", path)
if path == "" {
return e.JSON(http.StatusBadRequest, ApiError{Error: "\"path\" query param is missing"})
}
for _, file := range files {
if file.RelativePath == path {
stat, _ := os.Stat(file.Path)
e.Response().Header().Add("Content-Length", string(stat.Size()))
http.ServeFile(e.Response(), e.Request(), file.Path)
}
}
return e.JSON(http.StatusNotFound, ApiError{Error: "file not found"})
}
func apiList(e echo.Context, directory, url string) error {
files, err := getFiles(directory, url)
if err != nil {
return e.JSON(http.StatusInternalServerError, ApiError{Error: fmt.Sprintf("%s", err)})
}
return e.JSONPretty(http.StatusOK, files, " ")
}