/usr/share/perl5/Mango/GridFS/Writer.pm is in libmango-perl 1.29-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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | package Mango::GridFS::Writer;
use Mojo::Base -base;
use Carp 'croak';
use List::Util 'first';
use Mango::BSON qw(bson_bin bson_doc bson_oid bson_time);
use Mojo::IOLoop;
has chunk_size => 261120;
has [qw(content_type filename gridfs metadata)];
sub close {
my ($self, $cb) = @_;
# Already closed
if ($self->{closed}++) {
my $files_id = $self->_files_id;
return $files_id unless $cb;
return Mojo::IOLoop->next_tick(sub { $self->$cb(undef, $files_id) });
}
my @index = (bson_doc(files_id => 1, n => 1), {unique => \1});
my $gridfs = $self->gridfs;
my $command = bson_doc filemd5 => $self->_files_id, root => $gridfs->prefix;
# Non-blocking
my $chunks = $gridfs->chunks;
my $bulk = $chunks->bulk;
my $files = $gridfs->files;
return Mojo::IOLoop->delay(
sub { $self->_chunk($bulk)->execute(shift->begin) },
sub {
my ($delay, $err) = @_;
return $delay->pass($err) if $err;
$files->ensure_index({filename => 1} => $delay->begin);
$chunks->ensure_index(@index => $delay->begin);
},
sub {
my ($delay, $files_err, $chunks_err) = @_;
if (my $err = $files_err || $chunks_err) { return $delay->pass($err) }
$gridfs->db->command($command => $delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
return $delay->pass($err) if $err;
$files->insert($self->_meta($doc->{md5}) => $delay->begin);
},
sub { shift; $self->$cb(shift, $self->_files_id) }
) if $cb;
# Blocking
$self->_chunk($bulk)->execute;
$files->ensure_index({filename => 1});
$chunks->ensure_index(@index);
my $md5 = $gridfs->db->command($command)->{md5};
$files->insert($self->_meta($md5));
return $self->_files_id;
}
sub is_closed { !!shift->{closed} }
sub write {
my ($self, $chunk, $cb) = @_;
# Already closed
if ($self->is_closed) {
croak 'File already closed' unless $cb;
return Mojo::IOLoop->next_tick(sub { $self->$cb('File already closed') });
}
$self->{buffer} .= $chunk;
$self->{len} += length $chunk;
my $bulk = $self->gridfs->chunks->bulk->ordered(0);
my $size = $self->chunk_size;
$self->_chunk($bulk) while length $self->{buffer} >= $size;
# Non-blocking
return $bulk->execute(sub { shift; $self->$cb(shift) }) if $cb;
# Blocking
$bulk->execute;
return $self;
}
sub _chunk {
my ($self, $bulk) = @_;
my $chunk = substr $self->{buffer}, 0, $self->chunk_size, '';
return $bulk unless length $chunk;
my $n = $self->{n}++;
return $bulk->insert(
{files_id => $self->_files_id, n => $n, data => bson_bin($chunk)});
}
sub _files_id { shift->{files_id} //= bson_oid }
sub _meta {
my ($self, $md5) = @_;
my $doc = {
_id => $self->_files_id,
length => $self->{len},
chunkSize => $self->chunk_size,
uploadDate => bson_time,
md5 => $md5
};
if (my $name = $self->filename) { $doc->{filename} = $name }
if (my $type = $self->content_type) { $doc->{contentType} = $type }
if (my $data = $self->metadata) { $doc->{metadata} = $data }
return $doc;
}
1;
=encoding utf8
=head1 NAME
Mango::GridFS::Writer - GridFS writer
=head1 SYNOPSIS
use Mango::GridFS::Writer;
my $writer = Mango::GridFS::Writer->new(gridfs => $gridfs);
=head1 DESCRIPTION
L<Mango::GridFS::Writer> writes files to GridFS.
=head1 ATTRIBUTES
L<Mango::GridFS::Writer> implements the following attributes.
=head2 chunk_size
my $size = $writer->chunk_size;
$writer = $writer->chunk_size(1024);
Chunk size in bytes, defaults to C<261120> (255KB).
=head2 content_type
my $type = $writer->content_type;
$writer = $writer->content_type('text/plain');
Content type of file.
=head2 filename
my $name = $writer->filename;
$writer = $writer->filename('foo.txt');
Name of file.
=head2 gridfs
my $gridfs = $writer->gridfs;
$writer = $writer->gridfs(Mango::GridFS->new);
L<Mango::GridFS> object this writer belongs to.
=head2 metadata
my $data = $writer->metadata;
$writer = $writer->metadata({foo => 'bar'});
Additional information.
=head1 METHODS
L<Mango::GridFS::Writer> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 close
my $oid = $writer->close;
Close file. You can also append a callback to perform operation non-blocking.
$writer->close(sub {
my ($writer, $err, $oid) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 is_closed
my $success = $writer->is_closed;
Check if file has been closed.
=head2 write
$writer = $writer->write('hello world!');
Write chunk. You can also append a callback to perform operation non-blocking.
$writer->write('hello world!' => sub {
my ($writer, $err) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 SEE ALSO
L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|