| 1 | package Yandex::Disk; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Yandex::Disk::Public; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw/croak carp/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use IO::Socket::SSL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | my $WAIT_RETRY = 20; | |||||||||||||||||||||||||||
| 19 | 1 | my $BUFF_SIZE = 8192; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub new { | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 1 | my %opt = @_; | |||||||||||||||||||||||||||
| 24 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | $self->{token} = $opt{-token} || croak "Specify -token param"; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opt{'-token'} || croak('Specify -token param')
MC/DC0%0/2
$opt{'-token'} || croak('Specify -token param')
$opt{'-token'}
croak('Specify -token param')
Truth table33%1/3
A || B
A= $opt{'-token'}B= croak('Specify -token param')
| |||||||||||||||||||||||||||||
| 26 | 1 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 27 | 1 | $ua->agent("Yandex::Disk perl module"); | |||||||||||||||||||||||||||
| 28 | 1 | $ua->default_header('Accept' => 'application/json'); | |||||||||||||||||||||||||||
| 29 | 1 | $ua->default_header('Content-Type' => 'application/json'); | |||||||||||||||||||||||||||
| 30 | 1 | $ua->default_header('Connection' => 'keep-alive'); | |||||||||||||||||||||||||||
| 31 | 1 | $ua->default_header('Authorization' => 'OAuth ' . $self->{token}); | |||||||||||||||||||||||||||
| 32 | 1 | $self->{ua} = $ua; | |||||||||||||||||||||||||||
| 33 | 1 | $self->{public_info} = {}; | |||||||||||||||||||||||||||
| 34 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub getDiskInfo { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
getDiskInfo: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $url = 'https://cloud-api.yandex.net/v1/disk/'; | |||||||||||||||||||||||||||
| 41 | 0 | my $res = $self->__request($url, "GET"); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return __fromJson($res->decoded_content); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||||||||||
| 46 | 0 | croak "Cant execute request to $url: " . $res->status_line; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub uploadFile { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
uploadFile: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 53 | 0 | my $overwrite = $opt{-overwrite}; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $file = $opt{-file} || croak "Specify -file param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-file'} || croak('Specify -file param')
MC/DC0%0/2
$opt{'-file'} || croak('Specify -file param')
$opt{'-file'}
croak('Specify -file param')
Truth table0%0/3
A || B
A= $opt{'-file'}B= croak('Specify -file param')
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $remote_path = $opt{-remote_path} || croak "Specify -remote_path param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-remote_path'} || croak('Specify -remote_path param')
MC/DC0%0/2
$opt{'-remote_path'} || croak('Specify -remote_path param')
$opt{'-remote_path'}
croak('Specify -remote_path param')
Truth table0%0/3
A || B
A= $opt{'-remote_path'}B= croak('Specify -remote_path param')
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $overwrite = 1 if not defined $overwrite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (not -f $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | croak "File $file not exists"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $overwrite = $overwrite ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #Delete end slash | ||||||||||||||||||||||||||||
| 65 | 0 | $remote_path =~ s/^\/|\/$//g; | |||||||||||||||||||||||||||
| 66 | 0 | $remote_path = sprintf("/%s/%s", $remote_path, basename($file)); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $param = "?path=" . uri_escape($remote_path) . "&overwrite=$overwrite"; | |||||||||||||||||||||||||||
| 69 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources/upload' . $param, "GET"); | |||||||||||||||||||||||||||
| 70 | 0 | my $url_to_upload; | |||||||||||||||||||||||||||
| 71 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($code eq '409') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | 0 | croak "Folder $remote_path not exists"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ($code eq '200') { | ||||||||||||||||||||||||||||
| 76 | 0 | $url_to_upload = __fromJson($res->decoded_content)->{href}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | croak "Cant uploadFile: " . $res->status_line; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $upl_code = __upload_file($url_to_upload, $file); | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($upl_code ne '201') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | croak "Cant upload file. Code: $code"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | return 1; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub createFolder { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
createFolder: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $path = $opt{-path} || croak "Specify -path param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-path'} || croak('Specify -path param')
MC/DC0%0/2
$opt{'-path'} || croak('Specify -path param')
$opt{'-path'}
croak('Specify -path param')
Truth table0%0/3
A || B
A= $opt{'-path'}B= croak('Specify -path param')
| |||||||||||||||||||||||||||||
| 93 | 0 | my $recursive = $opt{-recursive}; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($recursive) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | my @half_path; | |||||||||||||||||||||||||||
| 97 | 0 | for my $l_path (split /\//, $path) { | |||||||||||||||||||||||||||
| 98 | 0 | push @half_path, $l_path; | |||||||||||||||||||||||||||
| 99 | 0 | $self->createFolder( -path => join('/', @half_path) ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources/?path=' . uri_escape($path), "PUT"); | |||||||||||||||||||||||||||
| 104 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($code eq '409') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | #Папка или существует или не создана родительская папка | ||||||||||||||||||||||||||||
| 107 | 0 | my $json_res = __fromJson($res->decoded_content); | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($json_res->{error} eq 'DiskPathPointsToExistentDirectoryError') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | return 1; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | croak $json_res->{description}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | elsif ($code ne '201') { | ||||||||||||||||||||||||||||
| 114 | 0 | croak "Cant create folder $path. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return 1; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | sub deleteResource { | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
deleteResource: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $path = $opt{-path} || croak "Specify -path param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-path'} || croak('Specify -path param')
MC/DC0%0/2
$opt{'-path'} || croak('Specify -path param')
$opt{'-path'}
croak('Specify -path param')
Truth table0%0/3
A || B
A= $opt{'-path'}B= croak('Specify -path param')
| |||||||||||||||||||||||||||||
| 124 | 0 | my $wait = $opt{-wait}; | |||||||||||||||||||||||||||
| 125 | 0 | my $permanently = $opt{-permanently}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $permanently = $permanently ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources?path=' . uri_escape($path) . "&permanently=$permanently", "DELETE"); | |||||||||||||||||||||||||||
| 130 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($code eq '204') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | #Free folder | ||||||||||||||||||||||||||||
| 133 | 0 | return 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | elsif ($code eq '202') { | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($wait) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | my $href = __fromJson($res->decoded_content)->{href}; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | $self->__waitResponse($href, $WAIT_RETRY) or croak "Timeout to wait response. Try increase $WAIT_RETRY variable"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | return 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 0 | croak "Cant delete $path. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub downloadFile { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
downloadFile: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $path = $opt{-path} || croak "Specify -path param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-path'} || croak('Specify -path param')
MC/DC0%0/2
$opt{'-path'} || croak('Specify -path param')
$opt{'-path'}
croak('Specify -path param')
Truth table0%0/3
A || B
A= $opt{'-path'}B= croak('Specify -path param')
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $file = $opt{-file} || croak "Specify -file param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-file'} || croak('Specify -file param')
MC/DC0%0/2
$opt{'-file'} || croak('Specify -file param')
$opt{'-file'}
croak('Specify -file param')
Truth table0%0/3
A || B
A= $opt{'-file'}B= croak('Specify -file param')
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . uri_escape($path), "GET"); | |||||||||||||||||||||||||||
| 154 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($code ne '200') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | croak "Error on request file $path: " . $res->status_line; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | my $download_url = __fromJson($res->decoded_content)->{href}; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $self->__download($download_url, $file); | |||||||||||||||||||||||||||
| 161 | 0 | return 1; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub emptyTrash { | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
emptyTrash: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $path = $opt{-path} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'-path'} || ''
MC/DC0%0/1
$opt{'-path'} || ''
$opt{'-path'}
Truth table0%0/2
A
A= $opt{'-path'}
| |||||||||||||||||||||||||||||
| 168 | 0 | my $wait = $opt{-wait}; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $param = $path ? '?path=' . uri_escape($path) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/trash/resources/' . $path, 'DELETE'); | |||||||||||||||||||||||||||
| 172 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ($code eq '204') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 174 | 0 | return 1; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | elsif ($code eq '202') { | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($wait) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | my $href = __fromJson($res->decoded_content)->{href}; | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | $self->__waitResponse($href, $WAIT_RETRY) or croak "Timeout to wait response. Try increase $WAIT_RETRY variable"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 0 | return 1; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | $path = "by path $path" if $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | croak "Cant empty trash$path. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub listFiles { | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
listFiles: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $path = $opt{-path} || croak "Specify -path param"; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opt{'-path'} || croak('Specify -path param')
MC/DC0%0/2
$opt{'-path'} || croak('Specify -path param')
$opt{'-path'}
croak('Specify -path param')
Truth table0%0/3
A || B
A= $opt{'-path'}B= croak('Specify -path param')
| |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $limit = $opt{-limit} || 999999; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'-limit'} || 999999
MC/DC0%0/1
$opt{'-limit'} || 999999
$opt{'-limit'}
Truth table0%0/2
A
A= $opt{'-limit'}
| |||||||||||||||||||||||||||||
| 194 | 0 | my $offset = $opt{-offset}; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | $offset = 0 if not $offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | my $param = '?path=' . uri_escape($path) . "&limit=$limit&offset=$offset&fields=_embedded.items"; | |||||||||||||||||||||||||||
| 198 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources' . $param, 'GET'); | |||||||||||||||||||||||||||
| 199 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ($code ne '200') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | croak "Error on listFiles. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | my $json_res = __fromJson($res->decoded_content); | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | return $json_res->{_embedded}->{items}; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub listAllFiles { | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
listAllFiles: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | my $limit = $opt{-limit} || 999999; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'-limit'} || 999999
MC/DC0%0/1
$opt{'-limit'} || 999999
$opt{'-limit'}
Truth table0%0/2
A
A= $opt{'-limit'}
| |||||||||||||||||||||||||||||
| 212 | 0 | my $media_type = $opt{-media_type}; | |||||||||||||||||||||||||||
| 213 | 0 | my $offset = $opt{-offset}; | |||||||||||||||||||||||||||
| 214 | 0 | ▶ | $offset = 0 if not $offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my $param = "?limit=$limit&offset=$offset"; | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | $param .= '&media_type=' . $media_type if $media_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources/files' . $param, 'GET'); | |||||||||||||||||||||||||||
| 220 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | if ($code ne '200') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | croak "Error on listFiles. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 0 | my $json_res = __fromJson($res->decoded_content); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | return $json_res->{items}; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | sub lastUploadedFiles { | ||||||||||||||||||||||||||||
| 230 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lastUploadedFiles: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 0 | my %opt = @_; | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | my $limit = $opt{-limit} || 999999; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'-limit'} || 999999
MC/DC0%0/1
$opt{'-limit'} || 999999
$opt{'-limit'}
Truth table0%0/2
A
A= $opt{'-limit'}
| |||||||||||||||||||||||||||||
| 233 | 0 | my $media_type = $opt{-media_type}; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | my $param = "?limit=$limit"; | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | $param .= '&media_type=' . $media_type if $media_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my $res = $self->__request('https://cloud-api.yandex.net/v1/disk/resources/last-uploaded' . $param, 'GET'); | |||||||||||||||||||||||||||
| 239 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | if ($code ne '200') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | croak "Error on listFiles. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 0 | my $json_res = __fromJson($res->decoded_content); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | return $json_res->{items}; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | sub public { | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
public: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 251 | 0 | return Yandex::Disk::Public->new( -token => $self->{token} ); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | sub __download { | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | my ($self, $url, $fname) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__download: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 256 | 0 | my $ua = $self->{ua}; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | ▶ | open my $FL, ">$fname" or croak "Cant open $fname to write $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | binmode $FL; | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | my $res = $ua->get($url, ':read_size_hint' => $BUFF_SIZE, ':content_cb' => sub {print $FL $_[0];}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 261 | 0 | close $FL; | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ($res->code eq '200') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | return 1; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 0 | croak "Cant download file $url to $fname. Error: " . $res->status_line; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | sub __waitResponse { | ||||||||||||||||||||||||||||
| 269 | #Дожидается ответа о статусе операции | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | my ($self, $url, $retry) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__waitResponse: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | while ($retry > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | my $res = $self->__request($url, "GET"); | |||||||||||||||||||||||||||
| 274 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||
| 275 | 0 | ▶ | if ($code eq '200' && __fromJson($res->decoded_content)->{status} eq 'success') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code eq 200 and __fromJson($res->decoded_content)->{"status"} eq "success"
MC/DC0%0/2
$code eq 200 and __fromJson($res->decoded_content)->{"status"} eq "success"
$code eq 200
__fromJson($res->decoded_content)->{"status"} eq "success"
Truth table0%0/3
A and B
A= $code eq 200B= __fromJson($res->decoded_content)->{"status"} eq "success"
| |||||||||||||||||||||||||||||
| 276 | 0 | return 1; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 279 | 0 | $retry--; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 0 | return; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | sub __upload_file { | ||||||||||||||||||||||||||||
| 286 | #Buffered chunked upload file | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | my ($url, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__upload_file: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | my $u1 = URI->new($url); | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # $IO::Socket::SSL::DEBUG = 3; | ||||||||||||||||||||||||||||
| 292 | 0 | my $host = $u1->host; | |||||||||||||||||||||||||||
| 293 | 0 | my $port = $u1->port; | |||||||||||||||||||||||||||
| 294 | 0 | my $path = $u1->path; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | my $sock = IO::Socket::SSL->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | PeerAddr => $host, | ||||||||||||||||||||||||||||
| 298 | PeerPort => $port, | ||||||||||||||||||||||||||||
| 299 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 300 | ) or croak "Cant connect to $host:$port"; | ||||||||||||||||||||||||||||
| 301 | 0 | binmode $sock; | |||||||||||||||||||||||||||
| 302 | 0 | $sock->autoflush(1); | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | $sock->print("PUT $path HTTP/1.1\n"); | |||||||||||||||||||||||||||
| 305 | 0 | $sock->print("HOST: $host\n"); | |||||||||||||||||||||||||||
| 306 | 0 | $sock->print("Connection: close\n"); | |||||||||||||||||||||||||||
| 307 | 0 | $sock->print("Content-Type: application/json\n"); | |||||||||||||||||||||||||||
| 308 | 0 | $sock->print("Transfer-Encoding: chunked\n"); | |||||||||||||||||||||||||||
| 309 | 0 | $sock->print("\n"); | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | open my $FH, "<$file" or croak "Cant open $file $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | binmode $FH; | |||||||||||||||||||||||||||
| 313 | 0 | my $filebuf; | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | while (my $bytes = read($FH, $filebuf, $BUFF_SIZE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | my $hex = sprintf("%X", $bytes); | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | $sock->print($hex) or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | $sock->print("\r\n") or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | ▶ | $sock->print($filebuf) or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 0 | ▶ | $sock->print("\r\n") or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | 0 | close $FH; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $sock->print("0\r\n") or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | $sock->print("\r\n") or croak "Cant print to socket"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 0 | my @answer = $sock->getlines(); | |||||||||||||||||||||||||||
| 328 | 0 | $sock->close(); | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 0 | my ($code) = $answer[0] =~ /(\d{3})/; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | return $code; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | sub __request { | ||||||||||||||||||||||||||||
| 337 | 4 | ▶ | my ($self, $url, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__request: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 4 | my $ua = $self->{ua}; | |||||||||||||||||||||||||||
| 340 | 4 | my $req = HTTP::Request->new($type => $url); | |||||||||||||||||||||||||||
| 341 | 4 | my $res = $ua->request($req); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 4 | return $res; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | sub __fromJson { | ||||||||||||||||||||||||||||
| 347 | 3 | ▶ | my $string = ref($_[0]) ? $_[1] : $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
__fromJson: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 3 | my $res = JSON::XS::decode_json($string); | |||||||||||||||||||||||||||
| 349 | 3 | return $res; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | sub errstr { | ||||||||||||||||||||||||||||
| 354 | 0 | ▶ | return shift->{errstr}; | ||||||||||||||||||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 1 | 1; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | __END__ | ||||||||||||||||||||||||||||