This file is indexed.

/usr/share/perl5/Mango/GridFS/Writer.pm is in libmango-perl 0.22-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
213
214
215
216
217
218
219
220
221
222
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 bson_true);
use Mojo::IOLoop;

has chunk_size => 262144;
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->timer(0 => sub { $self->$cb(undef, $files_id) });
  }

  my @index   = (bson_doc(files_id => 1, n => 1), {unique => bson_true});
  my $gridfs  = $self->gridfs;
  my $command = bson_doc filemd5 => $self->{files_id}, root => $gridfs->prefix;

  # Blocking
  my $files = $gridfs->files;
  unless ($cb) {
    $self->_chunk;
    $files->ensure_index({filename => 1});
    $gridfs->chunks->ensure_index(@index);
    my $md5 = $gridfs->db->command($command)->{md5};
    $files->insert($self->_meta($md5));
    return $self->{files_id};
  }

  # Non-blocking
  Mojo::IOLoop->delay(
    sub { $self->_chunk(shift->begin) },
    sub {
      my ($delay, $err) = @_;
      return $self->$cb($err) if $err;
      $files->ensure_index({filename => 1} => $delay->begin);
      $gridfs->chunks->ensure_index(@index => $delay->begin);
    },
    sub {
      my ($delay, $files_err, $chunks_err) = @_;
      if (my $err = $files_err || $chunks_err) { return $self->$cb($err) }
      $gridfs->db->command($command => $delay->begin);
    },
    sub {
      my ($delay, $err, $doc) = @_;
      return $self->$cb($err) if $err;
      $files->insert($self->_meta($doc->{md5}) => $delay->begin);
    },
    sub { shift; $self->$cb(shift, $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->timer(0 => sub { $self->$cb('File already closed') });
  }

  $self->{buffer} .= $chunk;
  $self->{len} += length $chunk;

  # Non-blocking
  my $size = $self->chunk_size;
  if ($cb) {
    my $delay = Mojo::IOLoop->delay(sub { shift; $self->_err($cb, @_) });
    $self->_chunk($delay->begin) while length $self->{buffer} >= $size;
    $delay->begin->(undef, undef);
  }

  # Blocking
  else { $self->_chunk while length $self->{buffer} >= $size }

  return $self;
}

sub _chunk {
  my ($self, $cb) = @_;

  my $chunk = substr $self->{buffer}, 0, $self->chunk_size, '';
  return $cb ? Mojo::IOLoop->timer(0 => $cb) : () unless length $chunk;

  # Blocking
  my $n   = $self->{n}++;
  my $oid = $self->{files_id} //= bson_oid;
  my $doc = {files_id => $oid, n => $n, data => bson_bin($chunk)};
  return $self->gridfs->chunks->insert($doc) unless $cb;

  # Non-blocking
  $self->gridfs->chunks->insert($doc => $cb);
}

sub _err {
  my ($self, $cb) = (shift, shift);
  $self->$cb(first {defined} @_[map { 2 * $_ } 0 .. @_ / 2]);
}

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<262144>.

=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