tarballs.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env perl
  2. use Mojolicious::Lite;
  3. use Mojo::Cache;
  4. use Archive::Tar;
  5. use File::Basename;
  6. use Encode qw(decode_utf8);
  7. my $dir = "/home/alex/oddmuse.org/releases";
  8. my $cache = Mojo::Cache->new(max_keys => 50);
  9. get '/' => sub {
  10. my $c = shift;
  11. my @tarballs = sort map {
  12. my ($name, $path, $suffix) = fileparse($_, '.tar.gz');
  13. $name;
  14. } <$dir/*.tar.gz>;
  15. $c->render(template => 'index', tarballs => \@tarballs);
  16. } => 'main';
  17. get '/#tarball' => sub {
  18. my $c = shift;
  19. my $tarball = $c->param('tarball');
  20. my $files = $cache->get($tarball);
  21. if (not $files) {
  22. $c->app->log->info("Reading $tarball.tar.gz");
  23. my $tar = Archive::Tar->new;
  24. $tar->read("$dir/$tarball.tar.gz");
  25. my @files = sort grep /./, map {
  26. my @e = split('/', $_->name);
  27. $e[1];
  28. } $tar->get_files();
  29. $files = \@files;
  30. $cache->set($tarball => $files);
  31. }
  32. $c->render(template => 'release', tarball=> $tarball, files => $files);
  33. } => 'release';
  34. get '/#tarball/#file' => sub {
  35. my $c = shift;
  36. my $tarball = $c->param('tarball');
  37. my $file = $c->param('file');
  38. my $text = $cache->get("$tarball/$file");
  39. if (not $text) {
  40. $c->app->log->info("Reading $tarball/$file");
  41. my $tar = Archive::Tar->new;
  42. $tar->read("$dir/$tarball.tar.gz");
  43. $text = decode_utf8($tar->get_content("$tarball/$file"));
  44. $cache->set("$tarball/$file" => $text);
  45. }
  46. if ($text) {
  47. $c->render(template => 'file', format => 'txt', content => $text);
  48. } else {
  49. $c->render(template => 'missing');
  50. }
  51. } => 'file';
  52. app->start;
  53. __DATA__
  54. @@ index.html.ep
  55. % layout 'default';
  56. % title 'Oddmuse Releases';
  57. <h1>Oddmuse Releases</h1>
  58. <p>Welcome! This is where you get access to tarballs and files in released
  59. versions of Oddmuse.</p>
  60. <ul>
  61. % for my $tarball (@$tarballs) {
  62. <li>
  63. <a href="https://oddmuse.org/releases/<%= $tarball %>.tar.gz"><%= $tarball %>.tar.gz</a>
  64. (files for <%= link_to release => {tarball => $tarball} => begin %>\
  65. <%= $tarball =%><%= end %>)
  66. </li>
  67. % }
  68. </ul>
  69. @@ release.html.ep
  70. % layout 'default';
  71. % title 'Release';
  72. <h1>Files for <%= $tarball %></h1>
  73. <p>
  74. Back to the list of <%= link_to 'releases' => 'main' %>.
  75. Remember,
  76. %= link_to file => {file => 'wiki.pl'} => begin
  77. wiki.pl
  78. % end
  79. is the main script.
  80. <ul>
  81. % for my $file (@$files) {
  82. <li>
  83. %= link_to file => {file => $file} => begin
  84. %= $file
  85. % end
  86. % }
  87. </ul>
  88. @@ file.txt.ep
  89. %layout 'file';
  90. <%== $content %>
  91. @@ missing.html.ep
  92. % layout 'default';
  93. % title 'Missing File';
  94. <h1><%= $tarball %> is missing <%= $file %></h1>
  95. <p>
  96. You can go back to the list of <%= link_to 'releases' => 'main' %>,
  97. or you can try to find <%= $file %> in the
  98. <a href="https://raw.githubusercontent.com/kensanata/oddmuse/master/modules/<%= $file %>">unstable modules</a> section on GitHub.
  99. @@ layouts/default.html.ep
  100. <!DOCTYPE html>
  101. <html>
  102. <head>
  103. <title><%= title %></title>
  104. %= stylesheet '/tarballs.css'
  105. %= stylesheet begin
  106. body {
  107. padding: 1em;
  108. font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
  109. }
  110. % end
  111. <meta name="viewport" content="width=device-width">
  112. </head>
  113. <body>
  114. <%= content %>
  115. <hr>
  116. <p>
  117. <a href="https://oddmuse.org/">Oddmuse</a>&#x2003;
  118. <%= link_to 'Releases' => 'main' %>&#x2003;
  119. <a href="https://alexschroeder.ch/wiki/Contact">Alex Schroeder</a>
  120. </body>
  121. </html>