| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 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::StaticFiles - serve static files via a Phoebe wiki | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Serving static files... Sometimes it's just easier. All the static files are | ||
| 26 | served from C</do/static>, without regard to wiki spaces. You need to define | ||
| 27 | routes that map a path to your filesystem. | ||
| 28 | |||
| 29 | package App::Phoebe::StaticFiles; | ||
| 30 | our %routes = ( | ||
| 31 | "zürich" => "/home/alex/Pictures/2020/Zürich", | ||
| 32 | "amaryllis" => "/home/alex/Pictures/2021/Amaryllis", ); | ||
| 33 | use App::Phoebe::StaticFiles; | ||
| 34 | |||
| 35 | The setup does not allow recursive traversal of the file system. | ||
| 36 | |||
| 37 | You still need to add a link to C</do/static> somewhere in your wiki. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | package App::Phoebe::StaticFiles; | ||
| 42 | require Exporter; | ||
| 43 | our @ISA = qw(Exporter); | ||
| 44 | our @EXPORT_OK = qw(%routes mime_type); | ||
| 45 | use App::Phoebe qw(@extensions $log host_regex port success result); | ||
| 46 | use Encode qw(encode_utf8 decode_utf8); | ||
| 47 | use URI::Escape; | ||
| 48 | use Mojo::File; | ||
| 49 | |||
| 50 | # add a code reference to the list of extensions | ||
| 51 | push(@extensions, \&static_routes); | ||
| 52 | |||
| 53 | # a hash mapping routes to the static directories to serve | ||
| 54 | our %routes; | ||
| 55 | |||
| 56 | sub static_routes { | ||
| 57 | my ($stream, $url) = @_; | ||
| 58 | my $host = host_regex(); | ||
| 59 | my $port = port($stream); | ||
| 60 | if ($url =~ m!^gemini://($host)(?::$port)?/do/static/?$!) { | ||
| 61 | $log->debug("Serving the list of static routes"); | ||
| 62 | success($stream); | ||
| 63 | for my $route (sort keys %routes) { | ||
| 64 | $stream->write("=> /do/static/" . uri_escape_utf8($route) . " " . encode_utf8($route) . "\n"); | ||
| 65 | } | ||
| 66 | return 1; | ||
| 67 | } elsif ($url =~ m!^gemini://($host)(?::$port)?/do/static/([^/]+)/?$!) { | ||
| 68 | my $route = decode_utf8(uri_unescape($2)); | ||
| 69 | my $dir = $routes{$route}; | ||
| 70 | $log->debug("Serving list of files at $route, reading $dir"); | ||
| 71 | if ($dir) { | ||
| 72 | success($stream); | ||
| 73 | my @files = map { $_->basename } Mojo::File->new($dir)->list->each; | ||
| 74 | for my $file (sort map { decode_utf8($_) } @files) { | ||
| 75 | $stream->write("=> /do/static/" . uri_escape_utf8($route) . "/" . uri_escape_utf8($file) | ||
| 76 | . " " . encode_utf8($file) . "\n"); | ||
| 77 | } | ||
| 78 | } else { | ||
| 79 | result($stream, "40", "Unknown route: " . encode_utf8($route)); | ||
| 80 | } | ||
| 81 | return 1; | ||
| 82 | } elsif ($url =~ m!^gemini://($host)(?::$port)?/do/static/([^/]+)/([^.].*)$!i) { | ||
| 83 | my $route = decode_utf8(uri_unescape($2)); | ||
| 84 | my $file = decode_utf8(uri_unescape($3)); | ||
| 85 | $log->debug("Serving $route/$file"); | ||
| 86 | my $dir = $routes{$route}; | ||
| 87 | return result($stream, "40", "Unknown route: " . encode_utf8($route)) | ||
| 88 | unless $dir; | ||
| 89 | my $path = Mojo::File->new($dir, $file); | ||
| 90 | return result($stream, "40", "Unknown file: " . encode_utf8($file)) | ||
| 91 | unless -f $path and is_in(Mojo::File->new($dir), $path); | ||
| 92 | success($stream, mime_type($$path)); | ||
| 93 | $stream->write($path->slurp); | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | # being paranoid about directory traversal | ||
| 100 | sub is_in { | ||
| 101 | my ($parent, $child) = map { $_->to_abs } @_; | ||
| 102 | return substr($child, 0, length($parent)) eq $parent; | ||
| 103 | } | ||
| 104 | |||
| 105 | # cheap MIME type guessing; alternatively, use File::MimeInfo | ||
| 106 | sub mime_type { | ||
| 107 | $_ = shift; | ||
| 108 | return 'text/gemini' if /\.gmi$/i; | ||
| 109 | return 'text/plain' if /\.te?xt$/i; | ||
| 110 | return 'text/markdown' if /\.md$/i; | ||
| 111 | return 'text/html' if /\.html?$/i; | ||
| 112 | return 'image/png' if /\.png$/i; | ||
| 113 | return 'image/jpeg' if /\.jpe?g$/i; | ||
| 114 | return 'image/gif' if /\.gif$/i; | ||
| 115 | return 'text/plain'; # or application/octet-stream? | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; |