lib/Mailru/Cloud.pm

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