Code > DiskMAGs dhandler
Shared
block
<%shared>
my ($disk, @path) = split m|/|, $m->dhandler_arg;
my ($disk_href, $object_href, $special);
</%shared>
Once
block
<%once>
use HTML::Entities;
use List::Util qw(sum);
use File::MMagic;
</%once>
Init
block
<%init>
$m->comp('SELF:validate-request');
$r->content_type('text/html');
if (defined $special) {
$m->comp("SELF:$special");
} elsif (!defined $disk) {
$m->comp('SELF:show-disk-index', %ARGS);
} elsif (!scalar @path || $object_href->{dir}) {
if ($r->uri !~ m|/$|) {
$m->redirect($r->uri . '/');
} else {
$m->comp('SELF:show-dir', %ARGS);
}
} else {
$m->comp('SELF:handle-file', %ARGS);
}
</%init>
Method:
validate-request
<%method validate-request>
<%init>
return if !defined $disk;
$disk_href = $dbh->selectrow_hashref("SELECT id, uri_name, volume,
COALESCE(title, 'MAG Disk') AS title,
COALESCE(TO_CHAR(issue_date, 'YYYY-MM'), '19xx') AS issue_date,
COALESCE(TO_CHAR(issue_date, 'Mon YYYY'), '19xx') AS issue_date_text
FROM disks WHERE uri_name = ?", undef, $disk);
$m->clear_and_abort(404) if !defined $disk_href;
if (scalar @path == 1) {
if ($path[0] eq "$disk_href->{uri_name}.png")
{ $special = 'send-disk-scan'; }
elsif ($path[0] eq "$disk_href->{uri_name}.adf")
{ $special = 'send-disk-dump'; }
}
return if defined $special;
my $parent_id;
for my $object_name (@path) {
my $parent_clause = defined $parent_id ? "= $parent_id" : 'IS NULL';
$object_href = $dbh->selectrow_hashref("
SELECT id, name, dir, type, mimetype FROM filesystem
WHERE disk_id = ? AND parent_id $parent_clause AND name = ?
", undef, ($disk_href->{id}, $object_name));
$m->clear_and_abort(404) if !defined $object_href;
$parent_id = $object_href->{id};
}
</%init>
</%method>
Method:
navigation
<%method navigation>
<%init>
my $years_aref = $dbh->selectall_arrayref("
SELECT DISTINCT DATE_PART('year', issue_date) AS year, COUNT(\*)
FROM disks GROUP BY year ORDER BY year");
my $total_disks = sum map { $_->[1] } @$years_aref;
</%init>
<div><a class="current-page" href="/diskmags/">DiskMAG Index</a></div>
<ul>
<li><a href="/diskmags/">All</a> (<% $total_disks %> disks)</li>
% for my $year (@$years_aref) {
<li><a href="/diskmags/?year=<% defined $year->[0] ? $year->[0] : 'unknown' %>">
<% defined $year->[0] ? $year->[0] : 'Unknown' %></a>
(<% $year->[1] %> disks)</li>
% }
</ul>
</%method>
Method:
page-head
<%method page-head>
<%init>
my $pagehead = 'All DiskMAGs';
if (defined (my $year = $m->request_args->{year})) {
if ($year =~ /^(19[89]\d|unknown)$/) {
$pagehead = ($year eq 'unknown' ? 'Undated' : $year) . ' DiskMAGs';
}
}
if (defined $disk) {
my $dir_depth = scalar @path;
$dir_depth-- if !$object_href->{dir};
$pagehead = '<a href="' . ('../' x $dir_depth || './')
. '">' . encode_entities($disk_href->{title})
. ' (' . $disk_href->{issue_date_text} . ')</a> : ';
for (my $i = 0; $i <= $dir_depth; $i++) {
my $uri = '../' x ($dir_depth - $i - 1) || './';
if ($i == $#path && !$object_href->{dir})
{ $uri = $m->interp->apply_escapes($path[$i], 'u'); }
$pagehead .= '<a href="' . $uri . '">' . encode_entities($path[$i])
. '</a> ';
$pagehead .= '/ ' if $i != $dir_depth;
}
}
</%init>
<% $pagehead %>
</%method>
Method:
title
<%method title>
<%init>
my $title = $m->scomp('SELF:page-head');
$title =~ s{</?a(>| [^>]*?>)}{}g;
$title =~ s{ (:|/) }{$1}g;
</%init>
<% $title %>
</%method>
Method:
show-disk-index
<%method show-disk-index>
<%args>
$year => undef
</%args>
<%init>
$year = undef if $year !~ /^(19[89]\d|unknown)$/;
my $where_clause;
if ($year eq 'unknown')
{ $where_clause = 'WHERE issue_date IS NULL'; }
elsif (defined $year)
{ $where_clause = "WHERE DATE_PART('year', issue_date) = $year"; }
my $disks_aref = $dbh->selectall_arrayref("
SELECT uri_name, date_exact, COALESCE(title, 'MAG Disk') AS title,
TO_CHAR(issue_date, 'Month YYYY') AS issue_date_text
FROM disks $where_clause ORDER BY issue_date, uri_name
", { Slice => {} });
</%init>
<p><a href="mag-disks.dat">Download clrmamepro DAT for all DiskMAGs</a></p>
<table class="alternating-list">
<tr>
<th></th>
<th>Date</th>
<th>Name</th>
</tr>
% my $i = 1;
% for my $disk_href (@$disks_aref) {
% my $cell_class = 'class="mark"' if $i++ % 2;
<tr>
<td <% $cell_class ? 'class="mark icon"' : 'class="icon"' %>>
<img src="/images/floppy.png" width="16" height="16" alt="" />
</td>
<td <% $cell_class %>>
% if (defined $disk_href->{issue_date_text}) {
<% $disk_href->{issue_date_text} %>
<% $disk_href->{date_exact} ? undef : '(?)' %>
% } else {
Unknown
% }
</td>
<td <% $cell_class %>>
<a href="<% $disk_href->{uri_name} | u %>/"><%
$disk_href->{title} | h %></a>
</td>
</tr>
% }
</table>
</%method>
Method:
show-dir
<%method show-dir>
<%args>
$icons => undef # Show Amiga icons (.info files?)
</%args>
<%init>
$icons = 1 if $icons;
my $dir_id = $object_href->{id};
my $parent_clause = defined $dir_id ? "= $dir_id" : 'IS NULL';
my $contents_aref = $dbh->selectall_arrayref("
SELECT id, disk_id, parent_id, name, dir, type, longtype,
LENGTH(contents) AS size, TO_CHAR(time_stamp, 'YYYY-MM-DD') AS time_stamp
FROM filesystem WHERE disk_id = ? AND parent_id $parent_clause
ORDER BY dir DESC, UPPER(name)
", { Slice => {} }, $disk_href->{id});
if (!$icons) {
$contents_aref = [ grep { $_->{type} ne 'icon' } @$contents_aref ];
}
my $images_aref = $dbh->selectall_arrayref("
SELECT fs.name, i.th_width, i.th_height, i.conversion
FROM filesystem AS fs, images AS i WHERE fs.id = i.id
AND fs.disk_id = ? AND parent_id $parent_clause ORDER BY fs.name
", { Slice => {} }, $disk_href->{id});
my $texts_aref = $dbh->selectall_arrayref("
SELECT name, contents FROM filesystem WHERE disk_id = ?
AND parent_id $parent_clause AND type = 'text' AND
(name ILIKE '%read%me%' OR name ILIKE '%mag%'
OR name ILIKE '%content%' OR name ILIKE '%prezcli%'
OR name ILIKE '%poster%' OR name ILIKE '%library%'
OR name ILIKE '%bbs%')
AND LENGTH(contents) < 10240 ORDER BY name
", { Slice => {} }, $disk_href->{id});
</%init>
% # Show the image thumbnails, if there are any.
% if (scalar @$images_aref) {
<div id="side-thumb-list">
<ul>
% for my $image (@$images_aref) {
<li>
<a class="image-link" href="./<% $image->{name} | u %>">
<% $m->comp('SELF:show-image-tag', name => $image->{name},
width => $image->{th_width}, height => $image->{th_height},
conversion => $image->{conversion}, thumb => 1) %>
</a><br />
<a href="./<% $image->{name} | u %>"><% $image->{name} | h %></a>
</li>
% }
</ul>
</div>
% }
<form method="get" action="<% $r->uri %>">
<p>
Options:
<input type="checkbox" name="icons" id="icons" value="1"
<% $icons ? 'checked="checked"' : '' %>
onclick="this.form.submit();" />
<label for="icons">Show Icons</label>
% # If this is the root directory, show links for the disk scan and dump.
% if (!defined $dir_id) {
· <a href="<% $disk_href->{uri_name} %>.png">Disk Scan</a>
· <a href="<% $disk_href->{uri_name} %>.adf">Download ADF</a>
% }
</p>
</form>
<table class="alternating-list">
<tr>
<th></th>
<th>Name</th>
<th>Size</th>
<th>Date</th>
<th>Type</th>
</tr>
<tr>
<td class="mark icon">
<img src="/images/directory.png" width="16" height="16" alt="" />
</td>
<td class="mark"><a href="../<% $icons ? '?icons=1' : '' %>">..</a>/</td>
<td class="mark"></td>
<td class="mark"></td>
<td class="mark"></td>
</tr>
% my $i = 0;
% for my $entry (@$contents_aref) {
% my $cell_class = 'class="mark"' if $i++ % 2;
% # Add slashes to directories; preserve state of "show icons" toggle.
% my $href_trailer = $entry->{dir} ? '/' : '';
% $href_trailer .= '?icons=1' if ($entry->{dir} && $icons);
<tr>
<td <% $cell_class ? 'class="mark icon"' : 'class="icon"' %>>
% if ($entry->{type} =~ /^(text|image)$/) {
<img src="/images/<% $entry->{type} %>.png" width="16" height="16" alt="" />
% } elsif ($entry->{dir}) {
<img src="/images/directory.png" width="16" height="16" alt="" />
% }
</td>
<td <% $cell_class %>>
<a href="<% $entry->{name} | u %><% $href_trailer %>">
<% $entry->{name} | h %></a><% $entry->{dir} ? '/' : '' %>
</td>
<td <% $cell_class %>><% $entry->{size} %></td>
<td <% $cell_class %>><% $entry->{time_stamp} %></td>
<td <% $cell_class %>>
% if ($entry->{type} =~ /^(text|image)$/) {
<% ucfirst($entry->{type}) %>
[<a href="<% $entry->{name} | u %>?noconvert=1">Original</a>]
% }
</td>
</tr>
% }
</table>
% # Show the retrieved texts, if there are any.
% for my $file (@$texts_aref) {
<h2><% $file->{name} | h %></h2>
<pre><% encode_entities($file->{contents}) %></pre>
% }
</%method>
Method:
handle-file
<%method handle-file>
<%args>
$noconvert => undef # Send the original file instead of the converted version?
</%args>
<%init>
$noconvert = 1 if $noconvert;
if ($object_href->{type} =~ /^(text|image)$/ && !$noconvert) {
$m->comp("SELF:handle-file-$object_href->{type}", %ARGS);
return;
}
$m->comp('SELF:send-file');
</%init>
</%method>
Method:
handle-file-text
<%method handle-file-text>
<%init>
my ($contents) = $dbh->selectrow_array('SELECT contents FROM filesystem
WHERE id = ?', undef, $object_href->{id});
if ($object_href->{name} =~ /\.guide$/i)
{ $m->comp('SELF:parse-amigaguide', contents => $contents); }
else
{ print '<pre>' . encode_entities($contents) . '</pre>'; }
</%init>
</%method>
Method:
handle-file-image
<%method handle-file-image>
<%args>
$display => undef # Send the converted image instead of displaying it inline?
$thumb => undef # Show the thumbnail version of the image?
$autoaspect => 1 # Automatically correct Amiga aspect ratio?
$aspect => undef # Correct Amiga aspect ratio (if autoaspect is false)?
</%args>
<%init>
for my $ref (\$display, \$thumb, \$autoaspect, \$aspect) {
$$ref = 1 if $$ref;
}
my $table_columns = $thumb ? 'thumbnail AS contents, th_width AS width, th_height AS height'
: 'contents, width, height';
my $image_href = $dbh->selectrow_hashref("SELECT $table_columns, conversion
FROM images WHERE id = ?", undef, $object_href->{id});
if (!$display) {
$m->comp('SELF:show-image-page', %$image_href,
name => $object_href->{name}, thumb => $thumb,
autoaspect => $autoaspect, aspect => $aspect);
return;
}
$m->comp('SELF:send-file', contents => $image_href->{contents});
</%init>
</%method>
Method:
parse-amigaguide
<%method parse-amigaguide>
<%args>
$contents
</%args>
<%init>
$contents = encode_entities($contents);
$contents =~ s/"/"/g;
$contents =~ s|\@\{"(.+?)",?\s+link\s+"?(.+?)"?\}|<a href="#$2">$1</a>|gi;
$contents =~ s|^\@node\s+"?(.+?)"?(?:\s+"(.+)")?$|<span id=\"$1\">$2</span>|gim;
$contents =~ s|^\@toc\s+"?(.+?)"?$|<a href="#$1">Table of Contents</a>|gim;
$contents =~ s|^\@database.*$||gim;
$contents =~ s|\s+\@endnode\s*|\n\n<hr />\n|gis;
$contents =~ s|\@\{b\}|<strong>|gi; # bold on
$contents =~ s|\@\{ub\}|</strong>|gi; # bold off
$contents =~ s|\@\{i\}|<em>|gi; # italics on
$contents =~ s|\@\{ui\}|</em>|gi; # italics off
$contents =~ s|\@\{u\}|<span style="text-decoration: underline;">|gi; # underline on
$contents =~ s|\@\{uu\}|</span>|gi; # underline off
</%init>
<pre class="amigaguide"><% $contents %></pre>
</%method>
Method:
show-image-tag
<%method show-image-tag>
<%args>
$name
$path => './'
$width => undef
$height => undef
$conversion => undef # Is this image a converstion of an Amiga format?
$thumb => undef # Show the thumbnail version?
$autoaspect => 1 # Automatically correct Amiga image aspect ratio?
$aspect => undef # If autoaspect is false, correct aspect ratio?
</%args>
<%init>
if ($conversion && defined $width && defined $height) {
if ( $autoaspect && $height > $width && int($height / 2) <= $width ) {
$aspect = 1;
}
if ($aspect) {
$height = int($height / 2 * 1.2);
}
}
$path = join '/', map { $m->interp->apply_escapes($_, 'u') }
split /\//, $path;
</%init>
<img alt="<% $name | h %>"
<% defined $width ? qq{width="$width"} : '' %>
<% defined $height ? qq{height="$height"} : '' %>
src="<% $path %>/<% $name | u %>?display=1<% $thumb ? '&thumb=1' : '' %>"
/>
</%method>
Method:
show-image-page
<%method show-image-page>
<%args>
$name
$width => undef
$height => undef
$conversion => undef # Is this image a converstion of an Amiga format?
$thumb => undef # Show the thumbnail version?
$autoaspect => 1 # Automatically correct Amiga image aspect ratio?
$aspect => undef # If autoaspect is false, correct aspect ratio?
</%args>
<%init>
if ( $conversion && defined $width && defined $height &&
$autoaspect && $height > $width && int($height / 2) <= $width ) {
$aspect = 1;
}
</%init>
% if ($conversion) {
<form method="get" action="<% $r->uri %>">
<p>
Options:
<input type="hidden" name="autoaspect" value="0" />
<input type="checkbox" name="aspect" id="aspect" value="1"
<% $aspect ? 'checked="checked"' : '' %>
onclick="this.form.submit();" />
<label for="aspect">Correct Amiga aspect ratio</label>
</p>
</form>
% }
<div class="center">
<% $m->comp('SELF:show-image-tag', name => $name, width => $width,
height => $height, conversion => $conversion, thumb => $thumb,
autoaspect => $autoaspect, aspect => $aspect) %>
</div>
</%method>
Method:
send-file
<%method send-file>
<%args>
$contents => undef
$mimetype => undef
$filename => undef
</%args>
<%init>
if (!defined $contents) {
if ($object_href->{dir}) {
die "send-file can't handle a directory";
}
($contents) = $dbh->selectrow_array('SELECT contents FROM filesystem
WHERE id = ?', undef, $object_href->{id});
($mimetype, $filename) = ($object_href->{mimetype}, $object_href->{name});
}
if (!defined $mimetype) {
$mimetype = File::MMagic->new->checktype_contents($contents);
}
if (!defined $filename) {
$filename = $object_href->{name};
}
$m->clear_buffer;
$r->err_headers_out->{'Content-Disposition'} = qq{filename="$filename"};
$r->err_headers_out->{'Content-Length'} = length $contents;
$r->err_headers_out->{'Cache-Control'} = 'max-age=3600, must-revalidate';
$r->content_type($mimetype);
$m->print($contents);
$m->abort;
</%init>
</%method>
Method:
send-disk-scan
<%method send-disk-scan>
<%init>
my ($contents) = $dbh->selectrow_array('SELECT image FROM disks
WHERE id = ?', undef, $disk_href->{id});
$m->comp('SELF:send-file', contents => $contents,
filename => "$disk_href->{uri_name}.png");
</%init>
</%method>
Method:
send-disk-dump
<%method send-disk-dump>
<%init>
my $tosec_name = $m->comp('SELF:make-tosec-name');
my ($contents) = $dbh->selectrow_array('SELECT adf FROM disks
WHERE id = ?', undef, $disk_href->{id});
$m->comp('SELF:send-file', contents => $contents,
mimetype => 'application/octet-stream', filename => $tosec_name);
</%init>
</%method>
Method:
make-tosec-name
<%method make-tosec-name>
<%init>
my $tosec_name = 'MAG ' if $disk_href->{title} !~ /^MAG/;
$tosec_name .= "$disk_href->{title} ($disk_href->{issue_date})"
. '(Memphis Amiga Group)';
$tosec_name .= '[!]' if $disk_href->{good_dump};
$tosec_name .= '.adf';
return $tosec_name;
</%init>
</%method>