| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MongoDBx::Tiny::GridFS; |
|
2
|
1
|
|
|
1
|
|
1822
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
28
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
MongoDBx::Tiny::GridFS - wrapper class of MongoDB::GridFS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Carp qw(confess); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
43
|
|
|
12
|
1
|
|
|
1
|
|
488
|
use MongoDB::GridFS; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Params::Validate; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head2 new |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$gridfs = MongoDBx::Tiny::GridFS->new( |
|
20
|
|
|
|
|
|
|
$database->get_gridfs,$fields_name |
|
21
|
|
|
|
|
|
|
); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
|
26
|
|
|
|
|
|
|
my $class = shift; |
|
27
|
|
|
|
|
|
|
my $gridfs = shift or confess q/no gridfs/; |
|
28
|
|
|
|
|
|
|
my $field = shift or confess q/no field/; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
return bless { _gridfs => $gridfs, _field => $field }, $class; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 gridfs, fields |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# get Mongodb::GridFS |
|
36
|
|
|
|
|
|
|
$gridfs_raw = $gridfs->gridfs; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# get fields name |
|
39
|
|
|
|
|
|
|
$gridfs_field_name = $gridfs->field; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=cut |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub gridfs { shift->{_gridfs} } |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub field { shift->{_field} } |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 put |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$gridfs->put('/tmp/foo.txt', {"filename" => 'foo.txt' }); |
|
50
|
|
|
|
|
|
|
$gridfs->put('/tmp/bar.txt','bar.txt'); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$fh = IO::File->new('/tmp/baz.txt','r'); |
|
53
|
|
|
|
|
|
|
$gridfs->put($fh,'baz.txt'); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub put { |
|
58
|
|
|
|
|
|
|
my $self = shift; |
|
59
|
|
|
|
|
|
|
my $proto = shift or confess q/no filepath or filehandle/; |
|
60
|
|
|
|
|
|
|
my $opt = shift or confess q/no gridfs filepath or opt/; |
|
61
|
|
|
|
|
|
|
my $fh; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
if (ref $proto) { |
|
64
|
|
|
|
|
|
|
$fh = $proto; |
|
65
|
|
|
|
|
|
|
} else { |
|
66
|
|
|
|
|
|
|
require IO::File; |
|
67
|
|
|
|
|
|
|
$fh = IO::File->new($proto,'r'); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
if (ref $opt ne 'HASH') { |
|
71
|
|
|
|
|
|
|
# just a gridfs path |
|
72
|
|
|
|
|
|
|
$opt = { $self->field => $opt }; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $no_exists_check = delete $opt->{no_exists_check}; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my %meta = Params::Validate::validate_with( |
|
78
|
|
|
|
|
|
|
params => $opt, |
|
79
|
|
|
|
|
|
|
spec => { |
|
80
|
|
|
|
|
|
|
$self->field => 1, |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
unless ($no_exists_check) { |
|
85
|
|
|
|
|
|
|
# xxx |
|
86
|
|
|
|
|
|
|
return if $self->exists_file( $meta{$self->field} ); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $oid = $self->gridfs->insert($fh, \%meta, { safe => 1 }); |
|
90
|
|
|
|
|
|
|
$self->get($oid); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 get |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# MongoDBx::Tiny::GridFS::File |
|
96
|
|
|
|
|
|
|
$gridfs_file = $gridfs->get({ filename => 'foo.txt' }); |
|
97
|
|
|
|
|
|
|
$foo_txt = $gridfs_file->slurp; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$bar_txt = $gridfs->get('bar.txt')->slurp; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub get { |
|
104
|
|
|
|
|
|
|
my $self = shift; |
|
105
|
|
|
|
|
|
|
my $proto = shift or confess /no id or query/; # $oid,$query |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $query = ref $proto eq 'HASH' ? $proto |
|
108
|
|
|
|
|
|
|
: ref $proto eq 'MongoDB::OID' ? { _id => $proto } |
|
109
|
|
|
|
|
|
|
: { $self->field => $proto }; |
|
110
|
|
|
|
|
|
|
my $gridfs_object = $self->gridfs->find_one($query); |
|
111
|
|
|
|
|
|
|
return unless $gridfs_object; |
|
112
|
|
|
|
|
|
|
return MongoDBx::Tiny::GridFS::File->new( $gridfs_object, $self->field ); |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 remove |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
$gridfs->remove({ filename => 'foo.txt' }); |
|
118
|
|
|
|
|
|
|
$gridfs->remove('bar.txt'); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub remove { |
|
123
|
|
|
|
|
|
|
my $self = shift; |
|
124
|
|
|
|
|
|
|
my $proto = shift or confess /no id or query/; # $oid,$query |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $query = ref $proto eq 'HASH' ? $proto |
|
127
|
|
|
|
|
|
|
: ref $proto eq 'MongoDB::OID' ? { _id => $proto } |
|
128
|
|
|
|
|
|
|
: { $self->field => $proto }; |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
$self->gridfs->remove( $query, {safe => 1, just_one => 1} ); |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 exists_file |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$gridfs->exists_file({ filename => 'foo.txt' }); |
|
136
|
|
|
|
|
|
|
$gridfs->exists_file('bar.txt'); |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub exists_file { |
|
141
|
|
|
|
|
|
|
my $self = shift; |
|
142
|
|
|
|
|
|
|
my $field = $self->field; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
my $val = shift or confess qq/no $field value/; |
|
145
|
|
|
|
|
|
|
return $self->gridfs->find_one({ $field => $val },{ _id => 1 }); |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 MongoDBx::Tiny::GridFS::File |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
wrapper class of MongoDB::GridFS::File |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
package MongoDBx::Tiny::GridFS::File; |
|
156
|
|
|
|
|
|
|
use strict; |
|
157
|
|
|
|
|
|
|
use Carp qw(confess); |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 new |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$gf = MongoDBx::Tiny::GridFS::File->new( $gridfs->find_one($query), $self->field ); |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub new { |
|
166
|
|
|
|
|
|
|
my $class = shift; |
|
167
|
|
|
|
|
|
|
my $g_file = shift or confess q/no GridFS::File object/; |
|
168
|
|
|
|
|
|
|
my $field = shift or confess q/no MongoDBx::Tiny::GridFS::field/; |
|
169
|
|
|
|
|
|
|
# g_file |
|
170
|
|
|
|
|
|
|
# bless { _info => {}, _grid => MongoDB::GridFS } MongoDB::GridFS::File |
|
171
|
|
|
|
|
|
|
unless ($class->can($field)) { |
|
172
|
|
|
|
|
|
|
{ |
|
173
|
|
|
|
|
|
|
no strict 'refs'; |
|
174
|
|
|
|
|
|
|
*{"${class}::" . $field} = sub { shift->gf->{info}->{$field} }; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
return bless { _gridfs_file => $g_file, _field => $field }, $class; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 gridfs_file, gf |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# MongoDB::GridFS::File |
|
185
|
|
|
|
|
|
|
$gf_raw = $gf->gridfs_file; |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub gridfs_file { shift->{_gridfs_file} } |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub gf { shift->gridfs_file } |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 print |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# MongoDB::GridFS::File::print |
|
196
|
|
|
|
|
|
|
$gf->print($fh,$length,$offset); |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub print { shift->gf->print(@_) } |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 slurp |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# MongoDB::GridFS::File::slurp |
|
205
|
|
|
|
|
|
|
$all = $gf->slurp(); |
|
206
|
|
|
|
|
|
|
$buf = $gf->slurp($length,$offset); |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub slurp { shift->gf->slurp(@_) } |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 field |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
field name. default is "filename" |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head2 _id,chunk_size,upload_date,md5 |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
MongoDB::GridFS::File attributes |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub field { shift->{_field} } |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub id { shift->gf->{info}->{_id} } |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub chunk_size { shift->gf->{info}->{chunkSize} } |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub upload_date { shift->gf->{info}->{uploadDate} } |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub md5 { shift->gf->{info}->{md5} } |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
1; |
|
235
|
|
|
|
|
|
|
__END__ |