Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"os"
"path"
"sort"
"unicode/utf8"

"strings"
Expand Down Expand Up @@ -70,6 +71,18 @@ func servePath(w http.ResponseWriter, r *http.Request) {
if p == "" {
p = "."
}
sortField := r.URL.Query().Get("sort")
if sortField == "" || (sortField != "mtime" && sortField != "size") {
sortField = "name"
}
desc := r.URL.Query().Get("desc")
var descb bool
if desc != "true" {
descb = false
} else {
descb = true
}

info, err := os.Stat(p)
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -94,7 +107,36 @@ func servePath(w http.ResponseWriter, r *http.Request) {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size())})
}
}
if err := tmpl["files"].Execute(w, model.FilesPageModel{Path: model.Path(p), Items: items, AllowWrite: allowWrite, SelectState: selectState}); err != nil {
sort.Slice(items, func(i, j int) bool {
var first int
var second int
if !descb {
first = i
second = j
} else {
first = j
second = i
}
switch sortField {
case "mtime":
return items[first].LastModified.Before(items[second].LastModified)
case "size":
if items[i].IsDir && !items[j].IsDir {
return true
}
if !items[i].IsDir && items[j].IsDir {
return false
}
if items[first].IsDir && items[second].IsDir {
return items[first].Size.(model.DirSize) < items[second].Size.(model.DirSize)
} else {
return items[first].Size.(model.FileSize) < items[second].Size.(model.FileSize)
}
default:
return strings.ToLower(items[first].Name) < strings.ToLower(items[second].Name)
}
})
if err := tmpl["files"].Execute(w, model.FilesPageModel{Path: model.Path(p), Items: items, AllowWrite: allowWrite, SelectState: selectState, SortField: sortField, Desc: descb}); err != nil {
log.Fatalln("[ERROR]", err)
}

Expand Down
2 changes: 2 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type FilesPageModel struct {
Items []Item
AllowWrite bool
SelectState string
SortField string
Desc bool
}

type DeletePageModel struct {
Expand Down
4 changes: 4 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ form.upload-form {
#drop-area {
min-height: 70vh;
min-width: 100vw;
}

th a {
text-decoration: none;
}
36 changes: 31 additions & 5 deletions templates/files.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@
<tr>
<th>
{{if ne .SelectState "all"}}
<a class="select-icon" href="?&select=all">
<a class="select-icon" href="?select=all&sort={{.SortField}}&desc={{.Desc}}">
<svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 0 24 24" width="24">
<path
d="m20.4961766 5.62668182c.8758909.3077952 1.5038234 1.14222595 1.5038234 2.12331818v10c0 2.3472102-1.9027898 4.25-4.25 4.25h-10c-.98109223 0-1.81552298-.6279325-2.12331818-1.5038234l2.09728006.0033799 10.02603812.0004435c1.5187831 0 2.75-1.2312169 2.75-2.75v-10l-.0039806-.05098057zm-3.2493636-3.62668182c1.2426407 0 2.25 1.00735931 2.25 2.25v12.996813c0 1.2426407-1.0073593 2.25-2.25 2.25h-12.996813c-1.24264069 0-2.25-1.0073593-2.25-2.25v-12.996813c0-1.24264069 1.00735931-2.25 2.25-2.25zm0 1.5h-12.996813c-.41421356 0-.75.33578644-.75.75v12.996813c0 .4142136.33578644.75.75.75h12.996813c.4142136 0 .75-.3357864.75-.75v-12.996813c0-.41421356-.3357864-.75-.75-.75zm-7.66566736 7.8581942 3.88852426-3.88852429c.2928932-.29289321.767767-.29289321 1.0606602 0 .2662665.26626657.2904726.68293025.0726181.97654174l-.0726181.08411844-4.5 4.50000001c-.29583771.2958377-.76898983.288617-1.05672616.0041163l-.07360394-.0844464-1.5-2c-.24852814-.3313708-.18137085-.8014719.15-1.05.30124623-.22593467.71714548-.1909723.97699676.06621555l.07300324.08378445.98114564 1.3081942 3.88852426-3.88852429z"
fill="#212121" />
</svg>
</a>
{{else}}
<a class="select-icon" href="?&select=none">
<a class="select-icon" href="?select=none&sort={{.SortField}}&desc={{.Desc}}">
<svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 0 24 24" width="24">
<path
d="m20.4961766 5.62668182c.8758909.3077952 1.5038234 1.14222595 1.5038234 2.12331818v10c0 2.3472102-1.9027898 4.25-4.25 4.25h-10c-.98109223 0-1.81552298-.6279325-2.12331818-1.5038234l2.09728006.0033799 10.02603812.0004435c1.5187831 0 2.75-1.2312169 2.75-2.75v-10l-.0039806-.05098057zm-3.2493636-3.62668182c1.2426407 0 2.25 1.00735931 2.25 2.25v12.996813c0 1.2426407-1.0073593 2.25-2.25 2.25h-12.996813c-1.24264069 0-2.25-1.0073593-2.25-2.25v-12.996813c0-1.24264069 1.00735931-2.25 2.25-2.25zm0 1.5h-12.996813c-.41421356 0-.75.33578644-.75.75v12.996813c0 .4142136.33578644.75.75.75h12.996813c.4142136 0 .75-.3357864.75-.75v-12.996813c0-.41421356-.3357864-.75-.75-.75z"
Expand All @@ -80,9 +80,35 @@
</a>
{{end}}
</th>
<th class="cell-name" colspan="2">Name</th>
<th class="cell-mtime">Last Modified</th>
<th class="cell-size">Size</th>
{{if eq .SortField "name"}}
{{if .Desc}}
<th class="cell-name" colspan="2"><a href="?select={{.SelectState}}&sort=name&desc=false">↓ Name</a></th>
{{else}}
<th class="cell-name" colspan="2"><a href="?select={{.SelectState}}&sort=name&desc=true">↑ Name</a></th>
{{end}}
{{else}}
<th class="cell-name" colspan="2"><a href="?select={{.SelectState}}&sort=name&desc=false">Name</a></th>
{{end}}

{{if eq .SortField "mtime"}}
{{if .Desc}}
<th class="cell-mtime"><a href="?select={{.SelectState}}&sort=mtime&desc=false">↓ Last Modified</a></th>
{{else}}
<th class="cell-mtime"><a href="?select={{.SelectState}}&sort=mtime&desc=true">↑ Last Modified</a></th>
{{end}}
{{else}}
<th class="cell-mtime"><a href="?select={{.SelectState}}&sort=mtime&desc=false">Last Modified</a></th>
{{end}}

{{if eq .SortField "size"}}
{{if .Desc}}
<th class="cell-size"><a href="?select={{.SelectState}}&sort=size&desc=false">↓ Size</a></th>
{{else}}
<th class="cell-size"><a href="?select={{.SelectState}}&sort=size&desc=true">↑ Size</a></th>
{{end}}
{{else}}
<th class="cell-size"><a href="?select={{.SelectState}}&sort=size&desc=false">Size</a></th>
{{end}}
</tr>
</thead>
<tbody>
Expand Down