| 1 | package Mango::GridFS::Writer; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use List::Util 'first'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Mango::BSON qw(bson_bin bson_doc bson_oid bson_time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | has chunk_size => 261120; | |||||||||||||||||||||||||||
| 10 | 9 | has [qw(content_type filename gridfs metadata)]; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Already closed | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if ($self->{closed}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | my $files_id = $self->_files_id; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | return $files_id unless $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return Mojo::IOLoop->next_tick(sub { $self->$cb(undef, $files_id) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my @index = (bson_doc(files_id => 1, n => 1), {unique => \1}); | |||||||||||||||||||||||||||
| 23 | 0 | my $gridfs = $self->gridfs; | |||||||||||||||||||||||||||
| 24 | 0 | my $command = bson_doc filemd5 => $self->_files_id, root => $gridfs->prefix; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Non-blocking | ||||||||||||||||||||||||||||
| 27 | 0 | my $chunks = $gridfs->chunks; | |||||||||||||||||||||||||||
| 28 | 0 | my $bulk = $chunks->bulk; | |||||||||||||||||||||||||||
| 29 | 0 | my $files = $gridfs->files; | |||||||||||||||||||||||||||
| 30 | return Mojo::IOLoop->delay( | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub { $self->_chunk($bulk)->execute(shift->begin) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my ($delay, $err) = @_; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | return $delay->pass($err) if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $files->ensure_index({filename => 1} => $delay->begin); | |||||||||||||||||||||||||||
| 36 | 0 | $chunks->ensure_index(@index => $delay->begin); | |||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 39 | 0 | my ($delay, $files_err, $chunks_err) = @_; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if (my $err = $files_err || $chunks_err) { return $delay->pass($err) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$files_err || $chunks_err
MC/DC0%0/2
$files_err || $chunks_err
$files_err
$chunks_err
Truth table0%0/3
A || B
A= $files_errB= $chunks_err
| |||||||||||||||||||||||||||||
| 41 | 0 | $gridfs->db->command($command => $delay->begin); | |||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 44 | 0 | my ($delay, $err, $doc) = @_; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | return $delay->pass($err) if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $files->insert($self->_meta($doc->{md5}) => $delay->begin); | |||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub { shift; $self->$cb(shift, $self->_files_id) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | ) if $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Blocking | ||||||||||||||||||||||||||||
| 52 | 0 | $self->_chunk($bulk)->execute; | |||||||||||||||||||||||||||
| 53 | 0 | $files->ensure_index({filename => 1}); | |||||||||||||||||||||||||||
| 54 | 0 | $chunks->ensure_index(@index); | |||||||||||||||||||||||||||
| 55 | 0 | my $md5 = $gridfs->db->command($command)->{md5}; | |||||||||||||||||||||||||||
| 56 | 0 | $files->insert($self->_meta($md5)); | |||||||||||||||||||||||||||
| 57 | 0 | return $self->_files_id; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub is_closed { !!shift->{closed} } | ||||||||||||||||||||||||||
|
Subroutine
is_closed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self, $chunk, $cb) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Already closed | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($self->is_closed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | croak 'File already closed' unless $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return Mojo::IOLoop->next_tick(sub { $self->$cb('File already closed') }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | $self->{buffer} .= $chunk; | |||||||||||||||||||||||||||
| 72 | 0 | $self->{len} += length $chunk; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $bulk = $self->gridfs->chunks->bulk->ordered(0); | |||||||||||||||||||||||||||
| 75 | 0 | my $size = $self->chunk_size; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | $self->_chunk($bulk) while length $self->{buffer} >= $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Non-blocking | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return $bulk->execute(sub { shift; $self->$cb(shift) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Blocking | ||||||||||||||||||||||||||||
| 82 | 0 | $bulk->execute; | |||||||||||||||||||||||||||
| 83 | 0 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _chunk { | ||||||||||||||||||||||||||
|
Subroutine
_chunk: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 87 | 0 | my ($self, $bulk) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $chunk = substr $self->{buffer}, 0, $self->chunk_size, ''; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | return $bulk unless length $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $n = $self->{n}++; | |||||||||||||||||||||||||||
| 93 | 0 | return $bulk->insert( | |||||||||||||||||||||||||||
| 94 | {files_id => $self->_files_id, n => $n, data => bson_bin($chunk)}); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub _files_id { shift->{files_id} //= bson_oid } | ||||||||||||||||||||||||||
|
Subroutine
_files_id: not called CC=2 SCAR=17.9
Condition0%0/3
+(shift())->{"files_id"} //= bson_oid()
MC/DC0%0/2
+(shift())->{"files_id"} //= bson_oid()
+(shift())->{"files_id"}
bson_oid()
Truth table0%0/3
A //= B
A= +(shift())->{"files_id"}B= bson_oid()
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub _meta { | ||||||||||||||||||||||||||
|
Subroutine
_meta: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $md5) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | my $doc = { | ||||||||||||||||||||||||||||
| 103 | _id => $self->_files_id, | ||||||||||||||||||||||||||||
| 104 | length => $self->{len}, | ||||||||||||||||||||||||||||
| 105 | 0 | chunkSize => $self->chunk_size, | |||||||||||||||||||||||||||
| 106 | uploadDate => bson_time, | ||||||||||||||||||||||||||||
| 107 | md5 => $md5 | ||||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (my $name = $self->filename) { $doc->{filename} = $name } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (my $type = $self->content_type) { $doc->{contentType} = $type } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (my $data = $self->metadata) { $doc->{metadata} = $data } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | return $doc; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =encoding utf8 | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 NAME | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Mango::GridFS::Writer - GridFS writer | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | use Mango::GridFS::Writer; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | my $writer = Mango::GridFS::Writer->new(gridfs => $gridfs); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | L<Mango::GridFS::Writer> writes files to GridFS. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | L<Mango::GridFS::Writer> implements the following attributes. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 chunk_size | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | my $size = $writer->chunk_size; | ||||||||||||||||||||||||||||
| 141 | $writer = $writer->chunk_size(1024); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Chunk size in bytes, defaults to C<261120> (255KB). | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 content_type | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | my $type = $writer->content_type; | ||||||||||||||||||||||||||||
| 148 | $writer = $writer->content_type('text/plain'); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Content type of file. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 filename | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $name = $writer->filename; | ||||||||||||||||||||||||||||
| 155 | $writer = $writer->filename('foo.txt'); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Name of file. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 gridfs | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | my $gridfs = $writer->gridfs; | ||||||||||||||||||||||||||||
| 162 | $writer = $writer->gridfs(Mango::GridFS->new); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | L<Mango::GridFS> object this writer belongs to. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 metadata | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | my $data = $writer->metadata; | ||||||||||||||||||||||||||||
| 169 | $writer = $writer->metadata({foo => 'bar'}); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Additional information. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 METHODS | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | L<Mango::GridFS::Writer> inherits all methods from L<Mojo::Base> and | ||||||||||||||||||||||||||||
| 176 | implements the following new ones. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 close | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | my $oid = $writer->close; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Close file. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | $writer->close(sub { | ||||||||||||||||||||||||||||
| 185 | my ($writer, $err, $oid) = @_; | ||||||||||||||||||||||||||||
| 186 | ... | ||||||||||||||||||||||||||||
| 187 | }); | ||||||||||||||||||||||||||||
| 188 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 is_closed | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | my $success = $writer->is_closed; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Check if file has been closed. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 write | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | $writer = $writer->write('hello world!'); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Write chunk. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | $writer->write('hello world!' => sub { | ||||||||||||||||||||||||||||
| 203 | my ($writer, $err) = @_; | ||||||||||||||||||||||||||||
| 204 | ... | ||||||||||||||||||||||||||||
| 205 | }); | ||||||||||||||||||||||||||||
| 206 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||