ggdrv/upload.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 70 CC 2 SCAR 10.0
CC
2
Cov
0%
f_each
6.0
upload
2.0
BEGIN
2.0
CRAP 2.7
1 package App::ggdrv::upload ;
2 use strict;
3 use warnings;
4 use feature 'say' ;
5 use HTTP::Request::Common;
6 use JSON qw/encode_json decode_json/;
7 use LWP::UserAgent;
8 use Getopt::Std ;
9 use Carp;
10  
11 my ($gfile , $atoken, $GOOGLE_DRIVE_UPLOAD_API , %o);
12 return 1 ;
13  
14 sub upload {
15 getopts 'f:m:' , \%o ;
16 $o{f} //= '' ; # フォルダ名
17 $o{m} //= 'text/plain' ; # MIMEタイプ
18 $gfile = $ENV{ GGDRV_API } // "~/.ggdrv2303v1" ;
19 $atoken = qx [ sed -ne's/^ACCESS_TOKEN[ =:\t]*//p' $gfile ] =~ s/\n$//r ;
20 $GOOGLE_DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" ;
21 & f_each ( $_ ) for @ARGV ;
22 }
23  
24 sub f_each ( $ ) {
25 binmode STDOUT, ":utf8";
26 my $mimeType = $o{m} ;
27 my $ej1 = encode_json { name => $_[0] , mimeType => $mimeType , $o{f} ne q[] ? ( parents => [ $o{f} ] ): () } ;
28 my $res = LWP::UserAgent -> new -> request (
29 POST $GOOGLE_DRIVE_UPLOAD_API ,
30 'Content-Type' => 'multipart/form-data' ,
31 Authorization => "Bearer $atoken" ,
32 Content => [
33 metadata => [ undef, undef , 'Content-Type' => 'application/json;charset=UTF-8' , 'Content' => $ej1 ] ,
34 file => [ $_[0] ] ,
35 ] ,
36 ) ;
37 my $j = decode_json ( $res->content ) ;
38 say join "\t" , map { $_ // 'undef' } $res->code , $j->{id}, $j->{name}, $j->{mimeType} , $j->{kind} ;
39 }
40  
41 =encoding utf8
42  
43 =head1
44  
45 $0 -f 目的のフォルダID ファイル名 [ファイル名] [ファイル名] ..
46  
47 指定したファイルを指定したGoogleドライブのフォルダにアップロードする。
48 (同じ名前のファイルも複数回、このプログラムを実行すると、新規に次々とGoogleドライブにアップロードされる。少し要注意。)
49  
50 このプログラムは HTTP::Request::Common を用いていて、Net::Google::OAuthを使わない。
51  
52 オプション:
53 -f STR : 指定しないか、空文字だと、グーグル直下のディレクトリになる。
54 -m TYPE : text/csv などを指定。 未指定なら text/plain ;
55  
56  
57 出力例:
58  
59 # 200
60 # {
61 # "kind": "drive#file",
62 # "id": "1...(全部で33文字-_英数大文字小文字)bag",
63 # "name": "test.txt",
64 # "mimeType": "text/plain"
65 # }
66  
67  
68 =cut