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