| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Box::File; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 7 |  |  | 7 |  | 41 | use strict; | 
|  | 7 |  |  |  |  | 15 |  | 
|  | 7 |  |  |  |  | 264 |  | 
| 4 | 7 |  |  | 7 |  | 37 | use warnings; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 183 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 7 |  |  | 7 |  | 39 | use Moo; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 45 |  | 
| 7 | 7 |  |  | 7 |  | 8697 | use Sub::Identify qw(sub_name); | 
|  | 7 |  |  |  |  | 7144 |  | 
|  | 7 |  |  |  |  | 477 |  | 
| 8 | 7 |  |  | 7 |  | 119 | use Types::Standard qw(Str Int InstanceOf); | 
|  | 7 |  |  |  |  | 16 |  | 
|  | 7 |  |  |  |  | 85 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 7 |  |  | 7 |  | 9146 | use WebService::Box::Types::Library qw(BoxPerson Timestamp BoxFolderHash OptionalStr SharedLink); | 
|  | 7 |  |  |  |  | 25 |  | 
|  | 7 |  |  |  |  | 45 |  | 
| 11 | 7 |  |  | 7 |  | 9868 | use WebService::Box::Request; | 
|  | 7 |  |  |  |  | 27 |  | 
|  | 7 |  |  |  |  | 325 |  | 
| 12 | 7 |  |  | 7 |  | 4184 | use WebService::Box::Folder; | 
|  | 7 |  |  |  |  | 24 |  | 
|  | 7 |  |  |  |  | 8123 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = 0.01; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has session => (is => 'ro', isa => InstanceOf["WebService::Box::Session"], required => 1); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has [qw/type id name description structure/] => ( | 
| 19 |  |  |  |  |  |  | is        => 'ro', | 
| 20 |  |  |  |  |  |  | isa       => Str, | 
| 21 |  |  |  |  |  |  | predicate => 1, | 
| 22 |  |  |  |  |  |  | ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has [qw/etag sequence_id sha1 item_status version_number/] => ( | 
| 25 |  |  |  |  |  |  | is        => 'ro', | 
| 26 |  |  |  |  |  |  | isa       => OptionalStr, | 
| 27 |  |  |  |  |  |  | predicate => 1, | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | has [qw/size comment_count/] => ( | 
| 31 |  |  |  |  |  |  | is        => 'ro', | 
| 32 |  |  |  |  |  |  | isa       => Int, | 
| 33 |  |  |  |  |  |  | predicate => 1, | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | has [qw/created_by modified_by owned_by/] => ( | 
| 37 |  |  |  |  |  |  | is        => 'ro', | 
| 38 |  |  |  |  |  |  | isa       => BoxPerson, | 
| 39 |  |  |  |  |  |  | coerce    => BoxPerson()->coercion, | 
| 40 |  |  |  |  |  |  | predicate => 1, | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | has [qw/created_at modified_at trashed_at purged_at content_created_at content_modified_at/] => ( | 
| 44 |  |  |  |  |  |  | is        => 'ro', | 
| 45 |  |  |  |  |  |  | isa       => Timestamp, | 
| 46 |  |  |  |  |  |  | coerce    => Timestamp()->coercion, | 
| 47 |  |  |  |  |  |  | predicate => 1, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | has shared_link => ( | 
| 51 |  |  |  |  |  |  | is        => 'ro', | 
| 52 |  |  |  |  |  |  | isa       => SharedLink, | 
| 53 |  |  |  |  |  |  | coerce    => SharedLink()->coercion, | 
| 54 |  |  |  |  |  |  | predicate => 1, | 
| 55 |  |  |  |  |  |  | ); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | has parent_data => (is => 'ro', isa => BoxFolderHash); | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | has request => ( | 
| 60 |  |  |  |  |  |  | is      => 'ro', | 
| 61 |  |  |  |  |  |  | isa     => InstanceOf["WebService::Box::Request"], | 
| 62 |  |  |  |  |  |  | lazy    => 1, | 
| 63 |  |  |  |  |  |  | default => sub { | 
| 64 |  |  |  |  |  |  | my $self = shift; | 
| 65 |  |  |  |  |  |  | WebService::Box::Request->new( session => $self->session ) | 
| 66 |  |  |  |  |  |  | }, | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | has _parent_object => (is => 'rwp', isa => InstanceOf["WebService::Box::Folder"]); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | has error => (is => 'rwp', isa => Str); | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | around [qw( | 
| 74 |  |  |  |  |  |  | size comment_count created_by modified_by owned_by created_at modified_at trashed_at purged_at | 
| 75 |  |  |  |  |  |  | content_created_at content_modified_at type name description structure shared_link | 
| 76 |  |  |  |  |  |  | etag sequence_id sha1 item_status version_number | 
| 77 |  |  |  |  |  |  | )] => sub { | 
| 78 |  |  |  |  |  |  | my $orig = shift; | 
| 79 |  |  |  |  |  |  | my $self = shift; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | my $method_name = sub_name $orig; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | if ( !$self->id ) { | 
| 84 |  |  |  |  |  |  | $self->session->box->error( | 
| 85 |  |  |  |  |  |  | "invalid method call ($method_name): file id does not exist, create a new object with id" | 
| 86 |  |  |  |  |  |  | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | return; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | my $predicate   = $self->can( 'has_' . $method_name ); | 
| 92 |  |  |  |  |  |  | if ( !$self->$predicate() ) { | 
| 93 |  |  |  |  |  |  | $self = $_[1] = $self->rebuild; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | return $self->$orig(); | 
| 97 |  |  |  |  |  |  | }; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub rebuild { | 
| 100 | 5 |  |  | 5 | 0 | 259 | my ($self) = @_; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 5 |  |  |  |  | 86 | $self->_set_error(''); | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 5 | 100 |  |  |  | 1705 | if ( !$self->id ) { | 
| 105 | 1 |  |  |  |  | 12 | $self->session->box->error( 'cannot rebuild: file id does not exist' ); | 
| 106 | 0 |  |  |  |  | 0 | return; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 4 |  |  |  |  | 20 | my $parent_object = $self->_parent_object; | 
| 110 |  |  |  |  |  |  | my %file_data = $self->request->do( | 
| 111 |  |  |  |  |  |  | ressource => 'files', | 
| 112 |  |  |  |  |  |  | action    => 'get', | 
| 113 |  |  |  |  |  |  | id        => $self->id, | 
| 114 | 4 | 50 |  |  |  | 61 | ) or do { $self->_set_error( $self->request->error ); return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 4 |  |  |  |  | 7816 | $self = $_[0] = WebService::Box::File->new( %file_data, session => $self->session ); | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 4 | 50 | 33 |  |  | 499 | if ( $parent_object && $parent_object->id == $_[0]->parent_data->{id} ) { | 
| 119 | 0 |  |  |  |  | 0 | $_[0]->_set__parent_object( $parent_object ); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 4 |  |  |  |  | 18 | return $_[0]; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub parent { | 
| 126 | 2 |  |  | 2 | 0 | 212 | my ($self) = @_; | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 2 |  |  |  |  | 13 | my $data = $self->parent_data; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 2 | 50 |  |  |  | 10 | if ( !$data ) { | 
| 131 | 2 | 100 |  |  |  | 13 | if ( !$self->id ) { | 
| 132 | 1 |  |  |  |  | 15 | $self->session->box->error( 'no id for parent found and no file id exists' ); | 
| 133 | 1 |  |  |  |  | 9 | return; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 1 |  |  |  |  | 5 | $self = $_[0] = $self->rebuild; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 1 | 50 |  |  |  | 16 | if ( !$self->_parent_object ) { | 
| 140 | 1 |  |  |  |  | 4 | my $data = $self->parent_data; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 1 |  |  |  |  | 12 | $self->_set__parent_object( | 
| 143 | 1 |  |  |  |  | 2 | WebService::Box::Folder->new( %{$data}, session => $self->session ) | 
| 144 |  |  |  |  |  |  | ); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 1 |  |  |  |  | 913 | return $self->_parent_object; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub upload { | 
| 151 | 0 |  |  | 0 | 0 | 0 | my ($self, $path, $parent) = @_; | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 0 |  |  |  |  | 0 | $self->_set_error(''); | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 | 0 |  |  |  | 0 | my $parent_id = ref $parent ? $parent->id : $parent; | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 0 | 0 |  |  |  | 0 | if ( !-e $path ) { | 
| 158 | 0 |  |  |  |  | 0 | $self->session->box->error( 'the file requested to upload does not exist' ); | 
| 159 | 0 |  |  |  |  | 0 | return; | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 0 |  |  |  |  | 0 | my $content = do{ local (@ARGV,$/) = $path; <> }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | my %upload_data = $self->request->do( | 
| 165 |  |  |  |  |  |  | ressource => 'files', | 
| 166 |  |  |  |  |  |  | action    => 'upload', | 
| 167 |  |  |  |  |  |  | file      => { filename => $path, content => $content }, | 
| 168 |  |  |  |  |  |  | parent_id => $parent_id, | 
| 169 | 0 | 0 |  |  |  | 0 | ) or do { $self->_set_error( $self->request->error ); return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 0 |  |  |  |  | 0 | my $uploaded_file = WebService::Box::File->new( %upload_data, session => $self->session ); | 
| 172 | 0 |  |  |  |  | 0 | return $uploaded_file; | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 0 |  |  | 0 | 0 | 0 | sub download { | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 0 |  |  | 0 | 0 | 0 | sub comment { | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 0 |  |  | 0 | 0 | 0 | sub comments { | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | sub BUILDARGS { | 
| 185 | 14 |  |  | 14 | 0 | 87893 | my ( $class, @args ) = @_; | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 14 | 50 |  |  |  | 82 | unshift @args, "id" if @args % 2 == 1; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 14 |  |  |  |  | 366 | return { @args }; | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | 1; | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | __END__ |