Use libmagic for better output MIME detection

This commit is contained in:
Marco Rebhan 2022-08-04 21:18:55 +02:00
parent e2756042b8
commit a58e2f1a64
No known key found for this signature in database
GPG key ID: C8D0FBE381A29D36
2 changed files with 8 additions and 32 deletions

View file

@ -503,6 +503,7 @@
DigestSHA1 DigestSHA1
EmailMIME EmailMIME
EmailSender EmailSender
FileLibMagic
FileSlurper FileSlurper
FileWhich FileWhich
final.nix.perl-bindings final.nix.perl-bindings

View file

@ -7,15 +7,16 @@ use base 'Hydra::Base::Controller::NixChannel';
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Hydra::Helper::CatalystUtils; use Hydra::Helper::CatalystUtils;
use File::Basename; use File::Basename;
use File::LibMagic;
use File::stat; use File::stat;
use Data::Dump qw(dump); use Data::Dump qw(dump);
use Nix::Store; use Nix::Store;
use Nix::Config; use Nix::Config;
use List::SomeUtils qw(all); use List::SomeUtils qw(all);
use Encode; use Encode;
use MIME::Types;
use JSON::PP; use JSON::PP;
use feature 'state';
sub buildChain :Chained('/') :PathPart('build') :CaptureArgs(1) { sub buildChain :Chained('/') :PathPart('build') :CaptureArgs(1) {
my ($self, $c, $id) = @_; my ($self, $c, $id) = @_;
@ -236,14 +237,10 @@ sub serveFile {
$c->stash->{'plain'} = { data => grab(cmd => ["nix", "--experimental-features", "nix-command", $c->stash->{'plain'} = { data => grab(cmd => ["nix", "--experimental-features", "nix-command",
"store", "cat", "--store", getStoreUri(), "$path"]) }; "store", "cat", "--store", getStoreUri(), "$path"]) };
# Detect MIME type. Borrowed from Catalyst::Plugin::Static::Simple. # Detect MIME type.
my $type = "text/plain"; state $magic = File::LibMagic->new(follow_symlinks => 1);
if ($path =~ /.*\.(\S{1,})$/xms) { my $info = $magic->info_from_filename($path);
my $ext = $1; my $type = $info->{mime_with_encoding};
my $mimeTypes = MIME::Types->new(only_complete => 1);
my $t = $mimeTypes->mimeTypeOf($ext);
$type = ref $t ? $t->type : $t if $t;
}
$c->response->content_type($type); $c->response->content_type($type);
$c->forward('Hydra::View::Plain'); $c->forward('Hydra::View::Plain');
} }
@ -288,29 +285,7 @@ sub download : Chained('buildChain') PathPart {
my $path = $product->path; my $path = $product->path;
$path .= "/" . join("/", @path) if scalar @path > 0; $path .= "/" . join("/", @path) if scalar @path > 0;
if (isLocalStore) { serveFile($c, $path);
notFound($c, "File '" . $product->path . "' does not exist.") unless -e $product->path;
# Make sure the file is in the Nix store.
$path = checkPath($self, $c, $path);
# If this is a directory but no "/" is attached, then redirect.
if (-d $path && substr($c->request->uri, -1) ne "/") {
return $c->res->redirect($c->request->uri . "/");
}
$path = "$path/index.html" if -d $path && -e "$path/index.html";
notFound($c, "File '$path' does not exist.") if !-e $path;
notFound($c, "Path '$path' is a directory.") if -d $path;
$c->serve_static_file($path);
} else {
serveFile($c, $path);
}
$c->response->headers->last_modified($c->stash->{build}->stoptime); $c->response->headers->last_modified($c->stash->{build}->stoptime);
} }