diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-05 15:56:34 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-05 15:56:34 +0300 |
| commit | fe35b52c6eb141fc7a92c6407bc44b33464692e3 (patch) | |
| tree | 3abd236a2d1b0ddd3b602cab16c543a7048161e0 | |
| parent | 9ac5b0bcfa6614c0d5656b9b09bf46bf1098d698 (diff) | |
Fix view redirect last view detection
| -rwxr-xr-x | bin/photoalbum | 15 | ||||
| -rwxr-xr-x | src/photoalbum.sh | 15 | ||||
| -rwxr-xr-x | tests/cli.sh | 37 |
3 files changed, 51 insertions, 16 deletions
diff --git a/bin/photoalbum b/bin/photoalbum index 653e1a8..ef15194 100755 --- a/bin/photoalbum +++ b/bin/photoalbum @@ -894,7 +894,7 @@ maybe_shuffle() { fi } -newest_view_html() { +last_view_number() { local -r page="$1"; shift local -r html_dir="$1"; shift @@ -902,9 +902,10 @@ newest_view_html() { -maxdepth 1 \ -regextype posix-egrep \ -regex ".*/${page}-[0-9]+\\.html" \ - -printf '%T@ %f\n' \ - | sort -nr \ - | sed -n '1{s/^[^ ]* //;p}' + -printf '%f\n' \ + | sed -n "s/^${page}-\\([0-9][0-9]*\\)\\.html$/\\1/p" \ + | sort -n \ + | tail -n 1 } album_photo_files() { @@ -1219,10 +1220,8 @@ render_view_redirects() { local prevredirect while IFS= read -r prefix; do - page=$(newest_view_html "$prefix" "$html_dir" \ - | sed 's#\(.*\)-.*.html#\1#') - lastview=$(newest_view_html "$prefix" "$html_dir" \ - | sed 's/.*-\(.*\).html/\1/') + page="$prefix" + lastview=$(last_view_number "$page" "$html_dir") prevredirect="${page}-0" nextredirect="${page}-$(( lastview + 1 ))" diff --git a/src/photoalbum.sh b/src/photoalbum.sh index 234bf8a..7d75d1c 100755 --- a/src/photoalbum.sh +++ b/src/photoalbum.sh @@ -894,7 +894,7 @@ maybe_shuffle() { fi } -newest_view_html() { +last_view_number() { local -r page="$1"; shift local -r html_dir="$1"; shift @@ -902,9 +902,10 @@ newest_view_html() { -maxdepth 1 \ -regextype posix-egrep \ -regex ".*/${page}-[0-9]+\\.html" \ - -printf '%T@ %f\n' \ - | sort -nr \ - | sed -n '1{s/^[^ ]* //;p}' + -printf '%f\n' \ + | sed -n "s/^${page}-\\([0-9][0-9]*\\)\\.html$/\\1/p" \ + | sort -n \ + | tail -n 1 } album_photo_files() { @@ -1219,10 +1220,8 @@ render_view_redirects() { local prevredirect while IFS= read -r prefix; do - page=$(newest_view_html "$prefix" "$html_dir" \ - | sed 's#\(.*\)-.*.html#\1#') - lastview=$(newest_view_html "$prefix" "$html_dir" \ - | sed 's/.*-\(.*\).html/\1/') + page="$prefix" + lastview=$(last_view_number "$page" "$html_dir") prevredirect="${page}-0" nextredirect="${page}-$(( lastview + 1 ))" diff --git a/tests/cli.sh b/tests/cli.sh index 2cc9d19..b58610c 100755 --- a/tests/cli.sh +++ b/tests/cli.sh @@ -1911,6 +1911,40 @@ test_integration_generates_album_outputs_and_cleans() { test::teardown } +test_render_view_redirects_uses_numeric_last_view() { + local dist_dir + local html_dir + local redirect_html + local view + + test::setup + dist_dir="$TEST_TMPDIR/dist" + html_dir='.' + mkdir -p "$dist_dir" + + # shellcheck source=/dev/null + source <(sed '$d' "$TEST_PHOTOALBUM") + + export DIST_DIR="$dist_dir" + export TEMPLATE_DIR="$TEST_REPO_ROOT/share/templates/default" + export PHOTOALBUM_OUTPUT_MODE=quiet + export MAXPREVIEWS=10 + + for view in 1 2 3 4 5 6 7 8 9 10; do + : > "$dist_dir/1-$view.html" + done + : > "$dist_dir/2-1.html" + touch -t 202606050000 "$dist_dir"/1-*.html "$dist_dir/2-1.html" + + render_view_redirects "$html_dir" + + test::assert_file_exists "$dist_dir/1-11.html" + redirect_html=$(<"$dist_dir/1-11.html") + test::assert_contains 'url=2-1.html' "$redirect_html" + test "$(<"$dist_dir/1-10.html")" = '' + test::teardown +} + test_generate_config_no_splash_keeps_index_redirect() { local config_file local fake_bin @@ -2771,6 +2805,9 @@ main() { '--generate creates output structure and --clean removes it' \ test_integration_generates_album_outputs_and_cleans test::run_case \ + 'view redirects use numeric last view' \ + test_render_view_redirects_uses_numeric_last_view + test::run_case \ '--generate SPLASH_PAGE=no keeps root index redirect' \ test_generate_config_no_splash_keeps_index_redirect test::run_case \ |
