Skip to content

Commit

Permalink
Item13821: remove dependency on File::MMagic
Browse files Browse the repository at this point in the history
... fixes pontential high memory usage
  • Loading branch information
MichaelDaum committed Oct 18, 2015
1 parent 1a0ad6f commit c7797fe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 3 additions & 2 deletions data/System/StringifierContrib.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" date="1443690574" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1445161237" format="1.1" version="1"}%
---+!! StringifierContrib
%FORMFIELD{"Description"}%

Expand Down Expand Up @@ -51,7 +51,7 @@ To index Word Documents (=.doc=) you will need to install one of the following:

---++ Backend for PDF

To index =.pdf= files you need to install =proppler-utils=.
To index =.pdf= files you need to install =poppler-utils=.

---++ Backend for PPT

Expand Down Expand Up @@ -139,6 +139,7 @@ Foswiki:Development/UnitTests for more information on unit testing.
%$DEPENDENCIES%

---++ Change History
| 18 Oct 2015: | (4.20) removed dependency on File::MMagic; now using extension-based mime detection |
| 01 Oct 2015: | (4.10) don't default to pass-through for non-supported document types; fixed unit tests |
| 29 Sep 2015: | (4.00) added unicode support with Foswiki > 2.0 |
| 22 Jul 2015: | (3.10) added support for stringification of ppt using catdoc as ppthtml isn't available on some distros |
Expand Down
36 changes: 29 additions & 7 deletions lib/Foswiki/Contrib/Stringifier.pm
Expand Up @@ -11,29 +11,50 @@
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Contrib::Stringifier;

use strict;
use warnings;

use Foswiki::Func ();
use Foswiki::Contrib::Stringifier::Base;
our @ISA = qw( Foswiki::Contrib::Stringifier::Base );
use File::MMagic();

our $mmagic;
sub mmagic {
$mmagic = File::MMagic->new() unless $mmagic;
return $mmagic;
our $types;
sub _getMimeType {
my $fileName = shift;

my $mimeType;

if ($fileName && $fileName =~ /\.([^.]+)$/) {
my $suffix = $1;

unless ($types) {
$types = Foswiki::Func::readFile($Foswiki::cfg{MimeTypesFileName});
}

if ($types =~ /^([^#]\S*).*?\s$suffix(?:\s|$)/im) {
$mimeType = $1;
}
}

return $mimeType;
}



sub stringFor {
my ($class, $filename) = @_;

return unless -r $filename;
my $mime = mmagic->checktype_filename($filename);
my $mime = _getMimeType($filename);

#print STDERR "no mime for $filename\n" unless $mime;
return unless $mime;

my $impl = $class->handler_for($filename, $mime);

#print STDERR "file $filename is a $mime ... using ".($impl||'undef')."\n";
Expand All @@ -45,5 +66,6 @@ sub stringFor {
}



1;

4 changes: 2 additions & 2 deletions lib/Foswiki/Contrib/StringifierContrib.pm
Expand Up @@ -18,8 +18,8 @@ package Foswiki::Contrib::StringifierContrib;
use strict;
use warnings;

our $VERSION = '4.10';
our $RELEASE = '01 Sep 2015';
our $VERSION = '4.20';
our $RELEASE = '18 Oct 2015';
our $SHORTDESCRIPTION = 'Helper library to stringify binary document formats';
our $NO_PREFS_IN_TOPIC = 1;

Expand Down
1 change: 0 additions & 1 deletion lib/Foswiki/Contrib/StringifierContrib/DEPENDENCIES
@@ -1,4 +1,3 @@
File::MMagic,>0,cpan,Required
File::Which,>0,cpan,Required
Module::Pluggable,>0,cpan,Required
Spreadsheet::ParseExcel,>0,cpan,Required for =.xls= files
Expand Down

0 comments on commit c7797fe

Please sign in to comment.