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