blib/lib/Mailru/Cloud.pm untested

filter: Statementstmt 0.0% 0 / 182 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 269 CC 17 SCAR 24.1
CC
17
Cov
0%
uploadFile
30.0
downloadFile
20.0
listFiles
20.0
CRAP 11.1
1 package Mailru::Cloud;
2  
3 use 5.008001;
4 use strict;
5 use warnings;
6 use utf8;
7 use open qw(:std :utf8);
8 use Carp qw/croak carp/;
9 use URI::Escape;
10 use File::Basename;
11 use HTTP::Request;
12 use JSON::XS;
13 use Encode;
14 use IO::Socket::SSL;
15 use base qw/Mailru::Cloud::Auth/;
16  
17 our $VERSION = '0.08';
18  
19 my $BUFF_SIZE = 512;
20  
21 sub uploadFile {
22 my ($self, %opt) = @_;
23 my $upload_file = $opt{'-file'} || croak "You must specify -file param for method uploadFile";
24 my $path = $opt{'-path'} || '/';
25 my $rename = $opt{'-rename'};
26 $self->{file_hash} = undef;
27  
28 $self->__isLogin();
29  
30 my $conflict_mode = $rename ? 'rename' : 'rewrite';
31  
32 if (not -f $upload_file) {
33 croak "File $upload_file not exist";
34 }
35  
36 if ($path !~ /\/$/) {
37 $path .= '/';
38 }
39  
40 my $request = 'https://cld-upload10.cloud.mail.ru/upload/?' .'cloud_domain=2&x-email=' . uri_escape($self->{email});
41  
42 my ($file_hash, $size) = $self->__upload_file($request, $upload_file) or return;
43 $self->{file_hash} = $file_hash;
44  
45 #Опубликуем файл
46 my %param = (
47 'api' => '2',
48 'build' => $self->{build},
49 'conflict' => $conflict_mode,
50 'email' => $self->{email},
51 'hash' => $file_hash,
52 'home' => $path . basename($upload_file),
53 'size' => $size,
54 'token' => $self->{authToken},
55 'x-email' => $self->{email},
56 'x-page-id' => $self->{'x-page-id'},
57 );
58 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/file/add', \%param);
59  
60 my $code = $res->code;
61 if ($code eq '200') {
62 my $json = JSON::XS::decode_json($res->content);
63 my $new_fname = $json->{body};
64 return $new_fname;
65 }
66  
67 croak "Cant upload file $upload_file. Code: $code " . $res->decoded_content . "\n";
68 }
69  
70 sub downloadFile {
71 my ($self, %opt) = @_;
72 my $file = $opt{-file} || croak "You must specify -file param for method downloadFile";
73 my $cloud_file = $opt{-cloud_file} || croak "You must specify -cloud_file param for method downloadFile";
74  
75 $self->__isLogin();
76  
77 my $FL;
78 my $ua = $self->{ua};
79 $DB::single = 1;
80 my $url = 'https://cloclo5.datacloudmail.ru/get/' . uri_escape($cloud_file) . '?x-email=' . uri_escape($self->{email});
81 my $res = $ua->get($url, ':read_size_hint' => $BUFF_SIZE, ':content_cb' => sub {
82 if (not $FL) {
83 open $FL, ">$file" or croak "Cant open $file to write $!";
84 binmode $FL;
85 }
86 print $FL $_[0];
87 });
88 my $code = $res->code;
89 if ($code ne '200') {
90 croak "Cant download file $cloud_file to $file. Code: $code";
91 }
92 close $FL if $FL;
93 return 1;
94 }
95  
96 sub createFolder {
97 my ($self, %opt) = @_;
98 my $path = $opt{-path} || croak "You must specify -path param for method createFolder";
99  
100 $self->__isLogin();
101  
102 my $ua = $self->{ua};
103 my %param = (
104 'api' => '2',
105 'build' => $self->{build},
106 'conflict' => 'strict',
107 'email' => $self->{email},
108 'home' => $path,
109 'token' => $self->{authToken},
110 'x-email' => $self->{email},
111 'x-page-id' => $self->{'x-page-id'},
112 );
113 my $res = $ua->post('https://cloud.mail.ru/api/v2/folder/add', \%param);
114  
115 my $code = $res->code;
116 if ($code eq '200') {
117 return 1;
118 }
119 if ($code eq '400') {
120 carp "Can't create folder $path. Folder exists";
121 return;
122 }
123 croak "Cant create folder $path. Code: $code";
124  
125 }
126  
127 sub deleteResource {
128 my ($self, %opt) = @_;
129 my $path = $opt{-path} || croak "You must specify -path options for method deleteResource";
130  
131 $self->__isLogin();
132  
133 my %param = (
134 'api' => '2',
135 'build' => $self->{build},
136 'email' => $self->{email},
137 'home' => $path,
138 'token' => $self->{authToken},
139 'x-email' => $self->{email},
140 'x-page-id' => $self->{'x-page-id'},
141 );
142  
143 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/file/remove', \%param);
144 my $code = $res->code;
145  
146 if ($code eq '200') {
147 return 1;
148 }
149 croak "Cant remove $path. Code: $code";
150 }
151  
152 sub emptyTrash {
153 my $self = shift;
154  
155 $self->__isLogin();
156  
157 my %param = (
158 'api' => '2',
159 'build' => $self->{build},
160 'email' => $self->{email},
161 'token' => $self->{authToken},
162 'x-email' => $self->{email},
163 'x-page-id' => $self->{'x-page-id'},
164 );
165  
166 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/trashbin/empty', \%param);
167 my $code = $res->code;
168  
169 if ($code eq '200') {
170 return 1;
171 }
172 croak "Cant empty trash. Code: $code";
173 }
174  
175 sub listFiles {
176 my ($self, %opt) = @_;
177 my $path = $opt{-path} || '/';
178 my $orig_path = $path;
179  
180 $self->__isLogin();
181 $path = uri_escape($path);
182 my $res = $self->{ua}->get('https://cloud.mail.ru/api/v2/folder' . '?token=' . $self->{authToken} . '&home=' . $path);
183 my $code = $res->code;
184 if ($res->is_success) {
185 my $json_parsed = decode_json($res->content);
186 my @list_files;
187  
188 for my $item (@{$json_parsed->{body}->{list}}) {
189 my $h = {
190 'type' => $item->{type},
191 'name' => $item->{name},
192 'size' => $item->{size},
193 };
194 if ($item->{weblink}) {
195 $h->{weblink} = 'https://cloud.mail.ru/public/' . $item->{weblink};
196 }
197 push @list_files, $h;
198 }
199 return \@list_files;
200 }
201 if ($code eq '404') {
202 croak "Folder $orig_path not exists";
203 }
204 croak "Cant get file list for path: $orig_path. Code: $code";
205 }
206  
207 sub shareResource {
208 my ($self, %opt) = @_;
209 my $path = $opt{-path} || croak "You must specify -path param for method shareResource";
210  
211 #Добавим слеш в начало, если его нет
212 $path =~ s/^([^\/])/\/$1/;
213  
214 my %param = (
215 'api' => '2',
216 'build' => $self->{build},
217 'email' => $self->{email},
218 'home' => $path,
219 'token' => $self->{authToken},
220 'x-email' => $self->{email},
221 'x-page-id' => $self->{'x-page-id'},
222 );
223  
224 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/file/publish', \%param);
225 my $code = $res->code;
226 if ($code ne '200') {
227 croak "Error on shareResource. Path: $path. Code: $code";
228 }
229 my $json = decode_json($res->decoded_content);
230 my $link = 'https://cloud.mail.ru/public/' . $json->{body};
231 return $link;
232 }
233  
234 sub __upload_file {
235 my ($self, $url, $file) = @_;
236  
237 my $u1 = URI->new($url);
238  
239 # $IO::Socket::SSL::DEBUG = 5;
240 my $host = $u1->host;
241 my $port = $u1->port;
242 my $path = $u1->path;
243  
244 my $sock = IO::Socket::SSL->new(
245 PeerAddr => $host,
246 PeerPort => $port,
247 Proto => 'tcp',
248 ) or croak "Cant connect to $host:$port";
249 binmode $sock;
250 $sock->autoflush(1);
251  
252 #Generate boundary
253 my $boundary = '5';
254 for (1..20) {
255 $boundary .= int(rand(10) + 1);
256 }
257 $boundary = '----------------------------' . $boundary;
258  
259 my $content_disposition = 'Content-Disposition: form-data; name="file"; filename="' . basename($file) . '"' . "\n";
260 $content_disposition .= "Content-Type: text/plain\n\n";
261 my $length = (stat $file)[7];
262  
263 my @cookie_arr;
264 $self->{ua}->cookie_jar->scan(sub {push @cookie_arr, "$_[1]=$_[2]"});
265 my $cookie = join('; ', @cookie_arr);
266  
267  
268 my @headers = ( "PUT $path HTTP/1.1",
269 "HOST: $host",
270 "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0",
271 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
272 "Accept-Language: en-US,en;q=0.5",
273 "Accept-Encoding: gzip, deflate, br",
274 "Content-Type: multipart/form-data; boundary=$boundary",
275 "Connection: close",
276 "Referer: https://cloud.mail.ru/home/",
277 "Origin: https://cloud.mail.ru",
278 "Cookie: $cookie",
279 "X-Requested-With: XMLHttpRequest",
280 );
281  
282 for my $head (@headers) {
283 $sock->print($head . "\n");
284 }
285  
286 $sock->print("Content-Length: $length\n");
287 $sock->print("\n");
288  
289  
290 open my $FH, "<$file" or croak "Cant open $file $!";
291 binmode $FH;
292 my $filebuf;
293 while (my $bytes = read($FH, $filebuf, $BUFF_SIZE)) {
294 $sock->print($filebuf);
295 }
296 $sock->print("\n");
297  
298 my @answer = $sock->getlines();
299 $sock->close();
300  
301 #Если запрос успешен
302 if ($answer[0] =~ /201/) {
303 #Возврат хэша файла
304 return (pop @answer, $length);
305 }
306  
307 return;
308 }
309  
310 ################################## ACCESSORS ##########################3
311 #
312 sub get_last_uploaded_file_hash {
313 return $_[0]->{file_hash};
314 }
315  
316 1;
317  
318 __END__