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