Selaa lähdekoodia

Fix #311: Integrate usage of LibMagic as YET ANOTHER fallback.

George S. Baugh 4 kuukautta sitten
vanhempi
commit
d1f01b1eb8
7 muutettua tiedostoa jossa 35 lisäystä ja 37 poistoa
  1. 1 1
      Installer.mk
  2. 1 0
      Makefile.PL
  3. 0 2
      lib/TCMS.pm
  4. 0 1
      lib/Trog/Data/FlatFile.pm
  5. 3 17
      lib/Trog/DataModule.pm
  6. 2 15
      lib/Trog/FileHandler.pm
  7. 28 1
      lib/Trog/Utils.pm

+ 1 - 1
Installer.mk

@@ -41,7 +41,7 @@ prereq-debs:
 	    libfile-copy-recursive-perl libxml-rss-perl libmodule-install-perl libio-string-perl uuid-dev                    \
 	    libmoose-perl libmoosex-types-datetime-perl libxml-libxml-perl liblist-moreutils-perl libclone-perl libpath-tiny-perl \
 		selinux-utils setools policycoreutils-python-utils policycoreutils selinux-basics auditd \
-		pdns-tools pdns-server pdns-backend-sqlite3
+		pdns-tools pdns-server pdns-backend-sqlite3 libmagic-dev
 
 .PHONY: prereq-perl
 prereq-perl:

+ 1 - 0
Makefile.PL

@@ -70,6 +70,7 @@ WriteMakefile(
     'Email::Sender::Simple'     => '0',
     'DNS::Unbound'              => '0',
     'Net::IP'                   => '0',
+    'File::LibMagic'            => '0',
   },
   test => {TESTS => 't/*.t'}
 );

+ 0 - 2
lib/TCMS.pm

@@ -13,8 +13,6 @@ use Sys::Hostname();
 use HTTP::Body   ();
 use URL::Encode  ();
 use Text::Xslate ();
-use Plack::MIME  ();
-use Mojo::File   ();
 use DateTime::Format::HTTP();
 use CGI::Cookie ();
 use File::Basename();

+ 0 - 1
lib/Trog/Data/FlatFile.pm

@@ -10,7 +10,6 @@ use Carp qw{confess};
 use JSON::MaybeXS;
 use File::Slurper;
 use File::Copy;
-use Mojo::File;
 use Path::Tiny();
 use Capture::Tiny qw{capture_merged};
 

+ 3 - 17
lib/Trog/DataModule.pm

@@ -7,8 +7,6 @@ use FindBin::libs;
 
 use List::Util;
 use File::Copy;
-use Mojo::File;
-use Plack::MIME;
 use Path::Tiny();
 use Ref::Util();
 
@@ -438,21 +436,9 @@ sub _process ($post) {
     @{ $post->{aliases} } = List::Util::uniq( @{ $post->{aliases} } );
 
     # Handle multimedia content types
-    if ( $post->{href} ) {
-        my $mf  = Mojo::File->new("www/$post->{href}");
-        my $ext = '.' . $mf->extname();
-        $post->{content_type} = Plack::MIME->mime_type($ext) if $ext;
-    }
-    if ( $post->{video_href} ) {
-        my $mf  = Mojo::File->new("www/$post->{video_href}");
-        my $ext = '.' . $mf->extname();
-        $post->{video_content_type} = Plack::MIME->mime_type($ext) if $ext;
-    }
-    if ( $post->{audio_href} ) {
-        my $mf  = Mojo::File->new("www/$post->{audio_href}");
-        my $ext = '.' . $mf->extname();
-        $post->{audio_content_type} = Plack::MIME->mime_type($ext) if $ext;
-    }
+    $post->{content_type}       = Trog::Utils::mime_type("www/$post->{href}")       if $post->{href};
+    $post->{video_content_type} = Trog::Utils::mime_type("www/$post->{video_href}") if $post->{video_href};
+    $post->{audio_content_type} = Trog::Utils::mime_type("www/$post->{audio_href}") if $post->{audio_href};
     $post->{content_type} ||= 'text/html';
 
     $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;

+ 2 - 15
lib/Trog/FileHandler.pm

@@ -7,19 +7,12 @@ no warnings 'experimental';
 use feature qw{signatures};
 
 use POSIX qw{strftime};
-use Mojo::File;
-use Plack::MIME;
 use IO::Compress::Gzip;
 use Time::HiRes qw{tv_interval};
 
 use Trog::Log qw{:all};
 use Trog::Vars;
-
-#TODO consider integrating libfile
-#Stuff that isn't in upstream finders
-my %extra_types = (
-    '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-);
+use Trog::Utils;
 
 =head2 serve
 
@@ -28,13 +21,7 @@ Serve a file, with options to stream and cache the output.
 =cut
 
 sub serve ( $fullpath, $path, $start, $streaming, $ranges, $last_fetch = 0, $deflate = 0 ) {
-    my $mf  = Mojo::File->new($path);
-    my $ext = '.' . $mf->extname();
-    my $ft;
-    if ($ext) {
-        $ft = Plack::MIME->mime_type($ext) if $ext;
-        $ft ||= $extra_types{$ext} if exists $extra_types{$ext};
-    }
+    my $ft = Trog::Utils::mime_type($path);
     $ft ||= $Trog::Vars::content_types{text};
 
     my $ct      = 'Content-type';

+ 28 - 1
lib/Trog/Utils.pm

@@ -4,10 +4,15 @@ use strict;
 use warnings;
 
 no warnings 'experimental';
-use feature qw{signatures};
+use feature qw{signatures state};
 
 use UUID;
 use HTTP::Tiny::UNIX();
+use Plack::MIME;
+use Mojo::File;
+use File::LibMagic;
+use Ref::Util qw{is_hashref};
+
 use Trog::Log qw{WARN};
 use Trog::Config();
 
@@ -44,4 +49,26 @@ sub uuid {
     return UUID::uuid();
 }
 
+#Stuff that isn't in upstream finders
+my %extra_types = (
+    '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+);
+
+sub mime_type ($file) {
+    # Use libmagic and if that doesn't work try guessing based on extension.
+    my $mt;
+    my $mf  = Mojo::File->new($file);
+    my $ext = '.' . $mf->extname();
+    $mt = Plack::MIME->mime_type($ext) if $ext;
+    $mt ||= $extra_types{$ext} if exists $extra_types{$ext};
+    return $mt if $mt;
+
+    # If all else fails, time for libmagic
+    state $magic = File::LibMagic->new;
+    my $maybe_ct = $magic->info_from_filename($file);
+    $mt = $maybe_ct->{mime_type} if (is_hashref( $maybe_ct ) && $maybe_ct->{mime_type});
+
+    return $mt;
+}
+
 1;