add filters

This commit is contained in:
Simon Vieille 2022-08-22 23:51:22 +02:00
parent 62174abbf7
commit f4708458a0
Signed by: deblan
GPG Key ID: 579388D585F70417
1 changed files with 67 additions and 8 deletions

75
main.go
View File

@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
@ -29,6 +30,8 @@ var (
defaultApiUrl = "http://127.0.0.1:4000"
defaultServerDirectory = "."
defaultDownloadDirectory = "."
defaultName = ""
defaultOrder = "date"
)
var (
@ -37,6 +40,8 @@ var (
flagApiUrl = "api-url"
flagDirectory = "directory"
flagDebug = "debug"
flagName = "name"
flagOrder = "order"
)
func main() {
@ -107,9 +112,19 @@ func main() {
Aliases: []string{"u"},
Value: defaultApiUrl,
},
&cli.StringFlag{
Name: flagName,
Aliases: []string{"n"},
Value: defaultName,
},
&cli.StringFlag{
Name: flagOrder,
Aliases: []string{"o"},
Value: defaultOrder,
},
},
Action: func(ctx *cli.Context) error {
return runShell(ctx, "play", "")
return runShell(ctx, "play")
},
},
{
@ -127,9 +142,19 @@ func main() {
Aliases: []string{"d"},
Value: defaultDownloadDirectory,
},
&cli.StringFlag{
Name: flagName,
Aliases: []string{"n"},
Value: defaultName,
},
&cli.StringFlag{
Name: flagOrder,
Aliases: []string{"o"},
Value: defaultOrder,
},
},
Action: func(ctx *cli.Context) error {
return runShell(ctx, "download", strings.TrimSuffix(ctx.String(flagDirectory), "/"))
return runShell(ctx, "download")
},
},
},
@ -147,6 +172,7 @@ type File struct {
RelativePath string `json:"-"`
Head []byte `json:"-"`
ContentType string `json:"-"`
Date int64 `json:"-"`
}
func (f *File) GenerateHeadAndContentType() {
@ -191,6 +217,7 @@ func getFiles(directory, url string) ([]File, error) {
Name: basename,
Path: path,
RelativePath: relativePath,
Date: info.ModTime().Unix(),
Url: fmt.Sprintf("%s/api/stream?path=%s", url, netUrl.QueryEscape(relativePath)),
}
@ -240,6 +267,34 @@ func apiList(e echo.Context, directory, url string) error {
return e.JSON(http.StatusInternalServerError, ApiError{Error: fmt.Sprintf("%s", err)})
}
name := e.QueryParam("name")
order := e.QueryParam("order")
if name != "" {
name = strings.ToLower(name)
var newFiles []File
for _, file := range files {
isMatchingName, _ := regexp.MatchString(name, strings.ToLower(file.Name))
if isMatchingName {
newFiles = append(newFiles, file)
}
}
files = newFiles
}
sort.SliceStable(files, func(i, j int) bool {
if order == "date" {
return files[i].Date < files[j].Date
} else if order == "name" {
return files[i].Name < files[j].Name
}
return false
})
return e.JSONPretty(http.StatusOK, files, " ")
}
@ -265,7 +320,7 @@ func promptInput(defaultValue string) string {
return input
}
func getFilesWyWildcard(files, result []File) []File {
func getFilesByWildcard(files, result []File) []File {
for i := len(files) - 1; i >= 0; i-- {
result = append(result, files[i])
}
@ -330,8 +385,8 @@ func getFilesByWordSplit(files, result []File, words []string) []File {
return result
}
func requestFileList(url string) []File {
response, err := http.Get(fmt.Sprintf("%s/api/list", url))
func requestFileList(url, name, order string) []File {
response, err := http.Get(fmt.Sprintf("%s/api/list?name=%s&order=%s", url, name, order))
if err != nil {
fmt.Println(err)
@ -381,7 +436,7 @@ func selectFiles(files []File, input string) []File {
isRange3, _ := regexp.MatchString(range3Regex, input)
if input == "*" || input == "*+" {
result = getFilesWyWildcard(files, result)
result = getFilesByWildcard(files, result)
} else if input == "-*" {
result = files
} else if isRange1 { // a-b
@ -413,8 +468,12 @@ func selectFiles(files []File, input string) []File {
return result
}
func runShell(ctx *cli.Context, action, directory string) error {
files := requestFileList(strings.TrimSuffix(ctx.String(flagApiUrl), "/"))
func runShell(ctx *cli.Context, action string) error {
directory := strings.TrimSuffix(ctx.String(flagDirectory), "/")
name := ctx.String(flagName)
order := ctx.String(flagOrder)
files := requestFileList(strings.TrimSuffix(ctx.String(flagApiUrl), "/"), name, order)
showFiles(files)
input := promptInput("1")