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