summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-12-04 01:05:14 -0500
committerJesse Luehrs <doy@tozt.net>2015-12-04 01:05:14 -0500
commit97e713249292eb4eb7ac6ba9fd05abbd1eb00fa5 (patch)
tree76103327160a0e7e353a32329f559e3c3d750a80
parentf1f3c514a88f9577b20a5166ccf5a801a9acb614 (diff)
downloadspreadsheet-parsexlsx-97e713249292eb4eb7ac6ba9fd05abbd1eb00fa5.tar.gz
spreadsheet-parsexlsx-97e713249292eb4eb7ac6ba9fd05abbd1eb00fa5.zip
simplify signature checking
-rw-r--r--lib/Spreadsheet/ParseXLSX.pm47
1 files changed, 25 insertions, 22 deletions
diff --git a/lib/Spreadsheet/ParseXLSX.pm b/lib/Spreadsheet/ParseXLSX.pm
index dd247b7..3d1ebd6 100644
--- a/lib/Spreadsheet/ParseXLSX.pm
+++ b/lib/Spreadsheet/ParseXLSX.pm
@@ -65,29 +65,12 @@ sub parse {
my $workbook = Spreadsheet::ParseExcel::Workbook->new;
- my $signature = '';
my $tempfile;
- if (openhandle($file)) {
- if (ref($file) eq 'GLOB') {
- read($file, $signature, 2);
- seek($file, -2, IO::File::SEEK_CUR);
- } else {
- $file->read($signature, 2);
- $file->seek(-2, IO::File::SEEK_CUR);
- }
- $workbook->{File} = undef;
- } elsif (!ref($file)) {
- my $fh = IO::File->new();
- if ($fh->open("<$file")) {
- $workbook->{File} = $file;
- $fh->read($signature, 2);
- $fh->seek(-2, IO::File::SEEK_CUR);
- $file = $fh;
- }
- }
-
- if ($signature eq "\xd0\xcf") {
- $tempfile = $file = Spreadsheet::ParseXLSX::Decryptor->open($file, $self->{Password});
+ if ($self->_check_signature($file)) {
+ $tempfile = $file = Spreadsheet::ParseXLSX::Decryptor->open(
+ $file,
+ $self->{Password}
+ );
}
eval {
@@ -96,6 +79,7 @@ sub parse {
bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
$zip->readFromFileHandle($file) == Archive::Zip::AZ_OK
or die "Can't open filehandle as a zip file";
+ $workbook->{File} = undef;
}
elsif (!ref($file)) {
$zip->read($file) == Archive::Zip::AZ_OK
@@ -116,6 +100,25 @@ sub parse {
return $workbook;
}
+sub _check_signature {
+ my $self = shift;
+ my ($file) = @_;
+
+ my $signature = '';
+ if (openhandle($file)) {
+ bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
+ $file->read($signature, 2);
+ $file->seek(-2, IO::File::SEEK_CUR);
+ }
+ elsif (!ref($file)) {
+ my $fh = IO::File->new($file, 'r');
+ $fh->read($signature, 2);
+ $fh->close;
+ }
+
+ return $signature eq "\xd0\xcf";
+}
+
sub _parse_workbook {
my $self = shift;
my ($zip, $workbook, $formatter) = @_;