fileserver: New file browse template (#5427)

* fileserver: New file browse template

* Redo extension/icon logic; minor color tweaks

* Fine-tune image display
This commit is contained in:
Matt Holt 2023-03-10 11:19:31 -07:00 committed by GitHub
parent 9e943319b4
commit 6cc3cbbc69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 663 additions and 194 deletions

View file

@ -152,12 +152,20 @@ func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, dir fs.ReadDi
// browseApplyQueryParams applies query parameters to the listing.
// It mutates the listing and may set cookies.
func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseTemplateContext) {
layoutParam := r.URL.Query().Get("layout")
sortParam := r.URL.Query().Get("sort")
orderParam := r.URL.Query().Get("order")
limitParam := r.URL.Query().Get("limit")
offsetParam := r.URL.Query().Get("offset")
// first figure out what to sort by
switch layoutParam {
case "list", "grid", "":
listing.Layout = layoutParam
default:
listing.Layout = "list"
}
// figure out what to sort by
switch sortParam {
case "":
sortParam = sortByNameDirFirst

File diff suppressed because it is too large Load diff

View file

@ -134,6 +134,9 @@ type browseTemplateContext struct {
// Sorting order
Order string `json:"order,omitempty"`
// Display format (list or grid)
Layout string `json:"layout,omitempty"`
}
// Breadcrumbs returns l.Path where every element maps
@ -229,6 +232,16 @@ type fileInfo struct {
IsSymlink bool `json:"is_symlink"`
}
// HasExt returns true if the filename has any of the given suffixes, case-insensitive.
func (fi fileInfo) HasExt(exts ...string) bool {
for _, ext := range exts {
if strings.HasSuffix(strings.ToLower(fi.Name), strings.ToLower(ext)) {
return true
}
}
return false
}
// HumanSize returns the size of the file as a
// human-readable string in IEC format (i.e.
// power of 2 or base 1024).