%### -*-html-*-
%###
%### Main output for Trivial Gallery.
%###
%### (c) 2021 Mark Wooding
%###
%
%###----- Licensing notice --------------------------------------------------
%###
%### This file is part of Trivial Gallery.
%###
%### Trivial Gallery is free software: you can redistribute it and/or modify
%### it under the terms of the GNU Affero General Public License as
%### published by the Free Software Foundation; either version 3 of the
%### License, or (at your option) any later version.
%###
%### Trivial Gallery is distributed in the hope that it will be useful, but
%### WITHOUT ANY WARRANTY; without even the implied warranty of
%### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%### Affero General Public License for more details.
%###
%### You should have received a copy of the GNU Affero General Public
%### License along with Trivial Gallery. If not, see
%### .
%
%###-------------------------------------------------------------------------
<%def .html>\
% $r->content_type("text/html; charset=\"utf-8\"");
">
<% $head %>\
<% $title %>
<% $m->content %>
\
%
<%args>
$title
$head => ""
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .not-found>\
<&| .html, title => "Not found" &>
Not found
Failed to find ‘<% $path |h %>’.
&>
% return 404;
%
<%args>
$path
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .contact>\
<%perl>
unless ($r->path_info =~ m!/$!)
{ $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
my $real = join_paths $IMGROOT, $path;
my $url = join_paths $SCRIPTURL, $path;
my ($dd, $ff, $ii) = listdir $real;
my $links = "";
my $uplink;
if ($path eq "" || $path eq "/") { $uplink = undef; }
else {
($uplink = $path) =~ s![^/]*/$!!;
$links .= sprintf " \n",
urlencode "$SCRIPTURL/$uplink";
}
(my $nosl = $path) =~ s!/$!!;
my $size = "medthumb";
my %tn;
for my $f (@$ff)
{ $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
for my $d (@$dd) {
my $p = join_paths $path, $d->name;
DIR: for (;;) {
my ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
if (defined $iii) {
my $index = join_paths $p, $iii->name;
$tn{$d} = TrivGal::Image->new($index)->scale($size);
last DIR;
}
if (!@$ddd) { $tn{$d} = undef; last DIR; }
$p = join_paths $p, $ddd->[0]->name;
}
}
%perl>
%
<&| .html, title =>
"Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
head => $links &>
<& .breadcrumbs, what => "Folder", path => $path &>
%
% my $note = contents "$IMGROOT/$path/.tgal-note.html";
% if (defined $note) {
<% $note %>
% }
%
% if (@$dd) {
Subfolders
% for my $d (@$dd) {
<& .thumbnail, target => $d->name, comment => $d->comment,
tn => $tn{$d}, size => $size,
caption => $m->interp->apply_escapes($d->name, "h") &>\
% }
% }
%
% if (@$ff) {
Images
% for my $f (@$ff) {
<& .thumbnail, target => $f->name, comment => $f->comment,
tn => $tn{$f}, size => $size,
caption => $m->interp->apply_escapes($f->name, "h") &>\
% }
% }
%
<& .footer, path => $path &>
&>
%
<%args>
$path
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .image>\
<%perl>
my ($dir, $base, $ext) = split_path $path;
my $real = join_paths $IMGROOT, $path;
my $img = TrivGal::Image->new($path);
if (defined $scale) { $m->redirect($img->scale($scale)); }
my $url = join_paths $IMGURL, $path;
my $realdir = join_paths $IMGROOT, $dir;
my $urldir = join_paths $SCRIPTURL, $dir;
my ($dd, $ff, $ii) = listdir $realdir;
my $vw = $img->scale("view");
my $fi = undef;
FILE: for (my $i = 0; $i < @$ff; $i++)
{ if ($ff->[$i]->name eq "$base$ext") { $fi = $i; last FILE; } }
defined $fi or die "image not found in its folder?";
my $this = $ff->[$fi];
my %link;
$link{up} = "";
if ($fi != 0) {
$link{first} = $ff->[0]->name;
$link{prev} = $ff->[$fi - 1]->name;
}
if ($fi != @$ff - 1) {
$link{last} = $ff->[-1]->name;
$link{next} = $ff->[$fi + 1]->name;
}
my $links = "";
my $pre =
urlencode join_paths $SCRIPTURL, $dir;
for my $rel (qw{up first prev next last}) {
exists $link{$rel} and
$links .= sprintf " \n",
$rel, urlencode "$pre/$link{$rel}";
}
%perl>
%
<&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
head => $links &>
<& .breadcrumbs, what => "Image", path => $path &>
% if ($this->comment) {
% }
%
% if ($link{prev}) {
% }
% if ($link{next}) {
% }
%
% my %img = map { $_ => TrivGal::Image->new($dir . "/" . $_->name) } @$ff;
% for my $size (qw{smallthumb medthumb bigthumb}) {
% for my $f (@$ff) {
<& .thumbnail, target => $f->name,
img => $img{$f}->scale($size), size => $size,
caption => $m->interp->apply_escapes($f->name, "h"),
focus => $f eq $this &>\
% }
% }
<& .footer, path => $dir &>
&>
%
<%args>
$path
$scale => undef
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .breadcrumbs>\
% $path =~ s!/$!!;
% my @p = split m!/!, $path;
% my $pp = "";
% my $prev = undef;
<% $what %> \
% if (!@p) {
[top]
% } else {
[top] / \
% STEP: for my $p (@p) {
% if (defined $prev) {
% $pp .= "$prev/";
\
<% $prev %> / \
% }
% $prev = $p;
% }
<% $prev %>\
% }
<%args>
$what
$path
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .thumbnail>\
% $tn //= "$STATICURL/folder.svg";
% if ($focus) {
<% $caption %>
% } else {
<% $caption %>
% if (defined $comment) {
% }
% }
%
<%args>
$target
$tn
$size
$caption
$comment => undef
$focus => 0
%args>
%def>
%
%###-------------------------------------------------------------------------
<%def .footer>\
<%perl>
%perl>
<%args>
$path
%args>
%def>
%
%###-------------------------------------------------------------------------
<%once>
use autodie;
use File::stat;
use TrivGal;
%once>
%
<%init>
TrivGal->init;
my $path = $m->dhandler_arg;
my $st = stat "$IMGROOT/$path";
my $comp;
if (!$st) { $comp = ".not-found"; }
elsif (-d $st) { $comp = ".contact"; }
elsif (-f $st) { $comp = ".image"; }
else { $comp = ".not-found"; }
$r->header_out("X-AGPL-Source" => $SRCURL);
$m->comp($comp, path => $path, %ARGS);
%init>
%
%###----- That's all, folks -------------------------------------------------
<% $this->comment %>