From f4708458a0fbeba3ae5bb52416012b199ee062a2 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 22 Aug 2022 23:51:22 +0200 Subject: [PATCH] add filters --- main.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index fa19952..991b9ea 100644 --- a/main.go +++ b/main.go @@ -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")