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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
What changed in WittyIntegratedFileBrowser, newest first. What changed before it had a repository of its own is in the
[WittyMediaServer changelog](https://github.com/wittywilla/WittyMediaServer/blob/master/CHANGELOG.md).

## 0.4.2

- **Drive sizes match the rest of the server.** Server folders counted 1000 bytes to a KB while the file browser's own window, Statistics and the server count 1024, as your computer does, so the same disk read 511 GB in one place and 476 GB in another. It now reads the same everywhere.
- **Fixed:** asking for a page of server folders with an enormous size could stop the file browser's helper.

## Developer notes for 0.4.2

- `page` compares the limit with what's left instead of adding it to the offset: `start+limit` wrapped round for a limit near the largest int and sliced with a negative end (a panic, found by the new `FuzzItemsPaging`).
- `size` uses 1024, as `fmtSize` in `web/FileBrowser.tsx` and the server's `fmtBytes` do; `TestSizeCountsLikeTheServer`.

## 0.4.1

- The README's picture is retaken from WittyMediaServer 0.8.0-alpha, on a test server with open media only.
Expand Down
35 changes: 35 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 WittyWilla and contributors

package wittyintegratedfilebrowser

import (
"encoding/json"
"testing"

"github.com/wittywilla/wittymodule/sdui"
)

// A page of places is asked for with any offset and limit a caller sends: none may crash the
// helper, and a page never holds more than was asked for or than there is.
func FuzzItemsPaging(f *testing.F) {
f.Add(`{"source":{"type":"wittyintegratedfilebrowser.places"},"offset":1,"limit":9223372036854775807,"viewer":{"admin":true}}`)
f.Add(`{"source":{"type":"wittyintegratedfilebrowser.places"},"offset":-5,"limit":-1,"viewer":{"admin":true}}`)
f.Add(`{"source":{"type":"wittyintegratedfilebrowser.folder","path":"../.."},"offset":0,"limit":2}`)
places := func() []Place {
return []Place{{Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1, Total: 2}, {Name: "Movies", Path: "/home/me/Movies", Group: "folders"}, {Name: "NAS", Path: "/mnt/nas", Group: "drives"}}
}
f.Fuzz(func(t *testing.T, raw string) {
var r sdui.ItemsRequest
if json.Unmarshal([]byte(raw), &r) != nil {
return
}
a, err := Items(r, places)
if err != nil {
return
}
if len(a.Items) > a.Total || (r.Limit > 0 && len(a.Items) > r.Limit) {
t.Errorf("offset %d limit %d: %d items of %d", r.Offset, r.Limit, len(a.Items), a.Total)
}
})
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "wittyintegratedfilebrowser",
"name": "WittyIntegratedFileBrowser",
"version": "0.4.1",
"version": "0.4.2",
"repo": "wittywilla/WittyIntegratedFileBrowser",
"license": "AGPL-3.0-or-later",
"delivery": "helper",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wittywilla/wittyintegratedfilebrowser",
"version": "0.4.1",
"version": "0.4.2",
"description": "A folder browser in a window, for choosing folders on another computer from any device: drives, places, path bar, filtering, several folders at once. Read-only.",
"license": "AGPL-3.0-or-later",
"author": "WittyWilla (https://github.com/wittywilla)",
Expand Down
7 changes: 5 additions & 2 deletions sdui.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func Items(r sdui.ItemsRequest, places func() []Place) (sdui.ItemsAnswer, error)
func page(r sdui.ItemsRequest, all []sdui.Item) sdui.ItemsAnswer {
start := min(max(r.Offset, 0), len(all))
end := len(all)
if r.Limit > 0 && start+r.Limit < end {
// Compared as what's left, not start+limit: a limit near the largest int would wrap round.
if r.Limit > 0 && r.Limit < end-start {
end = start + r.Limit
}
return sdui.ItemsAnswer{Items: all[start:end], More: end < len(all), Total: len(all)}
Expand Down Expand Up @@ -153,8 +154,10 @@ func folderItems(path string) []sdui.Item {
// fileIcons is the catalogue's icon for each kind of file FileKind names.
var fileIcons = map[string]string{"video": "video", "audio": "music", "book": "book", "disc": "disc", "image": "photo", "subtitle": "list"}

// size is a number of bytes as the server and the file browser's own window write it: 1024 to a
// KB, as the operating systems show a disk, so a drive reads the same everywhere.
func size(b uint64) string {
const k = 1000
const k = 1024
switch {
case b >= k*k*k*k:
return fmt.Sprintf("%.1f TB", float64(b)/(k*k*k*k))
Expand Down
17 changes: 16 additions & 1 deletion sdui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ask(t *testing.T, raw string) sdui.ItemsRequest {
func TestPlaceItems(t *testing.T) {
places := func() []Place {
return []Place{
{Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1_500_000_000_000, Total: 4_000_000_000_000},
{Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1_649_267_441_664, Total: 4_398_046_511_104},
{Name: "Movies", Path: "/home/me/Movies", Group: "folders"},
{Name: "NAS", Path: "/mnt/nas", Group: "drives", Label: "Network"},
}
Expand Down Expand Up @@ -154,3 +154,18 @@ func TestItemsAreWhatEachSourceYields(t *testing.T) {
}
}
}

// A drive's size reads as the server and the file browser's window write it: 1024 to a KB.
func TestSizeCountsLikeTheServer(t *testing.T) {
for b, want := range map[uint64]string{
512: "512 bytes",
2048: "2 KB",
5 << 20: "5 MB",
548_682_072_064: "511 GB",
2_000_398_934_016: "1.8 TB",
} {
if got := size(b); got != want {
t.Errorf("size(%d) = %q, want %q", b, got, want)
}
}
}