| 1 | package App::ggdrv::update ; | ||
| 2 | use strict ; | ||
| 3 | use warnings ; | ||
| 4 | use feature 'say' ; | ||
| 5 | use HTTP::Request::Common ; | ||
| 6 | use JSON qw[ encode_json ] ; | ||
| 7 | use LWP::UserAgent ; | ||
| 8 | use URI::QueryParam ; | ||
| 9 | use URI ; | ||
| 10 | |||
| 11 | my ($GOOGLE_DRIVE_UPLOAD_API , $gfile , $atoken ) ; | ||
| 12 | return 1 ; | ||
| 13 | |||
| 14 | sub update { | ||
| 15 | $GOOGLE_DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3/files/"; | ||
| 16 | $gfile = $ENV{ GGDRV_API } // "~/.ggdrv2303v1" ; | ||
| 17 | $atoken = qx [ sed -ne's/^ACCESS_TOKEN[ =:\t]*//p' $gfile ] =~ s/\n$//r ; | ||
| 18 | while ( my ($file,$id) = splice @ARGV , 0 , 2 ) { f_each ($file,$id) } | ||
| 19 | # & f_each ( split /:/, $_ , 2 ) for @ARGV ; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub f_each ( $$ ) { | ||
| 23 | my $URI = URI->new( $GOOGLE_DRIVE_UPLOAD_API . $_[1] ); | ||
| 24 | $URI->query_param( uploadType => 'multipart' ); | ||
| 25 | my $ua = LWP::UserAgent->new; | ||
| 26 | my $res = $ua->request( | ||
| 27 | PATCH $URI, | ||
| 28 | 'Content-Type' => 'multipart/form-data', | ||
| 29 | Authorization => "Bearer $atoken" , | ||
| 30 | Content => [ | ||
| 31 | metadata => [ | ||
| 32 | undef, undef, # undef => undef と書くことは出来るだろうか? | ||
| 33 | 'Content-Type' => 'application/json;charset=UTF-8', | ||
| 34 | 'Content' => encode_json( {} ) #name=>'temp.txt', mimeType=>'text/plain', parents => ['10_33chars_in_total'], id => $target_fileid}, | ||
| 35 | ], | ||
| 36 | file => [ $_[0] ] #["./anotherName.txt"], | ||
| 37 | ], | ||
| 38 | ); | ||
| 39 | print $res->code . "\n"; | ||
| 40 | print $res->content . "\n"; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | =encoding utf8 | ||
| 45 | |||
| 46 | =head1 | ||
| 47 | |||
| 48 | ggdrv --update ファイル名1 ファイルid1 [ファイル名2 ファイルid2] [ファイル名3 ファイルid3] .. | ||
| 49 | |||
| 50 | Googleドライブのフォルダーの指定するファイルIDにローカルのファイルを更新する。 | ||
| 51 | 引数の数は、偶数個とすること。 | ||
| 52 | ローカルのファイル名は、アップロード先のファイル名とは一致しないことがある(そのファイルIDの前のファイルと同じものになる)。 | ||
| 53 | |||
| 54 | 開発上のメモ: 複数のファイルに対応したい。 | ||
| 55 | 標準出力への出力の例: | ||
| 56 | |||
| 57 | # 200 | ||
| 58 | # { | ||
| 59 | # "kind": "drive#file", | ||
| 60 | # "id": "1..(33文字)....f", | ||
| 61 | # "name": "example.txt", | ||
| 62 | # "mimeType": "text/plain" | ||
| 63 | # } | ||
| 64 | |||
| 65 | アクセストークンの有効期限の30分が切れていたりすると、上記は"message": "Invalid Credentials"が現れるであろう。 |