mu-go/main.go

193 lines
3.9 KiB
Go
Raw Normal View History

2022-08-21 23:58:06 +02:00
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
2022-08-22 00:00:05 +02:00
2022-08-22 11:33:31 +02:00
"github.com/h2non/filetype"
2022-08-22 00:00:05 +02:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/urfave/cli/v2"
2022-08-22 11:33:31 +02:00
"strings"
2022-08-21 23:58:06 +02:00
)
var Commands = []*cli.Command{}
var version = "dev"
func main() {
app := &cli.App{
2022-08-22 11:33:31 +02:00
Flags: []cli.Flag{
&cli.StringFlag{
Name: "debug",
},
},
2022-08-21 23:58:06 +02:00
Commands: []*cli.Command{
{
Name: "serve",
Aliases: []string{"c"},
2022-08-22 00:12:24 +02:00
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Value: "127.0.0.1",
},
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Value: "4000",
},
&cli.StringFlag{
Name: "api-url",
Aliases: []string{"u"},
Value: "http://127.0.0.1:4000",
},
&cli.StringFlag{
Name: "directory",
Aliases: []string{"d"},
Value: ".",
},
},
Usage: "complete a task on the list",
2022-08-21 23:58:06 +02:00
Action: func(ctx *cli.Context) error {
listen := ctx.String("listen")
port := ctx.Int64("port")
2022-08-22 11:33:31 +02:00
directory := strings.TrimSuffix(ctx.String("directory"), "/")
url := strings.TrimSuffix(ctx.String("api-url"), "/")
2022-08-21 23:58:06 +02:00
e := echo.New()
2022-08-22 11:33:31 +02:00
if ctx.Bool("debug") {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
}
2022-08-21 23:58:06 +02:00
e.GET("/api/list", func(ctx echo.Context) error {
return apiList(ctx, directory, url)
})
2022-08-22 11:33:31 +02:00
e.GET("/api/stream", func(ctx echo.Context) error {
return apiStream(ctx, directory, url)
})
2022-08-21 23:58:06 +02:00
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", listen, port)))
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}
type File struct {
2022-08-22 11:33:31 +02:00
Name string `json:"name"`
Url string `json:"url"`
Path string `json:"-"`
RelativePath string `json:"-"`
Head []byte `json:"-"`
ContentType string `json:"-"`
}
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()
2022-08-21 23:58:06 +02:00
}
2022-08-22 11:33:31 +02:00
func (f File) IsSupported() bool {
return filetype.IsVideo(f.Head)
}
type ApiError struct {
2022-08-22 00:12:24 +02:00
Error string `json:"error"`
}
2022-08-22 11:33:31 +02:00
func getFiles(directory, url string) ([]File, error) {
2022-08-21 23:58:06 +02:00
files := []File{}
2022-08-22 11:33:31 +02:00
absoluteRootPath, err := filepath.Abs(directory)
if err != nil {
return nil, err
}
2022-08-21 23:58:06 +02:00
2022-08-22 11:33:31 +02:00
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
2022-08-21 23:58:06 +02:00
if err != nil {
return err
}
2022-08-22 11:33:31 +02:00
if info.IsDir() {
2022-08-21 23:58:06 +02:00
return nil
}
2022-08-22 11:33:31 +02:00
basename := string(info.Name())
relativePath := strings.Replace(path, absoluteRootPath, "", 1)
2022-08-21 23:58:06 +02:00
file := File{
2022-08-22 11:33:31 +02:00
Name: basename,
Path: path,
RelativePath: relativePath,
Url: fmt.Sprintf("%s/api/stream?path=%s", url, relativePath),
2022-08-21 23:58:06 +02:00
}
2022-08-22 11:33:31 +02:00
file.GenerateHeadAndContentType()
if file.IsSupported() {
files = append(files, file)
}
2022-08-21 23:58:06 +02:00
return nil
})
2022-08-22 00:12:24 +02:00
2022-08-21 23:58:06 +02:00
if err != nil {
2022-08-22 11:33:31 +02:00
return nil, err
}
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)})
2022-08-21 23:58:06 +02:00
}
2022-08-22 11:33:31 +02:00
return e.JSONPretty(http.StatusOK, files, " ")
2022-08-21 23:58:06 +02:00
}