Skip to content

Commit

Permalink
Adding required file name escaping in core module #10
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Feb 16, 2020
1 parent 98d518f commit e27b670
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/Catmandu/BagIt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ has 'algorithm' => (
},
);

# Set if we do file escaping
has 'escape' => (
is => 'ro',
default => sub { 1 },
init_arg => undef,
);

sub _build_user_agent {
my ($self) = @_;
my $ua = LWP::UserAgent->new;
Expand Down Expand Up @@ -1047,6 +1054,12 @@ sub _read_manifest {
chomp($line);
my ($sum,$file) = split(/\s+/,$line,2);
$file =~ s/^data\///;
# Unescape LF,CR,% when needed
if ($self->escape) {
$file =~ s{%0A}{\n}mg;
$file =~ s{%0D}{\r}mg;
$file =~ s{%25}{%}mg;
}
$self->_sums->{$file} = $sum;
}

Expand Down Expand Up @@ -1367,6 +1380,12 @@ sub _manifest_as_string {
foreach my $file ($self->list_checksum) {
next unless -f $self->_payload_file($path,$file);
my $sum = $self->get_checksum($file);
# Escape LF, CR and % (when needed)
if ($self->escape) {
$file =~ s{%}{%25}mg;
$file =~ s{\n}{%0A}mg;
$file =~ s{\r}{%0D}mg;
}
$str .= "$sum data/$file\n";
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Catmandu/Store/File/BagIt/Bag.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ sub _build__path {

sub _build__bagit {
my $self = shift;
Catmandu::BagIt->read($self->_path);
my $bag = Catmandu::BagIt->read($self->_path);
$bag->{escape} = 0 if $bag; # This implementation does its own file escaping...
$bag;
}

sub generator {
Expand Down Expand Up @@ -105,7 +107,7 @@ sub add {

unless ($bagit) {
$update = 0;
$bagit = Catmandu::BagIt->new;
$bagit = Catmandu::BagIt->new(algorithm => 'md5', escape => 0);
$self->{_bagit} = $bagit;
}

Expand Down

0 comments on commit e27b670

Please sign in to comment.