Phoebe/Galleries.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 143 CC 15 SCAR 50.6
CC
15
Cov
0%
galleries
240.0
gallery_title
2.0
gallery_print_link
2.0
CRAP 157.2
1 # -*- mode: perl -*-
2 # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org>
3  
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
7 # later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 App::Phoebe::Galleries - serving sitelen mute image galleries via Gemini
22  
23 =head1 DESCRIPTION
24  
25 This extension only makes sense if you have image galleries created by
26 F<fgallery> or L<App::sitelenmute>.
27  
28 If you do, you can serve them via Gemini. You have to configure two things: in
29 which directory the galleries are, and under what host they are served (the code
30 assumes that when you are virtual hosting multiple domains, only one of them has
31 the galleries).
32  
33 C<$galleries_dir> is the directory where the galleries are. This assumes that
34 your galleries are all in one directory. For example, under
35 F</home/alex/alexschroeder.ch/gallery> you’d find F<2016-altstetten> and many
36 others like it. Under F<2016-altstetten> you’d find the F<data.json> and
37 F<index.html> files (both of which get parsed), and the various subdirectories.
38  
39 In your F<config> file:
40  
41 package App::Phoebe::Galleries;
42 our $galleries_dir = "/home/alex/alexschroeder.ch/gallery";
43 our $galleries_host = "alexschroeder.ch";
44 use App::Phoebe::Galleries;
45  
46 =cut
47  
48 package App::Phoebe::Galleries;
49 use App::Phoebe qw(@extensions $log port success result print_link);
50 use File::Slurper qw(read_dir read_binary read_text);
51 use Encode qw(decode_utf8 encode_utf8);
52 use Modern::Perl;
53 use Mojo::JSON qw(decode_json encode_json);
54 use Mojo::Util qw(url_unescape);
55  
56 # galleries
57  
58 push(@extensions, \&galleries);
59  
60 our $galleries_dir = "/home/alex/alexschroeder.ch/gallery";
61 our $galleries_host = "alexschroeder.ch";
62  
63 sub gallery_title {
64 my $dir = shift;
65 $dir =~ s/-/ /g;
66 return join(' ', map {ucfirst} split(' ', $dir));
67 }
68  
69 sub galleries {
70 my $stream = shift;
71 my $url = shift;
72 my $host = $galleries_host;
73 my $port = port($stream);
74 if ($url =~ m!^gemini://$host(?::$port)?/do/gallery$!) {
75 success($stream);
76 $log->info("Serving galleries");
77 $stream->write("# Galleries\n");
78 for my $dir (
79 sort {
80 my ($year_a, $title_a) = split(/-/, $a, 2);
81 my ($year_b, $title_b) = split(/-/, $b, 2);
82 return ($year_b <=> $year_a || $title_a cmp $title_b);
83 } grep {
84 -d "$galleries_dir/$_"
85 } read_dir($galleries_dir)) {
86 gallery_print_link($stream, $host, gallery_title($dir), "do/gallery/$dir");
87 };
88 return 1;
89 } elsif (my ($dir) = $url =~ m!^gemini://$host(?::$port)?/do/gallery/([^/?]*)$!) {
90 if (not -d "$galleries_dir/$dir") {
91 result($stream, "40", "This is not actuall a gallery");
92 return 1;
93 }
94 if (not -r "$galleries_dir/$dir/data.json") {
95 result($stream, "40", "This gallery does not contain a data.json file like the one created by sitelen-mute or fgallery");
96 return 1;
97 }
98 my $bytes = read_binary("$galleries_dir/$dir/data.json");
99 if (not $bytes) {
100 result($stream, "40", "Cannot read the data.json file in this gallery");
101 return 1;
102 }
103 my $data;
104 eval { $data = decode_json $bytes };
105 $log->error("decode_json: $@") if $@;
106 if ($@ or not %$data) {
107 result($stream, "40", "Cannot decode the data.json file in this gallery");
108 return 1;
109 }
110 success($stream);
111 $log->info("Serving gallery $dir");
112 if (-r "$galleries_dir/$dir/index.html") {
113 my $dom = Mojo::DOM->new(read_text("$galleries_dir/$dir/index.html"));
114 $log->info("Parsed index.html");
115 my $title = $dom->at('*[itemprop="name"]');
116 $title = $title ? $title->text : gallery_title($dir);
117 $stream->write(encode_utf8 "# $title\n");
118 my $description = $dom->at('*[itemprop="description"]');
119 $stream->write(encode_utf8 $description->text . "\n") if $description;
120 $stream->write("## Images\n");
121 } else {
122 $stream->write(encode_utf8 "# " . gallery_title($dir) . "\n");
123 }
124 for my $image (@{$data->{data}}) {
125 $stream->write("\n");
126 $stream->write(encode_utf8 join("\n", grep /\S/, @{$image->{caption}}) . "\n") if $image->{caption};
127 gallery_print_link($stream, $host, "Thumbnail", "do/gallery/$dir/" . $image->{thumb}->[0]);
128 gallery_print_link($stream, $host, "Image", "do/gallery/$dir/" . $image->{img}->[0]);
129 }
130 return 1;
131 } elsif (my ($file, $extension) = $url =~ m!^gemini://$host(?::$port)?/do/gallery/([^/?]*/(?:thumbs|imgs)/[^/?]*\.(jpe?g|png))$!i) {
132 $file = url_unescape $file; # do not decode UTF-8
133 my $name = decode_utf8($file);
134 if (not -r "$galleries_dir/$file") {
135 $stream->write(encode_utf8 "40 Cannot read $name\r\n");
136 } else {
137 success($stream, $extension =~ /^png$/i ? "image/png" : "image/jpeg");
138 $log->info("Serving image $name");
139 $stream->write(read_binary("$galleries_dir/$file"));
140 }
141 return 1;
142 }
143 return;
144 }
145  
146 sub gallery_print_link {
147 my $stream = shift;
148 my $host = shift;
149 my $title = shift;
150 my $id = shift;
151 return print_link($stream, $host, undef, $title, $id);
152 }