/usr/share/perl5/Catmandu/FileBag.pm is in libcatmandu-perl 1.0700-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | package Catmandu::FileBag;
our $VERSION = '1.07';
use Catmandu::Sane;
use IO::String;
use Catmandu::Util qw(:is :check);
use Moo::Role;
use namespace::clean;
sub stream {
my ($self, $io, $data) = @_;
check_hash_ref($data);
check_invocant($io);
$data->{_stream}->($io);
}
sub as_string {
my ($self, $data) = @_;
check_hash_ref($data);
my $str;
my $io = IO::String->new($str);
$data->{_stream}->($io);
$str;
}
sub as_string_utf8 {
my ($self, $data) = @_;
check_hash_ref($data);
my $str;
my $io = IO::String->new($str);
$data->{_stream}->($io);
utf8::decode($str);
$str;
}
sub upload {
my ($self, $io, $id) = @_;
check_string($id);
check_invocant($io);
my $file = {_id => $id, _stream => $io};
$self->add($file);
# The add() method of FileBags should inline data the passed $file with
# file metadata. Use a get($id) when this inline update wasn't implemented
# by the Bag.
if (exists $file->{size}) {
# all ok
}
else {
$self->log->warn(
"$self doesn't inline update \$data in add(\$data) method");
$file = $self->get($id);
}
if (!defined($file)) {
return 0;
}
elsif (is_hash_ref($file)) {
return $file->{size};
}
else {
$self->log->error("expecting a HASH but got `$file'");
return 0;
}
}
1;
__END__
=pod
=head1 NAME
Catmandu::FileBag - A Catmandu::FileStore compartment to persist binary data
=head1 SYNOPSIS
use Catmandu;
my $store = Catmandu->store('Simple' , root => 't/data');
# List all containers
$store->bag->each(sub {
my $container = shift;
print "%s\n" , $container->{_id};
});
# Add a new folder
$store->bag->add({ _id => '1234' });
# Get the files
my $files = $store->bag->files('1234');
# Add a file to the files
$files->upload(IO::File->new('<foobar.txt'), 'foobar.txt');
# Stream the contents of a file
my $file = $files->get('foobar.txt');
$files->stream(IO::File->new('>foobar.txt'), $file);
# Delete a file
$files->delete('foobar.txt');
# Delete a folder
$store->index->delete('1234');
=head1 DESCRIPTION
Each L<Catmandu::FileBag> is a L<Catmandu::Bag> and inherits all its methods.
=head1 METHODS
=head2 upload($io, $file_name)
An helper application to add an IO::Handle $io to the L<Catmandu::FileBag>. Returns
the number of bytes written.
=head2 stream($io, $file)
A helper application to stream the contents of a L<Catmandu::FileBag> item
to an IO::Handle. Returns the number of bytes written.
=head2 as_string($file)
Return the contents of the L<Catmandu::FileBag> item as a string.
=head2 as_string_utf8($file)
Return the contents of the L<Catmandu::FileBag> item as an UTF-8 string.
=head1 SEE ALSO
L<Catmandu::FileStore> ,
L<Catmandu::FileBag::Index>
=cut
|