| 1 | # ========================================================================== # | ||||||||||||||||||||||||||||
| 2 | # WWW::Google::Drive | ||||||||||||||||||||||||||||
| 3 | # - Used to modify Google Drive data using service account (server to server) operations | ||||||||||||||||||||||||||||
| 4 | # ========================================================================== # | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package WWW::Google::Drive; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use JSON qw( from_json to_json decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use JSON::WebToken; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Config::JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Sysadm::Install qw( slurp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use File::Type; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | our $VERSION = "0.03"; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | WWW::Google::Drive - Used to modify Google Drive data for service account (server-to-server) | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | use WWW::Google::Drive; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $gd = WWW::Google::Drive->new( | ||||||||||||||||||||||||||||
| 37 | secret_json => 'YourProject.json', | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Set the Google user to impersonate. | ||||||||||||||||||||||||||||
| 40 | # Your Google Business Administrator must have already set up | ||||||||||||||||||||||||||||
| 41 | # your Client ID as a trusted app in order to use this successfully. | ||||||||||||||||||||||||||||
| 42 | user_as => 'name@domain.com' #(optional) | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | my $children = $gd->children('/MyDocs'); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | foreach my $item (@{$children}){ | ||||||||||||||||||||||||||||
| 47 | print "File name: $item->{name}\n"; | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | WWW::Google::Drive authenticates with a Google Drive service account (server-to-server) and offers several convenient methods to list, retrieve and modify the data stored in the google drive. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Refer: https://developers.google.com/identity/protocols/OAuth2ServiceAccount for creating a service account and the client_secret json file. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Refer: https://developers.google.com/drive/v3/reference/ for list of file properties, response values, query_params and body_params. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 METHODS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =over 4 | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | has secret_json => (is => "ro"); | |||||||||||||||||||||||||||
| 65 | 1 | has user_as => (is => 'ro'); | |||||||||||||||||||||||||||
| 66 | 1 | has http_retry_no => (is => "ro", default => 0); | |||||||||||||||||||||||||||
| 67 | 1 | has http_retry_interval => (is => "ro", default => 5); | |||||||||||||||||||||||||||
| 68 | 1 | has show_trash_items => (is => 'rw', default => 0); | |||||||||||||||||||||||||||
| 69 | 1 | has scope => (is => "ro", default => 'https://www.googleapis.com/auth/drive'); | |||||||||||||||||||||||||||
| 70 | 1 | has token_uri => (is => "ro", default => 'https://www.googleapis.com/oauth2/v4/token'); | |||||||||||||||||||||||||||
| 71 | 1 | has api_file_url => (is => "ro", default => 'https://www.googleapis.com/drive/v3/files'); | |||||||||||||||||||||||||||
| 72 | 1 | has api_upload_url => (is => "ro", default => 'https://www.googleapis.com/upload/drive/v3/files'); | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | has file_fields => (is => "rw", default => sub { return ['trashed', 'id', 'name', 'kind', 'mimeType'] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 1 | has file_fields_changed => (is => "rw", default => 0); | |||||||||||||||||||||||||||
| 75 | 1 | has file_fields_str => (is => "rw", default => 'trashed,id,name,kind,mimeType'); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | has export_options_mime => ( | ||||||||||||||||||||||||||||
| 78 | is => "ro", | ||||||||||||||||||||||||||||
| 79 | default => sub { | ||||||||||||||||||||||||||||
| 80 | return { | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | 'application/vnd.google-apps.document' => { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 'HTML' => 'text/html', | ||||||||||||||||||||||||||||
| 83 | 'Plain text' => 'text/plain', | ||||||||||||||||||||||||||||
| 84 | 'Rich text' => 'application/rtf', | ||||||||||||||||||||||||||||
| 85 | 'Open Office doc' => 'application/vnd.oasis.opendocument.text', | ||||||||||||||||||||||||||||
| 86 | 'PDF' => 'application/pdf', | ||||||||||||||||||||||||||||
| 87 | 'MS Word document' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | ||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | 'application/vnd.google-apps.spreadsheet' => { | ||||||||||||||||||||||||||||
| 90 | 'MS Excel' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||||||||||||||||||||||||||||
| 91 | 'Open Office sheet' => 'application/x-vnd.oasis.opendocument.spreadsheet', | ||||||||||||||||||||||||||||
| 92 | 'PDF' => 'application/pdf', | ||||||||||||||||||||||||||||
| 93 | 'CSV (first sheet only)' => 'text/csv', | ||||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | 'application/vnd.google-apps.drawing' => { | ||||||||||||||||||||||||||||
| 96 | 'JPEG' => 'image/jpeg', | ||||||||||||||||||||||||||||
| 97 | 'PNG' => 'image/png', | ||||||||||||||||||||||||||||
| 98 | 'SVG' => 'image/svg+xml', | ||||||||||||||||||||||||||||
| 99 | 'PDF' => 'application/pdf', | ||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 'application/vnd.google-apps.presentation' => { | ||||||||||||||||||||||||||||
| 103 | 'MS PowerPoint' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | ||||||||||||||||||||||||||||
| 104 | 'PDF' => 'application/pdf', | ||||||||||||||||||||||||||||
| 105 | 'Plain text' => 'text/plain', | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 1 | ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | has error => ( | ||||||||||||||||||||||||||||
| 112 | is => "rw", | ||||||||||||||||||||||||||||
| 113 | trigger => sub { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my ($self, $set) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (defined $set) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $self->{error} = $set; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 0 | return $self->{error}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 1 | ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | has lwp_opts => ( | ||||||||||||||||||||||||||||
| 123 | is => "ro", | ||||||||||||||||||||||||||||
| 124 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 125 | trigger => sub { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my ($self, $new_val, $old_val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if($new_val and ref $new_val eq "HASH"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$new_val and ref $new_val eq "HASH"
MC/DC0%0/2
$new_val and ref $new_val eq "HASH"
$new_val
ref $new_val eq "HASH"
Truth table0%0/3
A and B
A= $new_valB= ref $new_val eq "HASH"
| |||||||||||||||||||||||||||||
| 128 | 0 | my $proxy_conf = delete $new_val->{proxy_conf}; | |||||||||||||||||||||||||||
| 129 | 0 | $self->{user_agent} = LWP::UserAgent->new(%{$new_val}); | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if($proxy_conf){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if(ref $proxy_conf eq 'ARRAY'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $self->{user_agent}->proxy(@{$proxy_conf}); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | 0 | LOGDIE "Expected type of proxy_conf in lwp_opts is ARRAY ref"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | return $new_val; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | else{ | ||||||||||||||||||||||||||||
| 141 | 0 | LOGDIE "Expected type of lwp_opts is hash ref"; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 0 | return $old_val; | |||||||||||||||||||||||||||
| 144 | }, | ||||||||||||||||||||||||||||
| 145 | default => sub { | ||||||||||||||||||||||||||||
| 146 | 1 | ▶ | return {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 1 | ); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | has user_agent => ( | ||||||||||||||||||||||||||||
| 151 | is => "ro", | ||||||||||||||||||||||||||||
| 152 | isa => "LWP::UserAgent", | ||||||||||||||||||||||||||||
| 153 | default => sub { | ||||||||||||||||||||||||||||
| 154 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | if($self->{user_agent}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | return $self->{user_agent}; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||||||||||
| 159 | 1 | $self->{user_agent} = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 160 | 1 | return $self->{user_agent}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 1 | ); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item B<new> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $gd = WWW::Google::Drive->new( | ||||||||||||||||||||||||||||
| 168 | secret_json => "./YourProject.json" | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Parameters can be | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | user_as (optional) | ||||||||||||||||||||||||||||
| 174 | - email id of the account, if set, then all operations will be done in that respective user's space. Respective account should be authenticated using OAuth2.0 mechanism. See Net::Google::Drive::Simple eg/ directory for an example script to do authentication using OAuth2.0 | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | http_retry_no (default 0) | ||||||||||||||||||||||||||||
| 177 | - number of time each http requests should be retried if the request is failed | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | http_retry_interval (default 5) | ||||||||||||||||||||||||||||
| 180 | - time interval in seconds after which another attempt will be made for the previously failed http request, this setting is useless when http_retry_no is set to 0 | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | show_trash_items (default 0) | ||||||||||||||||||||||||||||
| 183 | - when this value is set, trash files filter will be disabled | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | lwp_opts (default {}) | ||||||||||||||||||||||||||||
| 186 | - This will be passed to LWP::UserAgent constructor | ||||||||||||||||||||||||||||
| 187 | - additionally you can pass an array ref with key as proxy_conf in lwp_opts | ||||||||||||||||||||||||||||
| 188 | which will be passed to $user_agent->proxy(@{$proxy_conf}); | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # ============================================= BUILD ======================================== # | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub BUILD | ||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||
| 196 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 1 | $self->{file_type_obj} = File::Type->new(); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # ========================================= files ============================================= # | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item B<files> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Params : $query_params (optional) | ||||||||||||||||||||||||||||
| 206 | For list of query_params refer https://developers.google.com/drive/v3/reference/files/list | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Returns : List of files | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Desc : Get all files from your drive | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Usage : | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | my $files_at_drive = $gd->files(); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub files | ||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | my ($self, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
files: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (!defined $query_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | $query_params = {}; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | my @docs = (); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | my $more_pages = 1; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | ▶ | while ($more_pages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | $more_pages = 0; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | my $url = $self->_file_uri($query_params, undef, 1); | |||||||||||||||||||||||||||
| 235 | 0 | my $data = $self->_get_http_json_response($url); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | return undef unless ($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | my $items = $data->{files}; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if (!$self->show_trash_items) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 0 | $items = $self->remove_trashed($items); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | foreach my $item (@{$items}) { | |||||||||||||||||||||||||||
| 246 | 0 | ▶ | if ($item->{kind} eq "drive#file") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 0 | my $file = $item->{name}; | |||||||||||||||||||||||||||
| 248 | 0 | ▶ | if (!defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 0 | DEBUG "Skipping $item->{id} (no originalFilename)"; | |||||||||||||||||||||||||||
| 250 | 0 | next; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 0 | push @docs, $item; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | else { | ||||||||||||||||||||||||||||
| 256 | 0 | DEBUG "Skipping $item->{name} ($item->{kind})"; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if ($data->{nextPageToken}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | $query_params->{pageToken} = $data->{nextPageToken}; | |||||||||||||||||||||||||||
| 262 | 0 | $more_pages = 1; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | return \@docs; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # ========================================= children ========================================= # | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =item B<children> | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | Params : $path, $query_params (optional), $body_params (optional) | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | Returns : All items under the given path as an array ref | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Desc : Get children of given directory. Since the directory path is iteratively found for optimization $parent_id is returned as second argument when calling in array context. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Usage : | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | my $children = $gd->children('/my_docs' $query_params, $body_params); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | or | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | my ($children, $parent_id) = $gd->children('/my_docs', $query_params, $body_params); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | sub children | ||||||||||||||||||||||||||||
| 290 | { | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | my ($self, $path, $query_params, $body_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
children: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | ▶ | if (!defined $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 0 | LOGDIE "No path given"; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | DEBUG "Determine children of $path"; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | my ($folder_id, $parent) = $self->_path_to_folderid($path, $query_params, $body_params); | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | unless ($folder_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 0 | DEBUG "Unable to resolve path $path"; | |||||||||||||||||||||||||||
| 303 | 0 | return undef; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | DEBUG "Getting content of folder $folder_id, Path: $path"; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 0 | my $children = $self->children_by_folder_id($folder_id, $query_params, $body_params); | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if (!defined $children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | return undef; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | return ($children, $parent); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | else { | ||||||||||||||||||||||||||||
| 318 | 0 | return $children; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # =================================== children_by_folder_id ===================================== # | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =item B<children_by_folder_id> | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Params : $folder_id, $query_params (optional), $body_params (optional) | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Returns : Arrayref of items (files) | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Desc : Get all the items which has $folder_id as parent | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Usage : | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | my $items = $gd->children_by_folder_id($parent_id, { orderBy => 'modifiedTime' }); | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | sub children_by_folder_id | ||||||||||||||||||||||||||||
| 339 | { | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | my ($self, $folder_id, $query_params, $body_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
children_by_folder_id: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if (!defined $body_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | $body_params = {}; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | $body_params = { | ||||||||||||||||||||||||||||
| 347 | 0 | page => 1, | |||||||||||||||||||||||||||
| 348 | %$body_params, | ||||||||||||||||||||||||||||
| 349 | }; | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | if (!defined $query_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 0 | $query_params = {}; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | $query_params->{q} = "'$folder_id' in parents"; | |||||||||||||||||||||||||||
| 356 | 0 | my $url = $self->_file_uri($query_params, undef, 1); | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ($body_params->{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | $query_params->{q} .= " AND name = '$body_params->{name}'"; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | my $result = $self->_get_items_from_result($url, $query_params); | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | return $result; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | # ====================================== new_file =========================================== # | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =item B<new_file> | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Params : $local_file_path, $folder_id, $options (optional key value pairs), $query_params (optional) | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Returns : new file id, ( and second argument as response data hashref when called in array context ) | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Desc : Uploads a new file ( file at $local_file_path ) to the drive in the given folder ( given folder_id ) | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | Usage : | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | my $file_id = $gd->new_file('./testfile', $parent_id, { description => "This is a test file upload" }); | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =cut | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | sub new_file | ||||||||||||||||||||||||||||
| 384 | { | ||||||||||||||||||||||||||||
| 385 | 0 | ▶ | my ($self, $file, $parent_id, $options, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | my $title = basename $file; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # First, POST the new file metadata to the Drive endpoint | ||||||||||||||||||||||||||||
| 390 | # http://stackoverflow.com/questions/10317638/inserting-file-to-google-drive-through-api | ||||||||||||||||||||||||||||
| 391 | 0 | my $url = $self->_file_uri(); | |||||||||||||||||||||||||||
| 392 | 0 | my $mime_type = $self->file_mime_type($file); | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | my $data = $self->_get_http_json_response( | ||||||||||||||||||||||||||||
| 395 | $url, | ||||||||||||||||||||||||||||
| 396 | { | ||||||||||||||||||||||||||||
| 397 | mimeType => $mime_type, | ||||||||||||||||||||||||||||
| 398 | parents => [$parent_id], | ||||||||||||||||||||||||||||
| 399 | name => $title, | ||||||||||||||||||||||||||||
| 400 | 0 | %{$options} | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | ); | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 0 | ▶ | return undef unless ($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | my $file_id = $data->{id}; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | $data = $self->_file_upload($file_id, $file, $mime_type, $query_params); | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 0 | return ($file_id, $data); | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | else { | ||||||||||||||||||||||||||||
| 414 | 0 | return $file_id; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # ===================================== update_file ========================================= # | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =item B<update_file> | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | Params : $old_file_id, $updated_local_file_path, $body_params (optional), $query_params (optional) | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | Returns : $file_id on Successful upload | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Desc : This will replace the existing file in the drive with new file. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Usage : | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | my $file_id = $gd->update_file($old_file_id, "./updated_file"); | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | NOTE : If you only want to update file metadata, then send file_path as undef and $body_params should have properties. | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | sub update_file | ||||||||||||||||||||||||||||
| 437 | { | ||||||||||||||||||||||||||||
| 438 | 0 | ▶ | my ($self, $file_id, $file_path, $options, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_file: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | my $title = basename $file_path; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | my $url = $self->_file_uri($query_params, $file_id); | |||||||||||||||||||||||||||
| 443 | 0 | my $mime_type = $self->file_mime_type($file_path); | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 0 | my $data; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 0 | ▶ | if (defined $options) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 0 | $data = $self->_patch_http_json_response($url, $options); | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | ▶ | if ($file_path and -f $file_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file_path and -f $file_path
MC/DC0%0/2
$file_path and -f $file_path
$file_path
-f $file_path
Truth table0%0/3
A and B
A= $file_pathB= -f $file_path
| |||||||||||||||||||||||||||||
| 452 | 0 | $data = $self->_file_upload($file_id, $file_path, $mime_type, $query_params); | |||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 0 | ▶ | return undef unless ($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | 0 | return ($file_id, $data); | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | else { | ||||||||||||||||||||||||||||
| 461 | 0 | return $file_id; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | # ====================================== delete ======================================== # | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =item B<delete> | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Params : $item_id (It can be a file_id or folder_id), $no_trash (optional) | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | Returns : deleted file id on successful deletion | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | Desc : Deletes an item from google drive. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Usage : | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | my $deleted_file_id = $gd->delete($file_id); | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | NOTE : If $no_trash option is set, file will be deleted from google drive permanently. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =cut | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | sub delete | ||||||||||||||||||||||||||||
| 484 | { | ||||||||||||||||||||||||||||
| 485 | 0 | ▶ | my ($self, $item_id, $no_trash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | LOGDIE 'Deletion requires file_id' if (!defined $item_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 0 | my $req; | |||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 0 | ▶ | if ($no_trash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | 0 | my $url = $self->_file_uri({}, $item_id); | |||||||||||||||||||||||||||
| 493 | 0 | $req = &HTTP::Request::Common::DELETE($url->as_string, $self->_authorization_headers()); | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | else { | ||||||||||||||||||||||||||||
| 496 | 0 | my $url = $self->_file_uri({}, $item_id); | |||||||||||||||||||||||||||
| 497 | 0 | $req = $self->_http_req_data($url, {'trashed' => JSON::true}, 1); | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 0 | my $resp = $self->_http_rsp_data($req); | |||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 0 | DEBUG "Request: ", $req->as_string; | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 0 | DEBUG "Response: ", $resp->as_string; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 0 | ▶ | if ($resp->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 507 | 0 | $self->error($resp->message()); | |||||||||||||||||||||||||||
| 508 | 0 | ERROR $resp->content; | |||||||||||||||||||||||||||
| 509 | 0 | return undef; | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 0 | return $item_id; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # ===================================== create_folder ======================================== # | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =item B<create_folder> | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | Params : $folder_name, $parent_folder_id | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | Returns : $folder_id (newly created folder Id) | ||||||||||||||||||||||||||||
| 522 | If called in an array context then second argument will be the http response data | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | Desc : Used to create a new directory in your drive | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | Usage : | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | my $new_folder_id = $gd->create_folder("Test",$parent_id); | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | =cut | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | sub create_folder | ||||||||||||||||||||||||||||
| 533 | { | ||||||||||||||||||||||||||||
| 534 | 0 | ▶ | my ($self, $title, $parent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
create_folder: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 0 | ▶ | LOGDIE "create_folder need 2 arguments (title and parent_id)" unless ($title or $parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$title or $parent
MC/DC0%0/2
$title or $parent
$title
$parent
Truth table0%0/3
A or B
A= $titleB= $parent
| |||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 0 | my $url = $self->_file_uri(); | |||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 0 | my $data = $self->_get_http_json_response( | |||||||||||||||||||||||||||
| 541 | $url, | ||||||||||||||||||||||||||||
| 542 | { | ||||||||||||||||||||||||||||
| 543 | name => $title, | ||||||||||||||||||||||||||||
| 544 | parents => [$parent], | ||||||||||||||||||||||||||||
| 545 | mimeType => "application/vnd.google-apps.folder", | ||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | ); | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | 0 | ▶ | if (!defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 550 | 0 | return undef; | |||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | 0 | return ($data->{id}, $data); | |||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | else { | ||||||||||||||||||||||||||||
| 557 | 0 | return $data->{id}; | |||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | # ========================================= search ========================================= # | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | =item B<search> | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | Params : $query string, $query_params (optional) | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | Returns : Result items (files) for the given query | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | Desc : Do search on the google drive using the syntax mentioned in google drive, refer https://developers.google.com/drive/v3/web/search-parameters for list of search parameters and examples | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | Usage : | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | my $items = $gd->search("mimeType contains 'image/'",{ corpus => 'user' }); | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | =cut | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | sub search | ||||||||||||||||||||||||||||
| 578 | { | ||||||||||||||||||||||||||||
| 579 | 0 | ▶ | my ($self, $query, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 0 | ▶ | if (!defined $query_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 582 | 0 | $query_params = {}; | |||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | $query_params->{q} = $query; | |||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 0 | my $url = $self->_file_uri($query_params, undef, 1); | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | my $items = $self->_get_items_from_result($url, $query_params); | |||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | 0 | return $items; | |||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | # ========================================= download ========================================= # | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | =item B<download> | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | Params : $file_id, $local_file_path, $acknowledgeAbuse | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | Returns : 0/1 when $file_path is given, otherwise returns the file content | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | Desc : Download file from the drive, when you pass file_id as a ref this method will try to find the $file_id->{id} and tries to download that file. When local file name with path is not given, this method will return the content of the file on success download. | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | Usage : | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | $gd->download($file_id, $local_file); | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | or | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | my $file_content = $gd->download($file_id); | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =cut | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | sub download | ||||||||||||||||||||||||||||
| 615 | { | ||||||||||||||||||||||||||||
| 616 | 0 | ▶ | my ($self, $file_id, $local_file, $acknowledgeAbuse) = @_; | ||||||||||||||||||||||||||
|
Subroutine
download: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 0 | ▶ | if (ref $file_id eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | 0 | $file_id = $file_id->{id}; | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | 0 | ▶ | if (not $file_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 623 | 0 | my $msg = "Can't download, file id required as 1st argument"; | |||||||||||||||||||||||||||
| 624 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 625 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 626 | 0 | return undef; | |||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 0 | my $url = $self->_file_uri({alt => "media"}, $file_id); | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 0 | my $req = HTTP::Request->new(GET => $url); | |||||||||||||||||||||||||||
| 632 | 0 | $req->header($self->_authorization_headers()); | |||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | 0 | my $ua = $self->user_agent; | |||||||||||||||||||||||||||
| 635 | 0 | my $resp = $ua->request($req, $local_file); | |||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | 0 | ▶ | if ($resp->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 638 | 0 | my $msg = "Can't download file id $file_id (" . $resp->message() . ")"; | |||||||||||||||||||||||||||
| 639 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 640 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 641 | 0 | return undef; | |||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | 0 | ▶ | if ($local_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 645 | 0 | return 1; | |||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | 0 | return $resp->decoded_content(); | |||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | # ========================================= export ========================================= # | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | =item B<export_options> | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | Params : $mimeType or { mimeType => $mime_type } | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | Return : hashref with key as mimeType name and value as mimeType | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | Desc : Return available export options with its mimeType for the given google docs mimeType | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | =cut | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | sub export_options | ||||||||||||||||||||||||||||
| 664 | { | ||||||||||||||||||||||||||||
| 665 | 0 | ▶ | my ($self, $mime_type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
export_options: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | 0 | my $mime; | |||||||||||||||||||||||||||
| 668 | 0 | ▶ | if (ref $mime_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 669 | 0 | $mime = $mime_type->{mimeType}; | |||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||
| 671 | else { | ||||||||||||||||||||||||||||
| 672 | 0 | $mime = $mime_type; | |||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | 0 | ▶ | return undef unless ($mime); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | 0 | return $self->{export_options_mime}->{$mime}; | |||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | # ========================================= export ========================================= # | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =item B<export> | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | Params : $file_id, $mime_type, $local_file_path | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | Returns : 0/1 when $local_file_path is given, otherwise returns the file content | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | Desc : Download exported file from the drive, when you pass file_id as a ref, this method will try to find the $file_id->{id} and tries to download that file. When local file name with path is not given, this method will return the content of the file on success download. | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | Usage : | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | $gd->export($file_id, 'application/pdf', $local_file_path); | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | or | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | my $file_content = $gd->export($file_id, 'application/pdf'); | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | =cut | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | sub export | ||||||||||||||||||||||||||||
| 701 | { | ||||||||||||||||||||||||||||
| 702 | 0 | ▶ | my ($self, $file_id, $mime_type, $local_file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
export: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | 0 | ▶ | if (ref $file_id eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 705 | 0 | $file_id = $file_id->{id}; | |||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | 0 | ▶ | if (not $file_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 709 | 0 | my $msg = "Can't download/export, file id required as 1st argument"; | |||||||||||||||||||||||||||
| 710 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 711 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 712 | 0 | return undef; | |||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | 0 | my $uri = $self->{api_file_url} . "/$file_id/export"; | |||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | 0 | my $url = URI->new($uri); | |||||||||||||||||||||||||||
| 718 | 0 | $url->query_form({mimeType => $mime_type}); | |||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | 0 | my $req = HTTP::Request->new(GET => $url); | |||||||||||||||||||||||||||
| 721 | 0 | $req->header($self->_authorization_headers()); | |||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | 0 | my $ua = $self->user_agent; | |||||||||||||||||||||||||||
| 724 | 0 | my $resp = $ua->request($req, $local_file); | |||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | 0 | ▶ | if ($resp->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 727 | 0 | my $msg = "Can't download file id $file_id (" . $resp->message() . ")"; | |||||||||||||||||||||||||||
| 728 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 729 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 730 | 0 | return undef; | |||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | 0 | ▶ | if ($local_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 734 | 0 | return 1; | |||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | 0 | return $resp->decoded_content(); | |||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | # ========================================= metadata ========================================= # | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | =item B<metadata> | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | Params : $file_id, $query_params | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | Returns : hashref of properties on success | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | Desc : Used to get the file metadata. (files.get) | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | Usage : | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | my $properties = $gd->metadata($file_id); | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | =cut | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | sub metadata | ||||||||||||||||||||||||||||
| 757 | { | ||||||||||||||||||||||||||||
| 758 | 0 | ▶ | my ($self, $file_id, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
metadata: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | 0 | ▶ | if (ref $file_id eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 761 | 0 | $file_id = $file_id->{id}; | |||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | 0 | ▶ | if (not $file_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 765 | 0 | my $msg = "Can't get properties, file id required as 1st argument"; | |||||||||||||||||||||||||||
| 766 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 767 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 768 | 0 | return undef; | |||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | 0 | my $url = $self->_file_uri($query_params, $file_id); | |||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | 0 | return $self->_get_http_json_response($url); | |||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | # ====================================== file_mime_type ====================================== # | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | =item B<file_mime_type> | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | Params : $local_file_path | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | Returns : mime type of the given file | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | Desc : Find the MimeType of a file using File::Type | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | Usage : | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | my $mime_type = $gd->file_mime_type("./testfile"); | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | =cut | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | sub file_mime_type | ||||||||||||||||||||||||||||
| 793 | { | ||||||||||||||||||||||||||||
| 794 | 1 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_mime_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | 1 | return $self->{file_type_obj}->checktype_filename($file); | |||||||||||||||||||||||||||
| 797 | } | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | # ====================================== remove_trashed ====================================== # | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | =item B<remove_trashed> | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | Params : $items_arrayref ( return value of files() or children() ) | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | Returns : $items arrayref | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | Desc : This method will filter out all the files marked as trashed | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | Usage : | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | my $items = $gd->children('./MyDocs'); | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | # do something with the data | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | my $live_items = $gd->remove_trashed($items); | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | =cut | ||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | sub remove_trashed | ||||||||||||||||||||||||||||
| 820 | { | ||||||||||||||||||||||||||||
| 821 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_trashed: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||
| 823 | 0 | ▶ | return unless (defined $data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||
| 825 | 0 | ▶ | if (ref $data ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 826 | 0 | LOGDIE "remove_trashed expects an array ref argument, but called with " . ref $data; | |||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | 0 | my @new_data = (); | |||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | 0 | foreach my $item (@{$data}) { | |||||||||||||||||||||||||||
| 832 | 0 | ▶ | if ($item->{trashed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 833 | 0 | DEBUG "Skipping trashed item '$item->{name}'"; | |||||||||||||||||||||||||||
| 834 | 0 | next; | |||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||
| 836 | 0 | push(@new_data, $item); | |||||||||||||||||||||||||||
| 837 | } | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | 0 | return \@new_data; | |||||||||||||||||||||||||||
| 840 | } | ||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||
| 842 | # ====================================== show_trash_items ===================================== # | ||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | =item B<show_trash_items> | ||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||
| 846 | Params : 0/1 | ||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||
| 848 | Returns : NONE | ||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||
| 850 | Desc : Disable/Enable listing deleted data from your drive | ||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||
| 852 | Usage : | ||||||||||||||||||||||||||||
| 853 | $gd->show_trash_items(1); | ||||||||||||||||||||||||||||
| 854 | my $all_files = $gd->children('/'); # will return all the files including files in trash | ||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||
| 856 | NOTE : This module will consider an item as trashed if the file's metadata 'trashed' is set to true. | ||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||
| 858 | =cut | ||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||
| 860 | # Moose property | ||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||
| 862 | # ====================================== add_req_file_fields ================================= # | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | =item B<add_req_file_fields> | ||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | Params : file_properties as list | ||||||||||||||||||||||||||||
| 867 | Refer: https://developers.google.com/drive/v3/reference/files#resource for list of properties | ||||||||||||||||||||||||||||
| 868 | Refer: https://developers.google.com/drive/v3/web/performance for fields syntax | ||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||
| 870 | Returns : NONE | ||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||
| 872 | Desc : Add file fields parameter that will be used in $query_params 'fields'. | ||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | Usage : | ||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||
| 876 | $gd->add_req_file_fields('appProperties','spaces','owners/kind'); | ||||||||||||||||||||||||||||
| 877 | $gd->children('/'); | ||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | Note : By Default fields such as 'kind','trashed','id','name' are added. | ||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | =cut | ||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||
| 883 | sub add_req_file_fields | ||||||||||||||||||||||||||||
| 884 | { | ||||||||||||||||||||||||||||
| 885 | 0 | ▶ | my ($self, @fields) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_req_file_fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||
| 887 | 0 | push(@{$self->{file_fields}}, @fields); | |||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||
| 889 | 0 | my %hash; | |||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||
| 891 | 0 | @hash{@{$self->{file_fields}}} = (); | |||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||
| 893 | 0 | @{$self->{file_fields}} = keys %hash; | |||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||
| 895 | 0 | $self->{file_fields_changed} = 1; | |||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||
| 898 | # ====================================== _file_upload ======================================== # | ||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||
| 900 | sub _file_upload | ||||||||||||||||||||||||||||
| 901 | { | ||||||||||||||||||||||||||||
| 902 | 0 | ▶ | my ($self, $file_id, $file, $mime_type, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_file_upload: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||
| 904 | # Since a file upload can take a long time, refresh the token | ||||||||||||||||||||||||||||
| 905 | # just in case. | ||||||||||||||||||||||||||||
| 906 | 0 | $self->_token_expire(); | |||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||
| 908 | 0 | ▶ | unless (-f $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 909 | 0 | LOGDIE "$file does not exist in your local machine"; | |||||||||||||||||||||||||||
| 910 | 0 | return undef; | |||||||||||||||||||||||||||
| 911 | } | ||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||
| 913 | 0 | my $file_data = slurp $file; | |||||||||||||||||||||||||||
| 914 | 0 | my $file_size = -s $file; | |||||||||||||||||||||||||||
| 915 | 0 | ▶ | $mime_type = $self->file_mime_type($file) unless ($mime_type); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | 0 | ▶ | $query_params = {} unless (defined $query_params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 918 | 0 | $query_params = {uploadType => "media", %{$query_params}}; | |||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||
| 920 | 0 | my $url = URI->new($self->{api_upload_url} . "/$file_id"); | |||||||||||||||||||||||||||
| 921 | 0 | $url->query_form($query_params); | |||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||
| 923 | #my $req = &HTTP::Request::Common::PUT( | ||||||||||||||||||||||||||||
| 924 | # $url->as_string, | ||||||||||||||||||||||||||||
| 925 | # $self->_authorization_headers(), | ||||||||||||||||||||||||||||
| 926 | # "Content-Type" => $mime_type, | ||||||||||||||||||||||||||||
| 927 | # "Content-Length" => $file_size, | ||||||||||||||||||||||||||||
| 928 | # "Content" => $file_data, | ||||||||||||||||||||||||||||
| 929 | #); | ||||||||||||||||||||||||||||
| 930 | 0 | my $req = HTTP::Request->new('PATCH', $url->as_string); | |||||||||||||||||||||||||||
| 931 | 0 | $req->header($self->_authorization_headers()); | |||||||||||||||||||||||||||
| 932 | 0 | $req->header("Content-Type" => $mime_type); | |||||||||||||||||||||||||||
| 933 | 0 | $req->header("Content-Length" => $file_size); | |||||||||||||||||||||||||||
| 934 | 0 | $req->content($file_data); | |||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||
| 936 | 0 | my $resp = $self->_http_rsp_data($req); | |||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||
| 938 | 0 | ▶ | if ($resp->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 939 | 0 | $self->error($resp->message()); | |||||||||||||||||||||||||||
| 940 | 0 | return undef; | |||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||
| 943 | 0 | DEBUG $resp->as_string; | |||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | 0 | my $json_data = from_json($resp->decoded_content()); | |||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | 0 | return $json_data; | |||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||
| 950 | # =================================== _get_items_from_result ============================== # | ||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||
| 952 | sub _get_items_from_result | ||||||||||||||||||||||||||||
| 953 | { | ||||||||||||||||||||||||||||
| 954 | 0 | ▶ | my ($self, $url, $query_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_items_from_result: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||
| 956 | 0 | my @items; | |||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||
| 958 | 0 | my $more_pages = 0; | |||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 961 | 0 | $url->query_form($query_params); | |||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||
| 963 | 0 | my $data = $self->_get_http_json_response($url); | |||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||
| 965 | 0 | ▶ | return undef if (!defined $data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||
| 967 | 0 | my $page_items = $data->{files}; | |||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||
| 969 | 0 | ▶ | if (!$self->show_trash_items) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 970 | 0 | $page_items = $self->remove_trashed($page_items); | |||||||||||||||||||||||||||
| 971 | } | ||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||
| 973 | 0 | push(@items, @{$page_items}); | |||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||
| 975 | 0 | $more_pages = 0; | |||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||
| 977 | 0 | ▶ | if ($data->{nextPageToken}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 978 | 0 | $query_params->{pageToken} = $data->{nextPageToken}; | |||||||||||||||||||||||||||
| 979 | 0 | $more_pages = 1; | |||||||||||||||||||||||||||
| 980 | } | ||||||||||||||||||||||||||||
| 981 | } while ($more_pages); | ||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||
| 983 | 0 | return \@items; | |||||||||||||||||||||||||||
| 984 | } | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | # =================================== _path_to_folderid ==================================== # | ||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||
| 988 | sub _path_to_folderid | ||||||||||||||||||||||||||||
| 989 | { | ||||||||||||||||||||||||||||
| 990 | 0 | ▶ | my ($self, $path, $query_params, $body_params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_path_to_folderid: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||
| 992 | 0 | my @parts = split '/', $path; | |||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||
| 994 | 0 | ▶ | if (!defined $body_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 995 | 0 | $body_params = {}; | |||||||||||||||||||||||||||
| 996 | } | ||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||
| 998 | 0 | my $parent = $parts[0] = "root"; | |||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||
| 1000 | 0 | DEBUG "Parent: $parent"; | |||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||
| 1002 | 0 | my $folder_id = shift @parts; | |||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||
| 1004 | 0 | PART: for my $part (@parts) { | |||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||
| 1006 | 0 | DEBUG "Looking up part $part (folder_id=$folder_id)"; | |||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||
| 1008 | 0 | my $children = $self->children_by_folder_id($folder_id, $query_params, {%$body_params, name => $part},); | |||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||
| 1010 | 0 | ▶ | if (!defined $children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1011 | 0 | DEBUG "Part $part not found in path $path"; | |||||||||||||||||||||||||||
| 1012 | 0 | return undef; | |||||||||||||||||||||||||||
| 1013 | } | ||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||
| 1015 | 0 | for my $child (@$children) { | |||||||||||||||||||||||||||
| 1016 | 0 | DEBUG "Found child ", $child->{name}; | |||||||||||||||||||||||||||
| 1017 | 0 | ▶ | if ($child->{name} eq $part) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1018 | 0 | $folder_id = $child->{id}; | |||||||||||||||||||||||||||
| 1019 | 0 | $parent = $folder_id; | |||||||||||||||||||||||||||
| 1020 | 0 | DEBUG "Parent: $parent"; | |||||||||||||||||||||||||||
| 1021 | 0 | next PART; | |||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||
| 1023 | } | ||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||
| 1025 | 0 | my $msg = "Child $part not found"; | |||||||||||||||||||||||||||
| 1026 | 0 | $self->error($msg); | |||||||||||||||||||||||||||
| 1027 | 0 | ERROR $msg; | |||||||||||||||||||||||||||
| 1028 | 0 | return undef; | |||||||||||||||||||||||||||
| 1029 | } | ||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||
| 1031 | 0 | return ($folder_id, $parent); | |||||||||||||||||||||||||||
| 1032 | } | ||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||
| 1034 | # =================================== _get_http_json_response ============================= # | ||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||
| 1036 | sub _get_http_json_response | ||||||||||||||||||||||||||||
| 1037 | { | ||||||||||||||||||||||||||||
| 1038 | 0 | ▶ | my ($self, $url, $post_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_http_json_response: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||
| 1040 | 0 | my $req = $self->_http_req_data($url, $post_data); | |||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||
| 1042 | 0 | my $resp = $self->_http_rsp_data($req); | |||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||
| 1044 | 0 | ▶ | if ($resp->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1045 | 0 | $self->error($resp->message()); | |||||||||||||||||||||||||||
| 1046 | 0 | return undef; | |||||||||||||||||||||||||||
| 1047 | } | ||||||||||||||||||||||||||||
| 1048 | |||||||||||||||||||||||||||||
| 1049 | 0 | my $json_data = from_json($resp->decoded_content()); | |||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||
| 1051 | 0 | return $json_data; | |||||||||||||||||||||||||||
| 1052 | } | ||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||
| 1054 | # =================================== _patch_http_json_response ============================= # | ||||||||||||||||||||||||||||
| 1055 | |||||||||||||||||||||||||||||
| 1056 | sub _patch_http_json_response | ||||||||||||||||||||||||||||
| 1057 | { | ||||||||||||||||||||||||||||
| 1058 | 0 | ▶ | my ($self, $url, $post_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_patch_http_json_response: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||
| 1060 | 0 | my $req = $self->_http_req_data($url, $post_data, 1); | |||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||
| 1062 | 0 | my $resp = $self->_http_rsp_data($req); | |||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||
| 1064 | 0 | ▶ | if ($resp->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1065 | 0 | $self->error($resp->message()); | |||||||||||||||||||||||||||
| 1066 | 0 | ERROR $resp->decoded_content(); | |||||||||||||||||||||||||||
| 1067 | 0 | return undef; | |||||||||||||||||||||||||||
| 1068 | } | ||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||
| 1070 | 0 | my $json_data = from_json($resp->decoded_content()); | |||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||
| 1072 | 0 | return $json_data; | |||||||||||||||||||||||||||
| 1073 | } | ||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | # ======================================== _http_req_data ================================= # | ||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||
| 1077 | sub _http_req_data | ||||||||||||||||||||||||||||
| 1078 | { | ||||||||||||||||||||||||||||
| 1079 | 0 | ▶ | my ($self, $url, $post_data, $patch) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_http_req_data: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 1080 | |||||||||||||||||||||||||||||
| 1081 | 0 | my $req; | |||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||
| 1083 | 0 | ▶ | if ($post_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||
| 1085 | 0 | my $json_post_data = to_json($post_data); | |||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||
| 1087 | 0 | DEBUG "Post Data: ", $json_post_data; | |||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||
| 1089 | 0 | ▶ | if ($patch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1090 | 0 | $req = HTTP::Request->new('PATCH', $url->as_string); | |||||||||||||||||||||||||||
| 1091 | 0 | $req->header($self->_authorization_headers()); | |||||||||||||||||||||||||||
| 1092 | 0 | $req->header("Content-Type" => "application/json"); | |||||||||||||||||||||||||||
| 1093 | 0 | $req->header("Content-Length" => length $json_post_data); | |||||||||||||||||||||||||||
| 1094 | 0 | $req->content($json_post_data); | |||||||||||||||||||||||||||
| 1095 | } | ||||||||||||||||||||||||||||
| 1096 | else { | ||||||||||||||||||||||||||||
| 1097 | 0 | $req = &HTTP::Request::Common::POST( | |||||||||||||||||||||||||||
| 1098 | $url->as_string, | ||||||||||||||||||||||||||||
| 1099 | $self->_authorization_headers(), | ||||||||||||||||||||||||||||
| 1100 | "Content-Type" => "application/json", | ||||||||||||||||||||||||||||
| 1101 | Content => $json_post_data, | ||||||||||||||||||||||||||||
| 1102 | ); | ||||||||||||||||||||||||||||
| 1103 | } | ||||||||||||||||||||||||||||
| 1104 | } | ||||||||||||||||||||||||||||
| 1105 | else { | ||||||||||||||||||||||||||||
| 1106 | 0 | $req = HTTP::Request->new(GET => $url->as_string,); | |||||||||||||||||||||||||||
| 1107 | 0 | $req->header($self->_authorization_headers()); | |||||||||||||||||||||||||||
| 1108 | } | ||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||
| 1110 | 0 | return $req; | |||||||||||||||||||||||||||
| 1111 | } | ||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||
| 1113 | # ====================================== _http_rsp_data ==================================== # | ||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||
| 1115 | sub _http_rsp_data | ||||||||||||||||||||||||||||
| 1116 | { | ||||||||||||||||||||||||||||
| 1117 | 0 | ▶ | my ($self, $req, $noinit) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_http_rsp_data: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||
| 1119 | 0 | my $ua = $self->user_agent; | |||||||||||||||||||||||||||
| 1120 | 0 | my $resp; | |||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||
| 1122 | 0 | my $RETRIES = $self->http_retry_no; | |||||||||||||||||||||||||||
| 1123 | 0 | my $SLEEP_INTERVAL = $self->http_retry_interval; | |||||||||||||||||||||||||||
| 1124 | 0 | my $retry_count = 0; | |||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||
| 1126 | { | ||||||||||||||||||||||||||||
| 1127 | 0 | DEBUG "====> HTTP(" . $req->method . ") ", $req->url->as_string(); | |||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||
| 1129 | 0 | $resp = $ua->request($req); | |||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||
| 1131 | 0 | ▶ | if (!$resp->is_success()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1132 | 0 | $self->error($resp->message()); | |||||||||||||||||||||||||||
| 1133 | 0 | ERROR "Failed with ", $resp->code(), ": ", $resp->message(); | |||||||||||||||||||||||||||
| 1134 | 0 | ▶ | if (--$RETRIES >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1135 | 0 | ERROR "Retry (" . ++$retry_count . ") in $SLEEP_INTERVAL seconds"; | |||||||||||||||||||||||||||
| 1136 | 0 | sleep $SLEEP_INTERVAL; | |||||||||||||||||||||||||||
| 1137 | 0 | redo; | |||||||||||||||||||||||||||
| 1138 | } | ||||||||||||||||||||||||||||
| 1139 | else { | ||||||||||||||||||||||||||||
| 1140 | 0 | return $resp; | |||||||||||||||||||||||||||
| 1141 | } | ||||||||||||||||||||||||||||
| 1142 | } | ||||||||||||||||||||||||||||
| 1143 | |||||||||||||||||||||||||||||
| 1144 | 0 | DEBUG "Successfully fetched ", length($resp->content()), " bytes."; | |||||||||||||||||||||||||||
| 1145 | 0 | DEBUG "Response", $resp->decoded_content(); | |||||||||||||||||||||||||||
| 1146 | } | ||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||
| 1148 | 0 | return $resp; | |||||||||||||||||||||||||||
| 1149 | } | ||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||
| 1151 | # ================================= _file_uri ==================================== # | ||||||||||||||||||||||||||||
| 1152 | |||||||||||||||||||||||||||||
| 1153 | sub _file_uri | ||||||||||||||||||||||||||||
| 1154 | { | ||||||||||||||||||||||||||||
| 1155 | 0 | ▶ | my ($self, $query_params, $file_id, $for_list) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_file_uri: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||
| 1157 | 0 | ▶ | $query_params = {} if !defined $query_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||
| 1159 | 0 | my $file_fields = $self->_get_fields(); | |||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||
| 1161 | 0 | ▶ | if ($file_id or !$for_list) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file_id or not $for_list
MC/DC0%0/2
$file_id or not $for_list
$file_id
not $for_list
Truth table0%0/3
A or not(B)
A= $file_idB= not $for_list
| |||||||||||||||||||||||||||||
| 1162 | 0 | $query_params->{fields} = $file_fields; | |||||||||||||||||||||||||||
| 1163 | } | ||||||||||||||||||||||||||||
| 1164 | else { | ||||||||||||||||||||||||||||
| 1165 | 0 | $query_params->{fields} = "kind,nextPageToken,files($file_fields)"; | |||||||||||||||||||||||||||
| 1166 | } | ||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||
| 1168 | 0 | my $uri = $self->{api_file_url}; | |||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||
| 1170 | 0 | ▶ | $uri .= "/$file_id" if ($file_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||
| 1172 | 0 | my $url = URI->new($uri); | |||||||||||||||||||||||||||
| 1173 | 0 | $url->query_form($query_params); | |||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||
| 1175 | 0 | return $url; | |||||||||||||||||||||||||||
| 1176 | } | ||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||
| 1178 | # =========================================== _get_fields ========================================= # | ||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||
| 1180 | sub _get_fields | ||||||||||||||||||||||||||||
| 1181 | { | ||||||||||||||||||||||||||||
| 1182 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_fields: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||
| 1184 | 0 | ▶ | if ($self->{file_fields_changed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1185 | 0 | $self->{file_fields_str} = join(',', @{$self->{file_fields}}); | |||||||||||||||||||||||||||
| 1186 | 0 | $self->{file_fields_changed} = 0; | |||||||||||||||||||||||||||
| 1187 | } | ||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||
| 1189 | 0 | return $self->{file_fields_str}; | |||||||||||||||||||||||||||
| 1190 | } | ||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||
| 1192 | # =========================================== OAuth ========================================= # | ||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||
| 1194 | # =================================== _authorization_headers ================================ # | ||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||
| 1196 | sub _authorization_headers | ||||||||||||||||||||||||||||
| 1197 | { | ||||||||||||||||||||||||||||
| 1198 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_authorization_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||
| 1200 | 0 | return ('Authorization' => 'Bearer ' . $self->_get_oauth_token); | |||||||||||||||||||||||||||
| 1201 | } | ||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||
| 1203 | # ====================================== _get_oauth_token =================================== # | ||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||
| 1205 | sub _get_oauth_token | ||||||||||||||||||||||||||||
| 1206 | { | ||||||||||||||||||||||||||||
| 1207 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_oauth_token: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||
| 1209 | 0 | ▶ | if (not exists $self->{oauth}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1210 | 0 | ▶ | $self->_authenticate or LOGDIE "Google drive authentication failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1211 | 0 | return $self->{oauth}->{_access_token}; | |||||||||||||||||||||||||||
| 1212 | } | ||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||
| 1214 | 0 | my $time_remaining = $self->{oauth}->{_expires} - time(); | |||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||
| 1216 | # checking if the token is still valid for more than 5 minutes | ||||||||||||||||||||||||||||
| 1217 | # why 5 minutes? simply :-). | ||||||||||||||||||||||||||||
| 1218 | 0 | ▶ | if ($time_remaining < 300) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1219 | 0 | ▶ | $self->_authenticate() or LOGDIE "Google drive token refresh failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1220 | } | ||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||
| 1222 | 0 | return $self->{oauth}->{_access_token}; | |||||||||||||||||||||||||||
| 1223 | } | ||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||
| 1225 | # ========================================== _authenticate =================================== # | ||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||
| 1227 | sub _authenticate | ||||||||||||||||||||||||||||
| 1228 | { | ||||||||||||||||||||||||||||
| 1229 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_authenticate: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 1230 | |||||||||||||||||||||||||||||
| 1231 | 0 | ▶ | LOGDIE "Config JSON file " . $self->secret_json . " not exist!" unless (-f $self->secret_json); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||
| 1233 | 0 | my $config = Config::JSON->new($self->secret_json); | |||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||
| 1235 | 0 | my $time = time; | |||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||
| 1237 | 0 | my $service_acc_id = $config->get("client_email"); | |||||||||||||||||||||||||||
| 1238 | 0 | my $private_key_string = $config->get("private_key"); | |||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||
| 1240 | 0 | my $jwt = JSON::WebToken->encode( | |||||||||||||||||||||||||||
| 1241 | { | ||||||||||||||||||||||||||||
| 1242 | iss => $service_acc_id, | ||||||||||||||||||||||||||||
| 1243 | scope => $self->scope, | ||||||||||||||||||||||||||||
| 1244 | aud => $self->token_uri, | ||||||||||||||||||||||||||||
| 1245 | exp => $time + 3600, | ||||||||||||||||||||||||||||
| 1246 | iat => $time, | ||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||
| 1248 | # Access files from this users drive/ impersonate user | ||||||||||||||||||||||||||||
| 1249 | prn => $self->user_as, | ||||||||||||||||||||||||||||
| 1250 | }, | ||||||||||||||||||||||||||||
| 1251 | $private_key_string, | ||||||||||||||||||||||||||||
| 1252 | 'RS256', | ||||||||||||||||||||||||||||
| 1253 | {typ => 'JWT'} | ||||||||||||||||||||||||||||
| 1254 | ); | ||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||
| 1256 | # Authenticate via post, and get a token | ||||||||||||||||||||||||||||
| 1257 | 0 | my $ua = $self->user_agent; | |||||||||||||||||||||||||||
| 1258 | 0 | my $response = $ua->post( | |||||||||||||||||||||||||||
| 1259 | $self->token_uri, | ||||||||||||||||||||||||||||
| 1260 | { | ||||||||||||||||||||||||||||
| 1261 | grant_type => encode_entities('urn:ietf:params:oauth:grant-type:jwt-bearer'), | ||||||||||||||||||||||||||||
| 1262 | assertion => $jwt | ||||||||||||||||||||||||||||
| 1263 | } | ||||||||||||||||||||||||||||
| 1264 | ); | ||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||
| 1266 | 0 | ▶ | unless ($response->is_success()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1267 | 0 | LOGDIE $response->code, $response->content; | |||||||||||||||||||||||||||
| 1268 | } | ||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||
| 1270 | 0 | my $data = decode_json($response->content); | |||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||
| 1272 | 0 | ▶ | if(!$data->{access_token}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1273 | 0 | LOGDIE "Authentication failed with error code: ", $response->code, $response->content; | |||||||||||||||||||||||||||
| 1274 | 0 | return 0; | |||||||||||||||||||||||||||
| 1275 | } | ||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||
| 1277 | 0 | $self->{oauth}->{_access_token} = $data->{access_token}; | |||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||
| 1279 | # expires_in is number of seconds the token is valid, storing the validity epoch | ||||||||||||||||||||||||||||
| 1280 | 0 | $self->{oauth}->{_expires} = $data->{expires_in} + time; | |||||||||||||||||||||||||||
| 1281 | 0 | return 1; | |||||||||||||||||||||||||||
| 1282 | } | ||||||||||||||||||||||||||||
| 1283 | |||||||||||||||||||||||||||||
| 1284 | # ========================================== _token_expire ==================================== # | ||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||
| 1286 | sub _token_expire | ||||||||||||||||||||||||||||
| 1287 | { | ||||||||||||||||||||||||||||
| 1288 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_token_expire: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1289 | 0 | $self->{oauth}->{_expires} = time - 1; | |||||||||||||||||||||||||||
| 1290 | } | ||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||
| 1292 | 1 | 1; | |||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||
| 1294 | __END__ | ||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||
| 1296 | =back | ||||||||||||||||||||||||||||
| 1297 | |||||||||||||||||||||||||||||
| 1298 | =head1 Error handling | ||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||
| 1300 | In case of an error while retrieving information from the Google Drive | ||||||||||||||||||||||||||||
| 1301 | API, the methods above will return C<undef> and a more detailed error | ||||||||||||||||||||||||||||
| 1302 | message can be obtained by calling the C<error()> method: | ||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||
| 1304 | print "An error occurred: ", $gd->error(); | ||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||
| 1306 | =head1 LOGGING/DEBUGGING | ||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||
| 1308 | WWW::Google::Drive is Log4perl-enabled. | ||||||||||||||||||||||||||||
| 1309 | To find out what's going on under the hood, turn on Log4perl: | ||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||
| 1311 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 1312 | Log::Log4perl->easy_init($DEBUG); | ||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||
| 1314 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||
| 1316 | L<https://github.com/dinesh-it/www-google-drive> | ||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||
| 1318 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 1319 | |||||||||||||||||||||||||||||
| 1320 | Net::Google::Drive::Simple | ||||||||||||||||||||||||||||
| 1321 | Net::GoogleDrive | ||||||||||||||||||||||||||||
| 1322 | |||||||||||||||||||||||||||||
| 1323 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||
| 1325 | Copyright 2016 by Dinesh Dharmalingam, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||
| 1327 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||
| 1329 | Dinesh D, <dinesh@exceleron.com> | ||||||||||||||||||||||||||||
| 1330 | |||||||||||||||||||||||||||||
| 1331 | =cut | ||||||||||||||||||||||||||||
| 1332 | |||||||||||||||||||||||||||||