line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mango::GridFS; |
2
|
9
|
|
|
9
|
|
31
|
use Mojo::Base -base; |
|
9
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
48
|
|
3
|
|
|
|
|
|
|
|
4
|
9
|
|
|
9
|
|
3748
|
use Mango::GridFS::Reader; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
58
|
|
5
|
9
|
|
|
9
|
|
3068
|
use Mango::GridFS::Writer; |
|
9
|
|
|
|
|
15
|
|
|
9
|
|
|
|
|
51
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has chunks => sub { $_[0]->db->collection($_[0]->prefix . '.chunks') }; |
8
|
|
|
|
|
|
|
has 'db'; |
9
|
|
|
|
|
|
|
has files => sub { $_[0]->db->collection($_[0]->prefix . '.files') }; |
10
|
|
|
|
|
|
|
has prefix => 'fs'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub delete { |
13
|
0
|
|
|
0
|
1
|
|
my ($self, $oid, $cb) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Non-blocking |
16
|
|
|
|
|
|
|
return Mojo::IOLoop->delay( |
17
|
|
|
|
|
|
|
sub { |
18
|
0
|
|
|
0
|
|
|
my $delay = shift; |
19
|
0
|
|
|
|
|
|
$self->files->remove({_id => $oid} => $delay->begin); |
20
|
0
|
|
|
|
|
|
$self->chunks->remove({files_id => $oid} => $delay->begin); |
21
|
|
|
|
|
|
|
}, |
22
|
0
|
|
0
|
0
|
|
|
sub { $self->$cb($_[1] || $_[3]) } |
23
|
0
|
0
|
|
|
|
|
) if $cb; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Blocking |
26
|
0
|
|
|
|
|
|
$self->files->remove({_id => $oid}); |
27
|
0
|
|
|
|
|
|
$self->chunks->remove({files_id => $oid}); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub find_version { |
31
|
0
|
|
|
0
|
1
|
|
my ($self, $name, $version, $cb) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Positive numbers are absolute and negative ones relative |
34
|
0
|
|
|
|
|
|
my $cursor = $self->files->find({filename => $name}, {_id => 1})->limit(-1); |
35
|
0
|
0
|
|
|
|
|
$cursor->sort({uploadDate => $version < 0 ? -1 : 1}) |
|
|
0
|
|
|
|
|
|
36
|
|
|
|
|
|
|
->skip($version < 0 ? abs($version) - 1 : $version); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Non-blocking |
39
|
|
|
|
|
|
|
return $cursor->next( |
40
|
0
|
0
|
|
0
|
|
|
sub { shift; $self->$cb(shift, $_[0] ? $_[0]{_id} : undef) }) |
|
0
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
if $cb; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Blocking |
44
|
0
|
|
|
|
|
|
my $doc = $cursor->next; |
45
|
0
|
0
|
|
|
|
|
return $doc ? $doc->{_id} : undef; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub list { |
49
|
0
|
|
|
0
|
1
|
|
my ($self, $cb) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Blocking |
52
|
0
|
0
|
|
|
|
|
return $self->files->find->distinct('filename') unless $cb; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Non-blocking |
55
|
0
|
|
|
0
|
|
|
$self->files->find->distinct('filename' => sub { shift; $self->$cb(@_) }); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
0
|
1
|
|
sub reader { Mango::GridFS::Reader->new(gridfs => shift) } |
59
|
0
|
|
|
0
|
1
|
|
sub writer { Mango::GridFS::Writer->new(gridfs => shift) } |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
1; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=encoding utf8 |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Mango::GridFS - GridFS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
use Mango::GridFS; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $gridfs = Mango::GridFS->new(db => $db); |
74
|
|
|
|
|
|
|
my $reader = $gridfs->reader; |
75
|
|
|
|
|
|
|
my $writer = $gridfs->writer; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 DESCRIPTION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
L is an interface for MongoDB GridFS access. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
L implements the following attributes. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 chunks |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $chunks = $gridfs->chunks; |
88
|
|
|
|
|
|
|
$gridfs = $gridfs->chunks(Mango::Collection->new); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
L object for C collection, defaults to one based on |
91
|
|
|
|
|
|
|
L"prefix">. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 db |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $db = $gridfs->db; |
96
|
|
|
|
|
|
|
$gridfs = $gridfs->db(Mango::Database->new); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
L object GridFS belongs to. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 files |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $files = $gridfs->files; |
103
|
|
|
|
|
|
|
$gridfs = $gridfs->files(Mango::Collection->new); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
L object for C collection, defaults to one based on |
106
|
|
|
|
|
|
|
L"prefix">. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 prefix |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $prefix = $gridfs->prefix; |
111
|
|
|
|
|
|
|
$gridfs = $gridfs->prefix('foo'); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Prefix for GridFS collections, defaults to C. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 METHODS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
L inherits all methods from L and implements the |
118
|
|
|
|
|
|
|
following new ones. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 delete |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$gridfs->delete($oid); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Delete file. You can also append a callback to perform operation non-blocking. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$gridfs->delete($oid => sub { |
127
|
|
|
|
|
|
|
my ($gridfs, $err) = @_; |
128
|
|
|
|
|
|
|
... |
129
|
|
|
|
|
|
|
}); |
130
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 find_version |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $oid = $gridfs->find_version('test.txt', 1); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Find versions of files, positive numbers from C<0> and upwards always point to |
137
|
|
|
|
|
|
|
a specific version, negative ones start with C<-1> for the most recently added |
138
|
|
|
|
|
|
|
version. You can also append a callback to perform operation non-blocking. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$gridfs->find_version(('test.txt', 1) => sub { |
141
|
|
|
|
|
|
|
my ($gridfs, $err, $oid) = @_; |
142
|
|
|
|
|
|
|
... |
143
|
|
|
|
|
|
|
}); |
144
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 list |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $names = $gridfs->list; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
List files. You can also append a callback to perform operation non-blocking. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$gridfs->list(sub { |
153
|
|
|
|
|
|
|
my ($gridfs, $err, $names) = @_; |
154
|
|
|
|
|
|
|
... |
155
|
|
|
|
|
|
|
}); |
156
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 reader |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $reader = $gridfs->reader; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Build L object. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Read all data at once from newest version of file |
165
|
|
|
|
|
|
|
my $oid = $gridfs->find_version('test.txt', -1); |
166
|
|
|
|
|
|
|
my $data = $gridfs->reader->open($oid)->slurp; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Read all data in chunks from file |
169
|
|
|
|
|
|
|
my $reader = $gridfs->reader->open($oid); |
170
|
|
|
|
|
|
|
while (defined(my $chunk = $reader->read)) { say "Chunk: $chunk" } |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 writer |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $writer = $gridfs->writer; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Build L object. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Write all data at once to file with name |
179
|
|
|
|
|
|
|
my $oid = $gridfs->writer->filename('test.txt')->write('Hello!')->close; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# Write data in chunks to file |
182
|
|
|
|
|
|
|
my $writer = $gridfs->writer; |
183
|
|
|
|
|
|
|
$writer->write($_) for 1 .. 100; |
184
|
|
|
|
|
|
|
my $oid = $writer->close; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SEE ALSO |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
L, L, L. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |