51 lines
999 B
Go
51 lines
999 B
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"gitnet.fr/deblan/mu-go/internal/api"
|
|
"gitnet.fr/deblan/mu-go/internal/fs"
|
|
)
|
|
|
|
func List(e echo.Context, directory, url string) error {
|
|
files, err := fs.DirectoryFiles(directory, url)
|
|
|
|
if err != nil {
|
|
return e.JSON(http.StatusInternalServerError, api.ApiError{Error: fmt.Sprintf("%s", err)})
|
|
}
|
|
|
|
name := e.QueryParam("name")
|
|
order := e.QueryParam("order")
|
|
|
|
if name != "" {
|
|
name = strings.ToLower(name)
|
|
var newFiles []fs.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, " ")
|
|
}
|