]> git.donarmstrong.com Git - catcam.git/blob - index.cgi
fix basename
[catcam.git] / index.cgi
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use CGI::Simple;
7 use File::Find;
8 use File::stat;
9 use File::Basename qw(dirname);
10 use POSIX;
11
12 my $q = CGI::Simple->new({num=>100});
13
14 my $cat_dir = dirname($ENV{SCRIPT_FILENAME});
15
16
17 my %pics;
18 find(sub {
19          if (/\.html$/) {
20              $pics{$_} = stat($_);
21          }
22       },
23      $cat_dir);
24
25 print $q->header(-status => 200);
26
27 print <<EOF;
28 <!DOCTYPE html>
29 <html>
30 <head>
31 <title>Cat Pictures!</title>
32 </head>
33 <body>
34 <header class="header"><span class="title">Cat Pictures!</span></header>
35
36 <section class="catpictures">
37 EOF
38
39 my $num = 0;
40 my $previous_date = '';
41 for my $pic (sort {$pics{$b}->ctime <=> $pics{$a}->ctime} keys %pics) {
42     my $this_date = POSIX::strftime('%Y-%m-%d',localtime($pics{$pic}->ctime()));
43     if ($previous_date ne $this_date){
44         print "<p>$this_date</p>\n";
45         $previous_date = $this_date;
46     }
47     my $basename = $pic;
48     $basename =~ s/\.html//;
49     print <<EOF;
50 <a href="$pic"><img src="${basename}.jpg" width=100 height=100 alt="$basename"></a>
51 EOF
52     $num++;
53     last if $num > 30;
54 }
55
56 print <<EOF;
57 </section>
58 <footer>
59 <span class="footer">A silly CGI script</span>
60 </footer>
61 </html>
62 EOF
63
64 sub error {
65     my ($q,$error,$text) = @_;
66     $text //= '';
67     print $q->header(-status => $error);
68     print "<h2>$error: $text</h2>";
69     exit 0;
70 }