| 1 | package Net::Google::DocumentsList::Item; | ||
| 2 | use Any::Moose; | ||
| 3 | use Net::Google::DataAPI; | ||
| 4 | with 'Net::Google::DataAPI::Role::Entry', | ||
| 5 | 'Net::Google::DocumentsList::Role::EnsureListed'; | ||
| 6 | use XML::Atom::Util qw(nodelist first); | ||
| 7 | use Carp; | ||
| 8 | use URI::Escape; | ||
| 9 | use File::stat; | ||
| 10 | |||
| 11 | feedurl item => ( | ||
| 12 | is => 'ro', | ||
| 13 | as_content_src => 1, | ||
| 14 | entry_class => 'Net::Google::DocumentsList::Item', | ||
| 15 | ); | ||
| 16 | |||
| 17 | entry_has 'kind' => ( | ||
| 18 | is => 'ro', | ||
| 19 | from_atom => sub { | ||
| 20 | my ($self, $atom) = @_; | ||
| 21 | my ($kind) = | ||
| 22 | map {$_->label} | ||
| 23 | grep {$_->scheme eq 'http://schemas.google.com/g/2005#kind'} | ||
| 24 | $atom->categories; | ||
| 25 | return $kind; | ||
| 26 | }, | ||
| 27 | to_atom => sub { | ||
| 28 | my ($self, $atom) = @_; | ||
| 29 | my $cat = XML::Atom::Category->new; | ||
| 30 | $cat->scheme('http://schemas.google.com/g/2005#kind'); | ||
| 31 | $cat->label($self->kind); | ||
| 32 | $cat->term(join("#", "http://schemas.google.com/docs/2007", $self->kind)); | ||
| 33 | $atom->category($cat); | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | |||
| 37 | with 'Net::Google::DocumentsList::Role::HasItems', | ||
| 38 | 'Net::Google::DocumentsList::Role::Exportable'; | ||
| 39 | |||
| 40 | for my $label (qw(starred viewed hidden mine private trashed)) { | ||
| 41 | entry_has $label => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Bool', | ||
| 44 | from_atom => sub { | ||
| 45 | my ($self, $atom) = @_; | ||
| 46 | grep { | ||
| 47 | ($_->scheme eq 'http://schemas.google.com/g/2005/labels') | ||
| 48 | && ($_->label eq $label) | ||
| 49 | } $atom->categories; | ||
| 50 | }, | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | feedurl 'acl' => ( | ||
| 55 | from_atom => sub { | ||
| 56 | my ($self, $atom) = @_; | ||
| 57 | return $self->_get_feedlink('http://schemas.google.com/acl/2007#accessControlList'); | ||
| 58 | }, | ||
| 59 | entry_class => 'Net::Google::DocumentsList::ACL', | ||
| 60 | ); | ||
| 61 | |||
| 62 | feedurl 'revision' => ( | ||
| 63 | from_atom => sub { | ||
| 64 | my ($self, $atom) = @_; | ||
| 65 | return $self->_get_feedlink('http://schemas.google.com/docs/2007/revisions'); | ||
| 66 | }, | ||
| 67 | entry_class => 'Net::Google::DocumentsList::Revision', | ||
| 68 | can_add => 0, | ||
| 69 | ); | ||
| 70 | |||
| 71 | entry_has 'published' => ( tagname => 'published', is => 'ro' ); | ||
| 72 | entry_has 'updated' => ( tagname => 'updated', is => 'ro' ); | ||
| 73 | entry_has 'edited' => ( tagname => 'edited', ns => 'app', is => 'ro' ); | ||
| 74 | entry_has 'resource_id' => ( tagname => 'resourceId', ns => 'gd', is => 'ro' ); | ||
| 75 | entry_has 'last_viewd' => ( tagname => 'lastViewed', ns => 'gd', is => 'ro' ); | ||
| 76 | entry_has 'deleted' => ( | ||
| 77 | is => 'ro', | ||
| 78 | isa => 'Bool', | ||
| 79 | from_atom => sub { | ||
| 80 | my ($self, $atom) = @_; | ||
| 81 | first($atom->elem, $self->ns('gd')->{uri}, 'deleted') ? 1 : 0; | ||
| 82 | }, | ||
| 83 | ); | ||
| 84 | entry_has 'parent' => ( | ||
| 85 | is => 'ro', | ||
| 86 | isa => 'Str', | ||
| 87 | from_atom => sub { | ||
| 88 | my ($self, $atom) = @_; | ||
| 89 | $self->container or return; | ||
| 90 | my ($parent) = | ||
| 91 | grep {$_ eq $self->container->_url_with_resource_id} | ||
| 92 | map {$_->href} | ||
| 93 | grep {$_->rel eq 'http://schemas.google.com/docs/2007#parent'} | ||
| 94 | $atom->link; | ||
| 95 | $parent; | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | |||
| 99 | entry_has 'alternate' => ( | ||
| 100 | is => 'ro', | ||
| 101 | isa => 'Str', | ||
| 102 | from_atom => sub { | ||
| 103 | my ($self, $atom) = @_; | ||
| 104 | my ($alt) = | ||
| 105 | map {$_->href} | ||
| 106 | grep {$_->rel eq 'alternate' && $_->type eq 'text/html'} | ||
| 107 | $atom->link; | ||
| 108 | return $alt; | ||
| 109 | } | ||
| 110 | ); | ||
| 111 | |||
| 112 | entry_has 'resumable_edit_media' => ( | ||
| 113 | is => 'ro', | ||
| 114 | isa => 'Str', | ||
| 115 | from_atom => sub { | ||
| 116 | my ($self, $atom) = @_; | ||
| 117 | my ($link) = | ||
| 118 | map {$_->href} | ||
| 119 | grep {$_->rel eq 'http://schemas.google.com/g/2005#resumable-edit-media'} | ||
| 120 | $atom->link; | ||
| 121 | return $link; | ||
| 122 | } | ||
| 123 | ); | ||
| 124 | |||
| 125 | sub _url_with_resource_id { | ||
| 126 | my ($self) = @_; | ||
| 127 | join('/', $self->service->item_feedurl, uri_escape $self->resource_id); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _get_feedlink { | ||
| 131 | my ($self, $rel) = @_; | ||
| 132 | my ($feedurl) = | ||
| 133 | map {$_->getAttribute('href')} | ||
| 134 | grep {$_->getAttribute('rel') eq $rel} | ||
| 135 | nodelist($self->elem, $self->ns('gd')->{uri}, 'feedLink'); | ||
| 136 | return $feedurl; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub update_content { | ||
| 140 | my ($self, $file) = @_; | ||
| 141 | |||
| 142 | $self->kind eq 'folder' | ||
| 143 | and confess "You can't update folder content with a file"; | ||
| 144 | -r $file or confess "File $file does not exist"; | ||
| 145 | my $stat = stat($file) or confess "can not stat file $file"; | ||
| 146 | my $size = $stat->size; | ||
| 147 | my $ct = MIME::Types->new->mimeTypeOf($file)->type || 'application/octet-stream'; | ||
| 148 | open my $fh, '<:bytes', $file or confess "file $file could not be opened"; | ||
| 149 | |||
| 150 | $self->sync; | ||
| 151 | my $res = $self->service->request( | ||
| 152 | { | ||
| 153 | method => 'PUT', | ||
| 154 | uri => $self->resumable_edit_media, | ||
| 155 | content_type => $ct, | ||
| 156 | header => { | ||
| 157 | 'Content-Length' => 0, | ||
| 158 | 'X-Upload-Content-Type' => $ct, | ||
| 159 | 'X-Upload-Content-Length' => $size, | ||
| 160 | 'If-Match' => $self->etag, | ||
| 161 | }, | ||
| 162 | } | ||
| 163 | ); | ||
| 164 | my $atom; | ||
| 165 | if ($res->is_success) { | ||
| 166 | my $uri = $res->header('Location'); | ||
| 167 | my $offset = 0; | ||
| 168 | while (my $length = read $fh, my $part, 512*1024) { | ||
| 169 | my $req = HTTP::Request->new(PUT => $uri); | ||
| 170 | $req->content_type($ct); | ||
| 171 | $req->content_length($length); | ||
| 172 | $req->header('Content-Range' => sprintf('bytes %d-%d/%d', $offset, $offset + $length - 1, $size)); | ||
| 173 | $req->content($part); | ||
| 174 | my $res = $self->service->request($req); | ||
| 175 | if ($res->code == 200) { | ||
| 176 | $atom = XML::Atom::Entry->new(\($res->content)); | ||
| 177 | last; | ||
| 178 | } else { | ||
| 179 | if (my $next = $res->header('Location')) { | ||
| 180 | $uri = $next; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | $self->container->sync if $self->container; | ||
| 184 | $offset = $offset + $length; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | my $updated = $self->atom($atom); | ||
| 188 | $self->container->sync if $self->container; | ||
| 189 | return $updated; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub move_to { | ||
| 193 | my ($self, $dest) = @_; | ||
| 194 | |||
| 195 | ( | ||
| 196 | ref($dest) eq 'Net::Google::DocumentsList::Item' | ||
| 197 | && $dest->kind eq 'folder' | ||
| 198 | ) or confess 'destination should be a folder'; | ||
| 199 | |||
| 200 | my $atom = $self->service->request( | ||
| 201 | { | ||
| 202 | method => 'POST', | ||
| 203 | content_type => 'application/atom+xml', | ||
| 204 | uri => $dest->item_feedurl, | ||
| 205 | content => $self->atom->as_xml, | ||
| 206 | response_object => 'XML::Atom::Entry', | ||
| 207 | } | ||
| 208 | ); | ||
| 209 | my $item = (ref $self)->new( | ||
| 210 | container => $dest, | ||
| 211 | atom => $atom, | ||
| 212 | ); | ||
| 213 | my $updated = $dest->ensure_listed($item); | ||
| 214 | $self->container->sync if $self->container; | ||
| 215 | $dest->sync; | ||
| 216 | $self->atom($updated->atom); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub move_out_of { | ||
| 220 | my ($self, $folder) = @_; | ||
| 221 | |||
| 222 | ( | ||
| 223 | ref($folder) eq 'Net::Google::DocumentsList::Item' | ||
| 224 | && $folder->kind eq 'folder' | ||
| 225 | ) or confess 'the argument should be a folder'; | ||
| 226 | |||
| 227 | my $res = $self->service->request( | ||
| 228 | { | ||
| 229 | method => 'DELETE', | ||
| 230 | uri => join('/', $folder->item_feedurl, $self->resource_id), | ||
| 231 | header => {'If-Match' => $self->etag}, | ||
| 232 | } | ||
| 233 | ); | ||
| 234 | if ($res->is_success) { | ||
| 235 | $self->ensure_not_listed($folder); | ||
| 236 | $self->container->sync if $self->container; | ||
| 237 | $folder->sync; | ||
| 238 | $self->sync; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub copy { | ||
| 243 | my ($self, $new_title) = @_; | ||
| 244 | |||
| 245 | $new_title or confess 'new title not specified'; | ||
| 246 | grep {$_ eq $self->kind} qw(document spreadsheet presentation) | ||
| 247 | or confess 'This kind of item can not be copied'; | ||
| 248 | |||
| 249 | my $target = (ref $self)->new( | ||
| 250 | { | ||
| 251 | service => $self->service, | ||
| 252 | title => $new_title, | ||
| 253 | } | ||
| 254 | )->to_atom; | ||
| 255 | $target->id($self->id); | ||
| 256 | |||
| 257 | my $atom = $self->service->request( | ||
| 258 | { | ||
| 259 | method => 'POST', | ||
| 260 | content_type => 'application/atom+xml', | ||
| 261 | uri => $self->service->item_feedurl, | ||
| 262 | content => $target->as_xml, | ||
| 263 | response_object => 'XML::Atom::Entry', | ||
| 264 | } | ||
| 265 | ); | ||
| 266 | my $item = (ref $self)->new( | ||
| 267 | service => $self->service, | ||
| 268 | atom => $atom, | ||
| 269 | ); | ||
| 270 | my $updated = $self->service->ensure_listed($item); | ||
| 271 | $self->container->sync if $self->container; | ||
| 272 | return $updated; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub update { | ||
| 276 | my ($self) = @_; | ||
| 277 | $self->etag or return; | ||
| 278 | my $parent = $self->container || $self->service; | ||
| 279 | my $atom = $self->service->put( | ||
| 280 | { | ||
| 281 | self => $self, | ||
| 282 | entry => $self->to_atom, | ||
| 283 | } | ||
| 284 | ); | ||
| 285 | my $item = (ref $self)->new( | ||
| 286 | $self->container ? (container => $self->container) | ||
| 287 | : ( service => $self->service), | ||
| 288 | atom => $atom | ||
| 289 | ); | ||
| 290 | my $updated = $parent->ensure_listed($item); | ||
| 291 | $self->container->sync if $self->container; | ||
| 292 | $self->atom($updated->atom); | ||
| 293 | } | ||
| 294 | |||
| 295 | sub delete { | ||
| 296 | my ($self, $args) = @_; | ||
| 297 | |||
| 298 | $self->sync; | ||
| 299 | my $parent = $self->container || $self->service; | ||
| 300 | |||
| 301 | my $selfurl = $self->container ? $self->_url_with_resource_id : $self->selfurl; | ||
| 302 | |||
| 303 | $args->{delete} = 'true' if $args->{delete}; | ||
| 304 | my $res = $self->service->request( | ||
| 305 | { | ||
| 306 | uri => $selfurl, | ||
| 307 | method => 'DELETE', | ||
| 308 | header => {'If-Match' => $self->etag}, | ||
| 309 | self => $self, | ||
| 310 | query => $args, | ||
| 311 | } | ||
| 312 | ); | ||
| 313 | $res->is_success or return; | ||
| 314 | if ($args->{delete}) { | ||
| 315 | $parent->ensure_deleted($self); | ||
| 316 | } else { | ||
| 317 | $parent->ensure_trashed($self); | ||
| 318 | } | ||
| 319 | return 1; | ||
| 320 | } | ||
| 321 | |||
| 322 | around 'add_acl' => sub { | ||
| 323 | my ($next, $self, $args) = @_; | ||
| 324 | if ((delete($args->{send_notification_emails}) || '') eq 'false') { | ||
| 325 | my $feedurl = $self->acl_feedurl . '?send-notification-emails=false'; | ||
| 326 | my $class = $self->acl_entryclass; | ||
| 327 | Any::Moose::load_class($class); | ||
| 328 | my $entry = $class->new( | ||
| 329 | container => $self, | ||
| 330 | %$args | ||
| 331 | )->to_atom; | ||
| 332 | my $atom = $self->service->post($feedurl, $entry); | ||
| 333 | $self->sync if $self->can('sync'); | ||
| 334 | return $class->new( | ||
| 335 | container => $self, | ||
| 336 | atom => $atom, | ||
| 337 | ); | ||
| 338 | } else { | ||
| 339 | return $next->($self, $args); | ||
| 340 | } | ||
| 341 | }; | ||
| 342 | |||
| 343 | __PACKAGE__->meta->make_immutable; | ||
| 344 | |||
| 345 | no Any::Moose; | ||
| 346 | |||
| 347 | 1; | ||
| 348 | __END__ |