| 1 | ## Copyright (C) Graham Barr | ||
| 2 | ## vim: ts=8:sw=2:expandtab:shiftround | ||
| 3 | |||
| 4 | ## WARNING!!!! | ||
| 5 | ## WARNING!!!! This code is still very much a work in progress, | ||
| 6 | ## WARNING!!!! do not depend on anything not changing | ||
| 7 | ## WARNING!!!! | ||
| 8 | |||
| 9 | package AnyEvent::MongoDB::GridFS; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | use DateTime; | ||
| 13 | |||
| 14 | use aliased 'AnyEvent::MongoDB::GridFS::File' => 'GridFSFile'; | ||
| 15 | use aliased 'AnyEvent::MongoDB::Database'; | ||
| 16 | |||
| 17 | has prefix => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'Str', | ||
| 20 | default => 'fs', | ||
| 21 | ); | ||
| 22 | |||
| 23 | has database => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Database, | ||
| 26 | required => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has files => ( | ||
| 30 | is => 'ro', | ||
| 31 | init_arg => undef, | ||
| 32 | lazy_build => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | sub _build_files { | ||
| 36 | my $self = shift; | ||
| 37 | $self->database->get_collection($self->prefix . ".files"); | ||
| 38 | } | ||
| 39 | |||
| 40 | has chunks => ( | ||
| 41 | is => 'ro', | ||
| 42 | init_arg => undef, | ||
| 43 | lazy_build => 1, | ||
| 44 | ); | ||
| 45 | |||
| 46 | sub _build_chunks { | ||
| 47 | my $self = shift; | ||
| 48 | $self->database->get_collection($self->prefix . ".chunks"); | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub fetch { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | my %arg; | ||
| 56 | $arg{query} = shift if ref($_[0]); | ||
| 57 | $arg{cb} = shift if ref($_[0]); | ||
| 58 | |||
| 59 | my %options = (@_, %arg); | ||
| 60 | my $cb = $options{cb}; | ||
| 61 | $options{cb} = sub { | ||
| 62 | $cb->(map { GridFSFile->new(doc => $_, gridfs => $self) } @_); | ||
| 63 | }; | ||
| 64 | |||
| 65 | $self->files->find(%options); | ||
| 66 | } | ||
| 67 | |||
| 68 | __PACKAGE__->meta->make_immutable; |