add filters
This commit is contained in:
parent
62174abbf7
commit
f4708458a0
75
main.go
75
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -29,6 +30,8 @@ var (
|
||||||
defaultApiUrl = "http://127.0.0.1:4000"
|
defaultApiUrl = "http://127.0.0.1:4000"
|
||||||
defaultServerDirectory = "."
|
defaultServerDirectory = "."
|
||||||
defaultDownloadDirectory = "."
|
defaultDownloadDirectory = "."
|
||||||
|
defaultName = ""
|
||||||
|
defaultOrder = "date"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -37,6 +40,8 @@ var (
|
||||||
flagApiUrl = "api-url"
|
flagApiUrl = "api-url"
|
||||||
flagDirectory = "directory"
|
flagDirectory = "directory"
|
||||||
flagDebug = "debug"
|
flagDebug = "debug"
|
||||||
|
flagName = "name"
|
||||||
|
flagOrder = "order"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -107,9 +112,19 @@ func main() {
|
||||||
Aliases: []string{"u"},
|
Aliases: []string{"u"},
|
||||||
Value: defaultApiUrl,
|
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 {
|
Action: func(ctx *cli.Context) error {
|
||||||
return runShell(ctx, "play", "")
|
return runShell(ctx, "play")
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -127,9 +142,19 @@ func main() {
|
||||||
Aliases: []string{"d"},
|
Aliases: []string{"d"},
|
||||||
Value: defaultDownloadDirectory,
|
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 {
|
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:"-"`
|
RelativePath string `json:"-"`
|
||||||
Head []byte `json:"-"`
|
Head []byte `json:"-"`
|
||||||
ContentType string `json:"-"`
|
ContentType string `json:"-"`
|
||||||
|
Date int64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) GenerateHeadAndContentType() {
|
func (f *File) GenerateHeadAndContentType() {
|
||||||
|
@ -191,6 +217,7 @@ func getFiles(directory, url string) ([]File, error) {
|
||||||
Name: basename,
|
Name: basename,
|
||||||
Path: path,
|
Path: path,
|
||||||
RelativePath: relativePath,
|
RelativePath: relativePath,
|
||||||
|
Date: info.ModTime().Unix(),
|
||||||
Url: fmt.Sprintf("%s/api/stream?path=%s", url, netUrl.QueryEscape(relativePath)),
|
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)})
|
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, " ")
|
return e.JSONPretty(http.StatusOK, files, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,7 +320,7 @@ func promptInput(defaultValue string) string {
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilesWyWildcard(files, result []File) []File {
|
func getFilesByWildcard(files, result []File) []File {
|
||||||
for i := len(files) - 1; i >= 0; i-- {
|
for i := len(files) - 1; i >= 0; i-- {
|
||||||
result = append(result, files[i])
|
result = append(result, files[i])
|
||||||
}
|
}
|
||||||
|
@ -330,8 +385,8 @@ func getFilesByWordSplit(files, result []File, words []string) []File {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestFileList(url string) []File {
|
func requestFileList(url, name, order string) []File {
|
||||||
response, err := http.Get(fmt.Sprintf("%s/api/list", url))
|
response, err := http.Get(fmt.Sprintf("%s/api/list?name=%s&order=%s", url, name, order))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
||||||
|
@ -381,7 +436,7 @@ func selectFiles(files []File, input string) []File {
|
||||||
isRange3, _ := regexp.MatchString(range3Regex, input)
|
isRange3, _ := regexp.MatchString(range3Regex, input)
|
||||||
|
|
||||||
if input == "*" || input == "*+" {
|
if input == "*" || input == "*+" {
|
||||||
result = getFilesWyWildcard(files, result)
|
result = getFilesByWildcard(files, result)
|
||||||
} else if input == "-*" {
|
} else if input == "-*" {
|
||||||
result = files
|
result = files
|
||||||
} else if isRange1 { // a-b
|
} else if isRange1 { // a-b
|
||||||
|
@ -413,8 +468,12 @@ func selectFiles(files []File, input string) []File {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func runShell(ctx *cli.Context, action, directory string) error {
|
func runShell(ctx *cli.Context, action string) error {
|
||||||
files := requestFileList(strings.TrimSuffix(ctx.String(flagApiUrl), "/"))
|
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)
|
showFiles(files)
|
||||||
|
|
||||||
input := promptInput("1")
|
input := promptInput("1")
|
||||||
|
|
Loading…
Reference in a new issue