diff --git a/README.md b/README.md index c9358f2..982e969 100644 --- a/README.md +++ b/README.md @@ -337,9 +337,18 @@ compression: most large files do not compress anyway. Clicking an image, a video, an audio file or a PDF opens it in a viewer, with Download next to it and the previous and next ones of its folder a -click or an arrow key away (Escape goes back to the folder). Listings show -thumbnails of JPEG, PNG, GIF, WebP and BMP images, and a link to a single -file shows it on the link's page. The same works with a query: `?view` (the +click, an arrow key or (for pictures, on touch screens) a swipe away, in +the order the folder was sorted in; Escape goes back to the folder, and +the next picture is fetched ahead. Listings show thumbnails of JPEG, PNG, +GIF, WebP and BMP images, and a link to a single file shows it on the +link's page. + +Folders can be laid out as a list or as a grid of thumbnails, with the +buttons next to Download zip; the grid keeps everything else of the list +(ticking entries, their ⋯ menu, sorting, filtering). Folders whose files +are mostly pictures and videos (two thirds of them, at least three) open +as a grid. A layout chosen with the buttons is remembered for the folder, +in the browser; `?layout=grid` or `?layout=list` asks for one in a link. The same works with a query: `?view` (the viewer), `?inline` (the file, to be shown in a browser) and `?thumb` (a thumbnail of at most 256×256 pixels). diff --git a/internal/server/gallery_test.go b/internal/server/gallery_test.go new file mode 100644 index 0000000..0c3bf4a --- /dev/null +++ b/internal/server/gallery_test.go @@ -0,0 +1,65 @@ +package server + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestGallery(t *testing.T) { + f := newFixture(t) + html := []string{"Accept", "text/html"} + // Pictures named against the order they were taken in. + for i, name := range []string{"c.jpg", "b.png", "a.jpg"} { + f.write(t, "photos/"+name, "not really a picture") + mod := time.Date(2026, 1, 1+i, 0, 0, 0, 0, time.UTC) + if err := os.Chtimes(filepath.Join(f.dir, "photos", name), mod, mod); err != nil { + t.Fatal(err) + } + } + f.write(t, "photos/notes.txt", "notes") + f.write(t, "docs/a.txt", "a") + f.write(t, "docs/b.pdf", "b") + f.write(t, "docs/c.jpg", "c") + + // Folders of mostly pictures and videos are a grid, others a list. + _, body := f.do(t, "GET", "/photos/", html...) + if !strings.Contains(body, ``) || + !strings.Contains(body, ``) { + t.Errorf("photos: %s", body) + } + _, body = f.do(t, "GET", "/docs/", html...) + if !strings.Contains(body, `
`) { + t.Errorf("docs: %s", body) + } + // As asked, and kept when sorting. + _, body = f.do(t, "GET", "/photos/?layout=list&sort=modified", html...) + if !strings.Contains(body, `
`) || + !strings.Contains(body, `href="?sort=modified&order=desc&layout=list"`) || + !strings.Contains(body, `href="?layout=grid&sort=modified"`) { + t.Errorf("asked for a list: %s", body) + } + // The viewer goes through the folder in the order it was listed in. + _, body = f.do(t, "GET", "/photos/?sort=modified&order=desc", html...) + if !strings.Contains(body, `href="/photos/a.jpg?view&sort=modified&order=desc"`) { + t.Errorf("viewer links: %s", body) + } + _, body = f.do(t, "GET", "/photos/b.png?view&sort=modified&order=desc", html...) + for _, want := range []string{ + `id="prev" href="/photos/a.jpg?view&sort=modified&order=desc"`, + `id="next" href="/photos/c.jpg?view&sort=modified&order=desc"`, + ``, + `data-folder="/photos/?sort=modified&order=desc"`, + "2 of 3", + } { + if !strings.Contains(body, want) { + t.Errorf("viewer lacks %s", want) + } + } + _, body = f.do(t, "GET", "/photos/b.png?view", html...) + if !strings.Contains(body, `id="prev" href="/photos/a.jpg?view"`) || !strings.Contains(body, `data-folder="/photos/"`) { + t.Errorf("by name: %s", body) + } +} diff --git a/internal/server/listing.go b/internal/server/listing.go index be39144..7df0af3 100644 --- a/internal/server/listing.go +++ b/internal/server/listing.go @@ -144,25 +144,30 @@ func (s *Server) usageOf(name string) (*usage, error) { type listing struct { page - Path string - Action string // where this folder's forms post to - Home string // where the first crumb leads - Crumbs []crumb - Parent string - Items []listItem - Upload *uploadForm - Mkdir bool - Sort string - Desc bool - Summary string - Uploaded int - Created *listItem - Renamed *listItem - Restored *listItem - Deleted int // entries just deleted - Trashed bool // they went to the trash - Undo *undo - Moved *moved // entries just moved or copied elsewhere + Path string + Action string // where this folder's forms post to + Home string // where the first crumb leads + Crumbs []crumb + Parent string + Items []listItem + Upload *uploadForm + Mkdir bool + Sort string + Desc bool + // Layout lays the entries out as a "list", or as a "grid" of + // thumbnails; LayoutAuto is set when it was picked from what the + // folder holds rather than asked for (see layoutOf). + Layout string + LayoutAuto bool + Summary string + Uploaded int + Created *listItem + Renamed *listItem + Restored *listItem + Deleted int // entries just deleted + Trashed bool // they went to the trash + Undo *undo + Moved *moved // entries just moved or copied elsewhere // TrashKeep says how long deleted entries stay in the trash; "" if // they are deleted at once. TrashKeep string @@ -295,12 +300,70 @@ func (l listing) PickHint() string { return acts[0] } -// SortHref links a column header: a second click reverses the order. +// SortHref links a column header: a second click reverses the order. A +// layout asked for stays. func (l listing) SortHref(col string) string { + q := "?sort=" + col if l.Sort == col && !l.Desc { - return "?sort=" + col + "&order=desc" + q += "&order=desc" } - return "?sort=" + col + if !l.LayoutAuto { + q += "&layout=" + l.Layout + } + return q +} + +// LayoutHref links the layout: list or grid, in the same order. +func (l listing) LayoutHref(layout string) string { + q := "?layout=" + layout + if s := sortQuery(l.Sort, l.Desc); s != "" { + q += "&" + s + } + return q +} + +// ViewQuery opens an entry in the viewer, which goes through the folder in +// its order. +func (l listing) ViewQuery() string { + if s := sortQuery(l.Sort, l.Desc); s != "" { + return "?view&" + s + } + return "?view" +} + +// sortQuery is the query of an order other than by name, "" for that. +func sortQuery(col string, desc bool) string { + q := "" + if col != "name" { + q = "sort=" + col + } + if desc { + q += "&order=desc" + } + return strings.TrimPrefix(q, "&") +} + +// layoutOf picks how to lay out items: as ?layout= asks, or else as a grid +// of thumbnails when pictures and videos are most of the files in it. +func layoutOf(c fiber.Ctx, items []listItem) (layout string, auto bool) { + switch q := c.Query("layout"); q { + case "grid", "list": + return q, false + } + media, files := 0, 0 + for _, it := range items { + if it.IsDir { + continue + } + files++ + if it.Thumb || it.Preview == "image" || it.Preview == "video" { + media++ + } + } + if media >= 3 && 3*media >= 2*files { + return "grid", true + } + return "list", true } // SortState is a column's aria-sort value. @@ -376,6 +439,7 @@ func (s *Server) serveDir(c fiber.Ctx, dir *os.File, urlPath, realPath string, w } data.Sort, data.Desc = sortOrder(c) sortItems(data.Items, data.Sort, data.Desc) + data.Layout, data.LayoutAuto = layoutOf(c, data.Items) switch { case rel == "/": diff --git a/internal/server/preview.go b/internal/server/preview.go index ecea81e..e94184c 100644 --- a/internal/server/preview.go +++ b/internal/server/preview.go @@ -155,10 +155,13 @@ type viewerPage struct { Prev, Next *viewerLink } -type viewerLink struct{ Name, Href string } +// viewerLink leads to another file of the folder in the viewer; Src is the +// file itself, for pictures, to fetch ahead. +type viewerLink struct{ Name, Href, Src string } // serveViewer shows the file f at urlPath in a page of its own, with links -// to the previous and next files of its folder that can be shown too. +// to the previous and next files of its folder that can be shown too, in +// the order the folder was listed in (?sort and ?order). func (s *Server) serveViewer(c fiber.Ctx, f *os.File, info fs.FileInfo, urlPath string, at fileAt) error { name := path.Base(urlPath) ctype, kind := inlineType(f, name) @@ -174,14 +177,19 @@ func (s *Server) serveViewer(c fiber.Ctx, f *os.File, info fs.FileInfo, urlPath if ctype != "" { v.Kind = kind } - v.Position, v.Prev, v.Next = s.around(path.Dir(urlPath), name, at) + col, desc := sortOrder(c) + if q := sortQuery(col, desc); q != "" { + v.Folder += "?" + q + } + v.Position, v.Prev, v.Next = s.around(path.Dir(urlPath), name, at, col, desc) return s.render(c, fiber.StatusOK, "viewer", v) } // around places the file name in its folder dirPath among the files that -// can be shown, as the folder lists them: its position, and the files -// before and after it. Folders the visitor may not list place it nowhere. -func (s *Server) around(dirPath, name string, at fileAt) (string, *viewerLink, *viewerLink) { +// can be shown, as the folder lists them by col: its position, and the +// files before and after it. Folders the visitor may not list place it +// nowhere. +func (s *Server) around(dirPath, name string, at fileAt, col string, desc bool) (string, *viewerLink, *viewerLink) { d, _, dirReal, err := s.lookup(dirPath, at.allow) if err != nil { return "", nil, nil @@ -192,7 +200,11 @@ func (s *Server) around(dirPath, name string, at fileAt) (string, *viewerLink, * return "", nil, nil } shown := slices.DeleteFunc(items, func(it listItem) bool { return it.IsDir || it.Preview == "" }) - sortItems(shown, "name", false) + sortItems(shown, col, desc) + view := "?view" + if q := sortQuery(col, desc); q != "" { + view += "&" + q + } i := slices.IndexFunc(shown, func(it listItem) bool { return it.Name == name }) if i < 0 { return "", nil, nil @@ -201,7 +213,12 @@ func (s *Server) around(dirPath, name string, at fileAt) (string, *viewerLink, * if j < 0 || j >= len(shown) { return nil } - return &viewerLink{Name: shown[j].Name, Href: escapePath(at.base+path.Join(path.Dir(at.rel), shown[j].Name)) + "?view"} + href := escapePath(at.base + path.Join(path.Dir(at.rel), shown[j].Name)) + l := &viewerLink{Name: shown[j].Name, Href: href + view} + if shown[j].Preview == "image" { + l.Src = href + "?inline" + } + return l } return strconv.Itoa(i+1) + " of " + strconv.Itoa(len(shown)), link(i - 1), link(i + 1) } diff --git a/internal/server/share.go b/internal/server/share.go index 56e8d42..c6a3ef8 100644 --- a/internal/server/share.go +++ b/internal/server/share.go @@ -358,6 +358,7 @@ func (s *Server) serveSharedDir(c fiber.Ctx, dir *os.File, view *shareView, urlP } data.Sort, data.Desc = sortOrder(c) sortItems(data.Items, data.Sort, data.Desc) + data.Layout, data.LayoutAuto = layoutOf(c, data.Items) return s.render(c, fiber.StatusOK, "listing", data) } diff --git a/internal/server/templates/app.js b/internal/server/templates/app.js index 0b746a1..38aba4a 100644 --- a/internal/server/templates/app.js +++ b/internal/server/templates/app.js @@ -63,7 +63,7 @@ } // The viewer: arrows go to the previous and next file, Escape back to the - // folder. + // folder; on touch screens, so do swipes across a picture. const stage = $("#stage"); if (stage) { document.addEventListener("keydown", e => { @@ -72,6 +72,51 @@ if (to) location.assign(to.href); else if (e.key === "Escape") location.assign(stage.dataset.folder); }); + if (stage.dataset.kind === "image") { + let start = null; + stage.addEventListener("pointerdown", e => { + if (e.pointerType !== "mouse") start = { x: e.clientX, y: e.clientY }; + }); + stage.addEventListener("pointerup", e => { + if (!start) return; + const dx = e.clientX - start.x; + const dy = e.clientY - start.y; + start = null; + if (Math.abs(dx) < 60 || Math.abs(dy) > Math.abs(dx) / 2) return; + const to = dx < 0 ? $("#next") : $("#prev"); + if (to) location.assign(to.href); + }); + stage.addEventListener("pointercancel", () => { start = null; }); + } + } + + // The list or grid of a folder: a choice made here is remembered for the + // folder, on this device. + const entries = $("#entries"); + if (entries) { + const key = "goftp-layout:" + location.pathname; + const apply = layout => { + entries.classList.toggle("grid", layout === "grid"); + entries.dataset.layout = layout; + for (const b of $$(".layout-toggle [data-layout]")) { + if (b.dataset.layout === layout) b.setAttribute("aria-current", "true"); + else b.removeAttribute("aria-current"); + } + }; + let saved = null; + try { + saved = localStorage.getItem(key); + } catch {} + if ("auto" in entries.dataset && (saved === "grid" || saved === "list")) apply(saved); + for (const b of $$(".layout-toggle [data-layout]")) { + b.addEventListener("click", e => { + e.preventDefault(); + apply(b.dataset.layout); + try { + localStorage.setItem(key, b.dataset.layout); + } catch {} + }); + } } // Filtering the listing; "/" jumps to the filter, Escape clears it. @@ -544,6 +589,8 @@ const update = () => { const n = boxes.filter(b => b.checked).length; for (const b of boxes) b.closest("tr").classList.toggle("ticked", b.checked); + // In the grid, every box shows once one is ticked. + if (entries) entries.classList.toggle("picking", n > 0); picked.hidden = n === 0; count.textContent = `${n} picked`; clear.hidden = n === 0; diff --git a/internal/server/templates/base.html b/internal/server/templates/base.html index 9717974..56ae273 100644 --- a/internal/server/templates/base.html +++ b/internal/server/templates/base.html @@ -460,6 +460,39 @@ form.inline { display: inline-block; margin: 0 4px 0 0; vertical-align: middle; } .thumb-box { position: relative; display: inline-grid; place-items: center; flex: none; width: 22px; height: 22px; } .thumb { position: absolute; top: 50%; left: 50%; width: 30px; height: 30px; transform: translate(-50%, -50%); object-fit: cover; border-radius: 6px; background: var(--surface); } + .layout-toggle { display: inline-flex; } + .layout-toggle .btn { width: 40px; padding: 0; border-radius: 0; } + .layout-toggle .btn:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); } + .layout-toggle .btn:last-child { margin-left: -1px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; } + .layout-toggle .btn[aria-current] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); position: relative; } + /* The grid: the same rows, as tiles of thumbnails. */ + .files.grid, .files.grid thead, .files.grid tbody { display: block; } + .files.grid thead tr { display: flex; align-items: center; gap: 4px 18px; padding: 10px 16px; border-bottom: 1px solid var(--border); } + .files.grid th { height: auto; padding: 0; border: 0; text-align: left; } + .files.grid th.pick { width: auto; } + .files.grid th.act { display: none; } + .files.grid tbody { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding: 12px; } + .files.grid tbody tr { position: relative; display: flex; flex-direction: column; min-width: 0; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); } + .files.grid tbody tr[hidden] { display: none; } + .files.grid tbody tr:hover { border-color: var(--border-strong); } + .files.grid tbody tr:hover td, .files.grid tbody tr td { background: none; } + .files.grid tbody tr.new, .files.grid tbody tr.selected, .files.grid tbody tr.ticked { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); } + .files.grid td { display: block; width: auto; height: auto; padding: 0; border: 0; text-align: left; } + .files.grid td.date { display: none; } + .files.grid td.name a { flex-direction: column; align-items: stretch; gap: 0; } + .files.grid td.name a > svg.kind, .files.grid .thumb-box { box-sizing: border-box; width: 100%; height: auto; aspect-ratio: 1; border-radius: calc(var(--radius-sm) - 1px) calc(var(--radius-sm) - 1px) 0 0; background: var(--surface-2); } + .files.grid td.name a > svg.kind { padding: 30%; } + .files.grid .thumb-box > svg.kind { width: 40%; height: 40%; } + .files.grid .thumb { top: 0; left: 0; width: 100%; height: 100%; transform: none; border-radius: inherit; } + .files.grid td.name a > span:last-child { padding: 8px 10px 0; overflow: hidden; font-size: 13px; white-space: nowrap; text-overflow: ellipsis; } + .files.grid td.num { padding: 2px 10px 8px; color: var(--faint); font-size: 12px; min-height: 14px; } + .files.grid tr.up td.name a > span:last-child { padding-bottom: 8px; } + .files.grid td.pick { position: absolute; top: 8px; left: 8px; z-index: 1; width: auto; padding: 0; opacity: 0; } + .files.grid tr:hover td.pick, .files.grid tr.ticked td.pick, .files.grid td.pick:focus-within, .files.grid.picking td.pick { opacity: 1; } + .files.grid td.pick input { width: 18px; height: 18px; } + .files.grid td.act { position: absolute; top: 4px; right: 4px; width: auto; padding: 0; } + .files.grid td.act .icon-btn { background: color-mix(in srgb, var(--surface) 85%, transparent); } + .files.grid tr.up { justify-content: center; } .stage { display: grid; place-items: center; min-height: 240px; padding: 12px; background: var(--surface-2); overflow: hidden; } .stage img, .stage video { display: block; max-width: 100%; max-height: calc(100vh - 250px); border-radius: var(--radius-sm); } .stage audio { width: min(560px, 100%); } @@ -676,6 +709,10 @@ } @media (max-width: 720px) { .files .date, .files .where-col { display: none; } + .files.grid tbody { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; padding: 8px; } + .files.grid td.name a > span:last-child { padding: 6px 8px 0; font-size: 12px; } + .files.grid td.num { padding: 1px 8px 6px; font-size: 11px; } + .files.grid td.pick { opacity: 1; } /* Each user or rule becomes a block: who on top, the rest below. */ .list-table thead { display: none; } .list-table tbody tr { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); } @@ -785,6 +822,8 @@ + + diff --git a/internal/server/templates/listing.html b/internal/server/templates/listing.html index 9ade14d..af437ff 100644 --- a/internal/server/templates/listing.html +++ b/internal/server/templates/listing.html @@ -68,6 +68,10 @@

{{.Title}}{{if and .User (not .Upload)}} View only {{end}} {{if .Items}} + + + + Download zip {{end}} {{with .People}} @@ -158,7 +162,7 @@

+
@@ -173,7 +177,7 @@

{{else}}{{end}}{{.Name}} +

{{if $.Manage}}{{end}} diff --git a/internal/server/templates/viewer.html b/internal/server/templates/viewer.html index 0b5af14..0a952ab 100644 --- a/internal/server/templates/viewer.html +++ b/internal/server/templates/viewer.html @@ -23,7 +23,8 @@

{{.Name}}

Download -
+ {{with .Next}}{{with .Src}}{{end}}{{end}} +
{{if eq .Kind "image"}}{{.Name}} {{else if eq .Kind "video"}} {{else if eq .Kind "audio"}}
Name{{if .Thumb}}{{else}}{{end}}{{.Name}} {{.Size}} {{if or .CanDelete .CanShare $.Copy}}{{end}}