| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Net::Google::Drive::Simple::V2; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use parent qw< Net::Google::Drive::Simple::Core >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use HTTP::Request (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::Basename qw( basename ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use JSON qw( from_json to_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | our $VERSION = '3.02'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ########################################### | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | ########################################### | ||||||||||||||||||||||||||||
| 22 | 2 | my ( $class, %options ) = @_; | |||||||||||||||||||||||||||
| 23 | 2 | return $class->SUPER::new( | |||||||||||||||||||||||||||
| 24 | %options, | ||||||||||||||||||||||||||||
| 25 | api_file_url => 'https://www.googleapis.com/drive/v2/files', | ||||||||||||||||||||||||||||
| 26 | api_upload_url => 'https://www.googleapis.com/upload/drive/v2/files', | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | ########################################### | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub files { | ||||||||||||||||||||||||||
|
Subroutine
files: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | ########################################### | ||||||||||||||||||||||||||||
| 33 | 0 | my ( $self, $opts, $search_opts ) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( !defined $search_opts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $search_opts = {}; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | $search_opts = { | ||||||||||||||||||||||||||||
| 39 | 0 | page => 1, | |||||||||||||||||||||||||||
| 40 | %$search_opts, | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ( !defined $opts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | $opts = {}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $self->init(); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( my $title = $search_opts->{title} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $title =~ s|\'|\\\'|g; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( defined $opts->{q} && length $opts->{q} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $opts->{"q"} and length $opts->{"q"}
MC/DC0%0/2
defined $opts->{"q"} and length $opts->{"q"}
defined $opts->{"q"}
length $opts->{"q"}
Truth table0%0/3
A and B
A= defined $opts->{"q"}B= length $opts->{"q"}
| |||||||||||||||||||||||||||||
| 52 | 0 | $opts->{q} .= ' AND '; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | $opts->{q} .= "title = '$title'"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my @docs = (); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | while (1) { | |||||||||||||||||||||||||||
| 61 | 0 | my $url = $self->file_url($opts); | |||||||||||||||||||||||||||
| 62 | 0 | my $data = $self->http_json($url); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | my $next_item = $self->item_iterator($data); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | while ( my $item = $next_item->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $item->{kind} eq "drive#file" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $file = $item->{originalFilename}; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( !defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | DEBUG "Skipping $item->{ title } (no originalFilename)"; | |||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | push @docs, $self->data_factory($item); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 0 | DEBUG "Skipping $item->{ title } ($item->{ kind })"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( $search_opts->{page} and $data->{nextPageToken} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$search_opts->{"page"} and $data->{"nextPageToken"}
MC/DC0%0/2
$search_opts->{"page"} and $data->{"nextPageToken"}
$search_opts->{"page"}
$data->{"nextPageToken"}
Truth table0%0/3
A and B
A= $search_opts->{"page"}B= $data->{"nextPageToken"}
| |||||||||||||||||||||||||||||
| 82 | 0 | $opts->{pageToken} = $data->{nextPageToken}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | 0 | last; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | return \@docs; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ########################################### | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub folder_create { | ||||||||||||||||||||||||||
|
Subroutine
folder_create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | ########################################### | ||||||||||||||||||||||||||||
| 95 | 0 | my ( $self, $title, $parent ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return $self->file_create( $title, "application/vnd.google-apps.folder", $parent ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ########################################### | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub file_create { | ||||||||||||||||||||||||||
|
Subroutine
file_create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | ########################################### | ||||||||||||||||||||||||||||
| 103 | 0 | my ( $self, $title, $mime_type, $parent ) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $url = URI->new( $self->{api_file_url} ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $data = $self->http_json( | |||||||||||||||||||||||||||
| 108 | $url, | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | title => $title, | ||||||||||||||||||||||||||||
| 111 | parents => [ { id => $parent } ], | ||||||||||||||||||||||||||||
| 112 | mimeType => $mime_type, | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $data->{id}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | ########################################### | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub file_upload { | ||||||||||||||||||||||||||
|
Subroutine
file_upload: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | ########################################### | ||||||||||||||||||||||||||||
| 124 | 0 | my ( $self, $file, $parent_id, $file_id, $opts ) = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $opts = {} if !defined $opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Since a file upload can take a long time, refresh the token | ||||||||||||||||||||||||||||
| 129 | # just in case. | ||||||||||||||||||||||||||||
| 130 | 0 | $self->{oauth}->token_expire(); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $title = basename $file; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # First, insert the file placeholder, according to | ||||||||||||||||||||||||||||
| 135 | # http://stackoverflow.com/questions/10317638 | ||||||||||||||||||||||||||||
| 136 | 0 | my $mime_type = $self->file_mime_type($file); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my $url; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ( !defined $file_id ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $url = URI->new( $self->{api_file_url} ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | my $data = $self->http_json( | ||||||||||||||||||||||||||||
| 144 | $url, | ||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | mimeType => $mime_type, | ||||||||||||||||||||||||||||
| 147 | parents => [ { id => $parent_id } ], | ||||||||||||||||||||||||||||
| 148 | title => $opts->{title} ? $opts->{title} : $title, | ||||||||||||||||||||||||||||
| 149 | description => $opts->{description}, | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | $file_id = $data->{id}; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $url = URI->new( $self->{api_upload_url} . "/$file_id" ); | |||||||||||||||||||||||||||
| 159 | 0 | $url->query_form( uploadType => "media" ); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | my $file_length = -s $file; | |||||||||||||||||||||||||||
| 162 | 0 | my $file_data = $self->_content_sub($file); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | $self->http_put( | ||||||||||||||||||||||||||||
| 166 | $url, | ||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 'Content-Type' => $mime_type, | ||||||||||||||||||||||||||||
| 169 | 'Content' => $file_data, | ||||||||||||||||||||||||||||
| 170 | 'Content-Length' => $file_length | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | ) | ||||||||||||||||||||||||||||
| 173 | ) { | ||||||||||||||||||||||||||||
| 174 | 0 | return $file_id; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | ########################################### | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub rename { | ||||||||||||||||||||||||||
|
Subroutine
rename: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | ########################################### | ||||||||||||||||||||||||||||
| 181 | 0 | my ( $self, $file_id, $new_name ) = @_; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | my $url = URI->new( $self->{api_file_url} . "/$file_id" ); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | $self->http_put( | ||||||||||||||||||||||||||||
| 187 | $url, | ||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||
| 189 | "Accept" => "application/json", | ||||||||||||||||||||||||||||
| 190 | "Content-Type" => "application/json", | ||||||||||||||||||||||||||||
| 191 | Content => to_json( { title => $new_name } ), | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | ) | ||||||||||||||||||||||||||||
| 194 | ) { | ||||||||||||||||||||||||||||
| 195 | 0 | return 1; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | return; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | ########################################### | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub http_put { | ||||||||||||||||||||||||||
|
Subroutine
http_put: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | ########################################### | ||||||||||||||||||||||||||||
| 204 | 0 | my ( $self, $url, $params ) = @_; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | my $content = delete $params->{Content}; | |||||||||||||||||||||||||||
| 207 | my $req = HTTP::Request->new( | ||||||||||||||||||||||||||||
| 208 | 'PUT', | ||||||||||||||||||||||||||||
| 209 | $url->as_string, | ||||||||||||||||||||||||||||
| 210 | 0 | [ $self->{oauth}->authorization_headers(), %$params ], | |||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # $content can be a string or a CODE ref. For example rename() calls us with a string, but | ||||||||||||||||||||||||||||
| 214 | # file_upload() calls us with a CODE ref. The HTTP::Request::new() only accepts a string, | ||||||||||||||||||||||||||||
| 215 | # so we set the content of the request after calling the constructor. | ||||||||||||||||||||||||||||
| 216 | 0 | $req->content($content); | |||||||||||||||||||||||||||
| 217 | 0 | my $resp = $self->http_loop($req); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if ( $resp->is_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $self->error( $resp->message() ); | |||||||||||||||||||||||||||
| 221 | 0 | return; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | 0 | DEBUG $resp->as_string; | |||||||||||||||||||||||||||
| 224 | 0 | return $resp; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | ########################################### | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub file_mvdir { | ||||||||||||||||||||||||||
|
Subroutine
file_mvdir: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | ########################################### | ||||||||||||||||||||||||||||
| 230 | 0 | my ( $self, $path, $target_folder ) = @_; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | my $url; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | ▶ | if ( !defined $path or !defined $target_folder ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and defined $target_folder
MC/DC0%0/2
defined $path and defined $target_folder
defined $path
defined $target_folder
Truth table0%0/3
A and B
A= defined $pathB= defined $target_folder
| |||||||||||||||||||||||||||||
| 235 | 0 | LOGDIE "Missing parameter"; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Determine the file's parent in the path | ||||||||||||||||||||||||||||
| 239 | 0 | my ( $file_id, $folder_id ) = $self->path_resolve($path); | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if ( !defined $file_id ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 0 | LOGDIE "Cannot find source file: $path"; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | my ($target_folder_id) = $self->path_resolve($target_folder); | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ( !defined $target_folder_id ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 0 | LOGDIE "Cannot find destination path: $target_folder"; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | print "file_id=$file_id\n"; | |||||||||||||||||||||||||||
| 252 | 0 | print "folder_id=$folder_id\n"; | |||||||||||||||||||||||||||
| 253 | 0 | print "target_folder_id=$target_folder_id\n"; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | # Delete it from the current parent | ||||||||||||||||||||||||||||
| 256 | 0 | $url = URI->new( $self->{api_file_url} . "/$folder_id/children/$file_id" ); | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ( !$self->http_delete($url) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | LOGDIE "Failed to remove $path from parent folder."; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Add a new parent | ||||||||||||||||||||||||||||
| 262 | 0 | $url = URI->new( $self->{api_file_url} . "/$target_folder_id/children" ); | |||||||||||||||||||||||||||
| 263 | 0 | ▶ | if ( !$self->http_json( $url, { id => $file_id } ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | LOGDIE "Failed to insert $path into $target_folder."; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | return 1; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | ########################################### | ||||||||||||||||||||||||||||
| 271 | 0 | ▶ | sub path_resolve { | ||||||||||||||||||||||||||
|
Subroutine
path_resolve: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 272 | ########################################### | ||||||||||||||||||||||||||||
| 273 | 0 | my ( $self, $path, $search_opts ) = @_; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | $search_opts = {} if !defined $search_opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | my @parts = grep { $_ ne '' } split '/', $path; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 0 | my @ids = qw(root); | |||||||||||||||||||||||||||
| 280 | 0 | my $folder_id = my $parent = "root"; | |||||||||||||||||||||||||||
| 281 | 0 | DEBUG "Parent: $parent"; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 0 | PART: for my $part (@parts) { | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | DEBUG "Looking up part $part (folder_id=$folder_id)"; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | my $children = $self->children_by_folder_id( | |||||||||||||||||||||||||||
| 288 | $folder_id, | ||||||||||||||||||||||||||||
| 289 | { | ||||||||||||||||||||||||||||
| 290 | maxResults => 100, # path resolution maxResults is different | ||||||||||||||||||||||||||||
| 291 | }, | ||||||||||||||||||||||||||||
| 292 | { %$search_opts, title => $part }, | ||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | return unless defined $children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | for my $child (@$children) { | |||||||||||||||||||||||||||
| 298 | 0 | DEBUG "Found child ", $child->title(); | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | if ( $child->title() eq $part ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | $folder_id = $child->id(); | |||||||||||||||||||||||||||
| 301 | 0 | unshift @ids, $folder_id; | |||||||||||||||||||||||||||
| 302 | 0 | $parent = $folder_id; | |||||||||||||||||||||||||||
| 303 | 0 | DEBUG "Parent: $parent"; | |||||||||||||||||||||||||||
| 304 | 0 | next PART; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 0 | my $msg = "Child $part not found"; | |||||||||||||||||||||||||||
| 309 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 310 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 311 | 0 | return; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | if ( @ids == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # parent of root is root | ||||||||||||||||||||||||||||
| 317 | 0 | return ( @ids, @ids ); | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | return (@ids); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | ########################################### | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | sub file_delete { | ||||||||||||||||||||||||||
|
Subroutine
file_delete: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | ########################################### | ||||||||||||||||||||||||||||
| 326 | 0 | my ( $self, $file_id ) = @_; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | my $url; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 0 | ▶ | LOGDIE 'Deletion requires file_id' if ( !defined $file_id ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | $url = URI->new( $self->{api_file_url} . "/$file_id" ); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | ▶ | if ( $self->http_delete($url) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 0 | return $file_id; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 0 | return; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | ########################################### | ||||||||||||||||||||||||||||
| 342 | 0 | ▶ | sub http_delete { | ||||||||||||||||||||||||||
|
Subroutine
http_delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | ########################################### | ||||||||||||||||||||||||||||
| 344 | 0 | my ( $self, $url ) = @_; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | my $req = HTTP::Request->new( | ||||||||||||||||||||||||||||
| 347 | 'DELETE', | ||||||||||||||||||||||||||||
| 348 | $url, | ||||||||||||||||||||||||||||
| 349 | 0 | [ $self->{oauth}->authorization_headers() ], | |||||||||||||||||||||||||||
| 350 | ); | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 0 | my $resp = $self->http_loop($req); | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | DEBUG $resp->as_string; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | ▶ | if ( $resp->is_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 0 | $self->error( $resp->message() ); | |||||||||||||||||||||||||||
| 358 | 0 | return; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 0 | return 1; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | ########################################### | ||||||||||||||||||||||||||||
| 365 | 1 | ▶ | sub children_by_folder_id { | ||||||||||||||||||||||||||
|
Subroutine
children_by_folder_id: called CC=9 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||
| 366 | ########################################### | ||||||||||||||||||||||||||||
| 367 | 1 | my ( $self, $folder_id, $opts, $search_opts ) = @_; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 1 | $self->init(); | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 1 | ▶ | $search_opts = {} unless defined $search_opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 1 | ▶ | $search_opts->{page} = 1 unless exists $search_opts->{page}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 1 | ▶ | if ( !defined $opts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 1 | $opts = { | |||||||||||||||||||||||||||
| 376 | maxResults => 100, | ||||||||||||||||||||||||||||
| 377 | }; | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 1 | my $url = URI->new( $self->{api_file_url} ); | |||||||||||||||||||||||||||
| 381 | 1 | $opts->{'q'} = "'$folder_id' in parents"; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 1 | ▶ | if ( my $title = $search_opts->{title} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | $title =~ s|\'|\\\'|g; | |||||||||||||||||||||||||||
| 385 | 0 | $opts->{q} .= " AND title = '$title'"; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 1 | my @children = (); | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 1 | while (1) { | |||||||||||||||||||||||||||
| 391 | 1 | $url->query_form($opts); | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 1 | my $data = $self->http_json($url); | |||||||||||||||||||||||||||
| 394 | 1 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 1 | my $next_item = $self->item_iterator($data); | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 1 | ▶ | while ( my $item = $next_item->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 2 | push @children, $self->data_factory($item); | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 1 | ▶ | if ( $search_opts->{page} and $data->{nextPageToken} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$search_opts->{"page"} and $data->{"nextPageToken"}
MC/DC0%0/2
$search_opts->{"page"} and $data->{"nextPageToken"}
$search_opts->{"page"}
$data->{"nextPageToken"}
Truth table33%1/3
A and B
A= $search_opts->{"page"}B= $data->{"nextPageToken"}
| |||||||||||||||||||||||||||||
| 403 | 0 | $opts->{pageToken} = $data->{nextPageToken}; | |||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | else { | ||||||||||||||||||||||||||||
| 406 | 1 | last; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 1 | return \@children; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | ########################################### | ||||||||||||||||||||||||||||
| 414 | 1 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 415 | ########################################### | ||||||||||||||||||||||||||||
| 416 | 1 | my ( $self, $path, $opts, $search_opts ) = @_; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 1 | DEBUG "Determine children of $path"; | |||||||||||||||||||||||||||
| 419 | 1 | ▶ | LOGDIE "No $path given" unless defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 1 | ▶ | $search_opts = {} unless defined $search_opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 1 | my ( $folder_id, $parent ) = $self->path_resolve( $path, $search_opts ); | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 1 | ▶ | return unless defined $folder_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 1 | DEBUG "Getting content of folder $folder_id"; | |||||||||||||||||||||||||||
| 428 | 1 | my $children = $self->children_by_folder_id( | |||||||||||||||||||||||||||
| 429 | $folder_id, $opts, | ||||||||||||||||||||||||||||
| 430 | $search_opts | ||||||||||||||||||||||||||||
| 431 | ); | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 1 | ▶ | return unless defined $children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 1 | ▶ | return wantarray ? ( $children, $folder_id ) : $children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | ########################################### | ||||||||||||||||||||||||||||
| 439 | 0 | ▶ | sub search { | ||||||||||||||||||||||||||
|
Subroutine
search: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 440 | ########################################### | ||||||||||||||||||||||||||||
| 441 | 0 | my ( $self, $opts, $search_opts, $query ) = @_; | |||||||||||||||||||||||||||
| 442 | 0 | ▶ | $search_opts ||= { page => 1 }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$search_opts ||= {"page", 1}
MC/DC0%0/1
$search_opts ||= {"page", 1}
$search_opts
Truth table0%0/2
A
A= $search_opts
| |||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 0 | $self->init(); | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if ( !defined $opts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | 0 | $opts = { | |||||||||||||||||||||||||||
| 448 | maxResults => 100, | ||||||||||||||||||||||||||||
| 449 | }; | ||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 0 | my $url = URI->new( $self->{api_file_url} ); | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | $opts->{'q'} = $query; | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 0 | my @children = (); | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | while (1) { | |||||||||||||||||||||||||||
| 459 | 0 | $url->query_form($opts); | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 0 | my $data = $self->http_json($url); | |||||||||||||||||||||||||||
| 462 | 0 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 0 | my $next_item = $self->item_iterator($data); | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | ▶ | while ( my $item = $next_item->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | 0 | push @children, $self->data_factory($item); | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | ▶ | if ( $search_opts->{page} and $data->{nextPageToken} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$search_opts->{"page"} and $data->{"nextPageToken"}
MC/DC0%0/2
$search_opts->{"page"} and $data->{"nextPageToken"}
$search_opts->{"page"}
$data->{"nextPageToken"}
Truth table0%0/3
A and B
A= $search_opts->{"page"}B= $data->{"nextPageToken"}
| |||||||||||||||||||||||||||||
| 471 | 0 | $opts->{pageToken} = $data->{nextPageToken}; | |||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | else { | ||||||||||||||||||||||||||||
| 474 | 0 | last; | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 0 | return \@children; | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | ########################################### | ||||||||||||||||||||||||||||
| 482 | 0 | ▶ | sub download { | ||||||||||||||||||||||||||
|
Subroutine
download: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 483 | ########################################### | ||||||||||||||||||||||||||||
| 484 | 0 | my ( $self, $url, $local_file ) = @_; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 0 | $self->init(); | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 0 | ▶ | if ( ref $url ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | 0 | $url = $url->downloadUrl(); | |||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||
| 493 | GET => $url, | ||||||||||||||||||||||||||||
| 494 | ); | ||||||||||||||||||||||||||||
| 495 | 0 | $req->header( $self->{oauth}->authorization_headers() ); | |||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 498 | 0 | my $resp = $ua->request( $req, $local_file ); | |||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 0 | ▶ | if ( $resp->is_error() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | 0 | my $msg = "Can't download $url (" . $resp->message() . ")"; | |||||||||||||||||||||||||||
| 502 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 503 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 504 | 0 | return; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 0 | ▶ | if ($local_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | 0 | return 1; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 0 | return $resp->content(); | |||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | 3 | 1; | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head2 METHODS | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =over 4 | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | =item C<new> | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | my $gd_v2 = Net::Google::Drive::Simple::V2->new(); | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | # same as: | ||||||||||||||||||||||||||||
| 525 | my $gd_v2 = Net::Google::Drive::Simple->new( 'version' => 2 ); | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | # same as: | ||||||||||||||||||||||||||||
| 528 | my $gd_v2 = Net::Google::Drive::Simple->new(); | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | =item C<my $children = $gd-E<gt>children( "/path/to" )> | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | Return the entries under a given path on the Google Drive as a reference | ||||||||||||||||||||||||||||
| 533 | to an array. Each entry | ||||||||||||||||||||||||||||
| 534 | is an object composed of the JSON data returned by the Google Drive API. | ||||||||||||||||||||||||||||
| 535 | Each object offers methods named like the fields in the JSON data, e.g. | ||||||||||||||||||||||||||||
| 536 | C<originalFilename()>, C<downloadUrl>, etc. | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | Will return all entries found unless C<maxResults> is set: | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | my $children = $gd->children( "/path/to", { maxResults => 3 } ) | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | Due to the somewhat capricious ways Google Drive handles its directory | ||||||||||||||||||||||||||||
| 543 | structures, the method needs to traverse the path component by component | ||||||||||||||||||||||||||||
| 544 | and determine the ID of each directory to get to the next level. To speed | ||||||||||||||||||||||||||||
| 545 | up subsequent lookups, it also returns the ID of the last component to the | ||||||||||||||||||||||||||||
| 546 | caller: | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | my( $children, $parent ) = $gd->children( "/path/to" ); | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | If the caller now wants to e.g. insert a file into the directory, its | ||||||||||||||||||||||||||||
| 551 | ID is available in $parent. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | Each child comes back as a files#resource type and gets mapped into | ||||||||||||||||||||||||||||
| 554 | an object that offers access to the various fields via methods: | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | for my $child ( @$children ) { | ||||||||||||||||||||||||||||
| 557 | print $child->kind(), " ", $child->title(), "\n"; | ||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | Please refer to | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | https://developers.google.com/drive/v2/reference/files#resource | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | for details on which fields are available. | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | =item C<my $files = $gd-E<gt>files( )> | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | Return all files on the drive as a reference to an array. | ||||||||||||||||||||||||||||
| 569 | Will return all entries found unless C<maxResults> is set: | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | my $files = $gd->files( { maxResults => 3 } ) | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | Note that Google limits the number of entries returned by default to | ||||||||||||||||||||||||||||
| 574 | 100, and seems to restrict the maximum number of files returned | ||||||||||||||||||||||||||||
| 575 | by a single query to 3,500, even if you specify higher values for | ||||||||||||||||||||||||||||
| 576 | C<maxResults>. | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | Each file comes back as an object that offers access to the Google | ||||||||||||||||||||||||||||
| 579 | Drive item's fields, according to the API (see C<children()>). | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =item C<my $id = $gd-E<gt>folder_create( "folder-name", $parent_id )> | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | Create a new folder as a child of the folder with the id C<$parent_id>. | ||||||||||||||||||||||||||||
| 584 | Returns the ID of the new folder or undef in case of an error. | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | =item C<my $id = $gd-E<gt>file_create( "folder-name", "mime-type", $parent_id )> | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | Create a new file with the given mime type as a child of the folder with the id C<$parent_id>. | ||||||||||||||||||||||||||||
| 589 | Returns the ID of the new file or undef in case of an error. | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | Example to create an empty google spreadsheet: | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | my $id = $gd->file_create( "Quarter Results", "application/vnd.google-apps.spreadsheet", "root" ); | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | =item C<$gd-E<gt>file_upload( $file, $dir_id )> | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | Uploads the content of the file C<$file> into the directory with the ID | ||||||||||||||||||||||||||||
| 598 | $dir_id on Google Drive. Uses C<$file> as the file name. | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | To overwrite an existing file on Google Drive, specify the file's ID as | ||||||||||||||||||||||||||||
| 601 | an optional parameter: | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | $gd->file_upload( $file, $dir_id, $file_id ); | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =item C<$gd-E<gt>rename( $file_id, $name )> | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | Renames the file or folder with C<$file_id> to the specified C<$name>. | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | =item C<$gd-E<gt>download( $item, [$local_filename] )> | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | Downloads an item found via C<files()> or C<children()>. Also accepts | ||||||||||||||||||||||||||||
| 612 | the downloadUrl of an item. If C<$local_filename> is not specified, | ||||||||||||||||||||||||||||
| 613 | C<download()> will return the data downloaded (this might be undesirable | ||||||||||||||||||||||||||||
| 614 | for large files). If C<$local_filename> is specified, C<download()> will | ||||||||||||||||||||||||||||
| 615 | store the downloaded data under the given file name. | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | my $gd = Net::Google::Drive::Simple->new(); | ||||||||||||||||||||||||||||
| 618 | my $files = $gd->files( { maxResults => 20 }, { page => 0 } ); | ||||||||||||||||||||||||||||
| 619 | for my $file ( @$files ) { | ||||||||||||||||||||||||||||
| 620 | my $name = $file->originalFilename(); | ||||||||||||||||||||||||||||
| 621 | print "Downloading $name\n"; | ||||||||||||||||||||||||||||
| 622 | $gd->download( $file, $name ) or die "failed: $!"; | ||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | Be aware that only documents like PDF or png can be downloaded directly. Google Drive Documents like spreadsheets or (text) documents need to be exported into one of the available formats. | ||||||||||||||||||||||||||||
| 626 | Check for "exportLinks" on a file given. In case of a document that can be exported you will receive a hash in the form: | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | { | ||||||||||||||||||||||||||||
| 629 | 'format_1' => 'download_link_1', | ||||||||||||||||||||||||||||
| 630 | 'format_2' => 'download_link_2', | ||||||||||||||||||||||||||||
| 631 | ... | ||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | Choose your download link and use it as an argument to the download() function which can also take urls directly. | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | my $gd = Net::Google::Drive::Simple->new(); | ||||||||||||||||||||||||||||
| 637 | my $children = $gd->children( '/path/to/folder/on/google/drive' ); | ||||||||||||||||||||||||||||
| 638 | for my $child ( @$children ) { | ||||||||||||||||||||||||||||
| 639 | if ($child->can( 'exportLinks' )){ | ||||||||||||||||||||||||||||
| 640 | my $type_chosen; | ||||||||||||||||||||||||||||
| 641 | foreach my $type (keys %{$child->exportLinks()}){ | ||||||||||||||||||||||||||||
| 642 | # Take any type you can get.. | ||||||||||||||||||||||||||||
| 643 | $type_chosen = $type; | ||||||||||||||||||||||||||||
| 644 | # ..but choose your preferred format, opendocument here: | ||||||||||||||||||||||||||||
| 645 | last if $type =~/oasis\.opendocument/; | ||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | my $url = $child->exportLinks()->{$type_chosen}; | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | $gd->download($url, 'my/local/file'); | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | =item C<my $files = $gd-E<gt>search( )> | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | my $children= $gd->search({ maxResults => 20 },{ page => 0 }, | ||||||||||||||||||||||||||||
| 657 | "title contains 'Futurama'"); | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | Search files for attributes. See | ||||||||||||||||||||||||||||
| 660 | L<https://developers.google.com/drive/web/search-parameters> | ||||||||||||||||||||||||||||
| 661 | for a definition of the attributes. | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | To list all available files, those on the drive, those directly shared | ||||||||||||||||||||||||||||
| 664 | with the user, and those generally available to the user, use an | ||||||||||||||||||||||||||||
| 665 | empty search: | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | my $children= $gd->search({},{ page => 0 },""); | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | =item C<$gd-E<gt>file_delete( file_id )> | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | Delete the file with the specified ID from Google Drive. | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =item C<$gd-E<gt>drive_mvdir( "/gdrive/path/to/file", "/path/to/new/folder" )> | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | Move an existing file to a new folder. Removes the file's "parent" | ||||||||||||||||||||||||||||
| 676 | setting (pointing to the old folder) and then adds the new folder as a | ||||||||||||||||||||||||||||
| 677 | new parent. | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =item C<children_by_folder_id> | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | =item C<file_mvdir> | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =item C<http_delete> | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | =item C<http_put> | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | =back | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | =head1 LEGALESE | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | Copyright 2012-2019 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||
| 692 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||
| 693 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | 2019, Nicolas R. <cpan@atoomic.org> | ||||||||||||||||||||||||||||
| 698 | 2012-2019, Mike Schilli <cpan@perlmeister.com> | ||||||||||||||||||||||||||||