From 30943dd21785d5d9ad078cd8dbbbcea5860221c5 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 18 Oct 2017 13:38:34 +0200 Subject: [PATCH] Detect MIME type --- src/lib/Hydra/Controller/Build.pm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/Hydra/Controller/Build.pm b/src/lib/Hydra/Controller/Build.pm index b96d0f2f..25084f29 100644 --- a/src/lib/Hydra/Controller/Build.pm +++ b/src/lib/Hydra/Controller/Build.pm @@ -14,6 +14,7 @@ use Nix::Store; use Nix::Config; use List::MoreUtils qw(all); use Encode; +use MIME::Types; sub buildChain :Chained('/') :PathPart('build') :CaptureArgs(1) { @@ -242,7 +243,15 @@ sub download : Chained('buildChain') PathPart { } else { $c->stash->{'plain'} = { data => readNixFile($path) }; - $c->response->content_type('application/octet-stream'); + # Detect MIME type. Borrowed from Catalyst::Plugin::Static::Simple. + my $type = "text/plain"; + if ($path =~ /.*\.(\S{1,})$/xms) { + my $ext = $1; + 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->forward('Hydra::View::Plain'); } }