lib/Mailru/Cloud.pm

filter: Statementstmt 23.0% 41 / 178 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.1% 61 / 288 CC 33 SCAR 33.4
CC
33
Cov
17%
uploadFile
72.0
__upload_file
56.0
listFiles
42.0
CRAP 28.3
« blib/lib/Mailru/Cloud/Auth.pm ↑ Summary blib/lib/Mailru/Cloud.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.08';
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://cld-upload10.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 $DB::single = 1;
80 0 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 0 ▶ if (not $FL) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless ($FL) 0 0 0
83 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
84 0 binmode $FL;
85 }
86 0 print $FL $_[0];
87 0 });
88 0 my $code = $res->code;
89 0 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 0 0
90 0 croak "Cant download file $cloud_file to $file. Code: $code";
91 }
92 0 ▶ close $FL if $FL;
Branch: 1 branch
BranchTrueFalseTotal
if $FL 0 0 0
93 0 return 1;
94 }
95  
96 sub createFolder {
97 0 ▶ my ($self, %opt) = @_;
Subroutine createFolder: not called CC=4 SCAR=30.0
Pod documented
98 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
99  
100 0 $self->__isLogin();
101  
102 0 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 0 'x-page-id' => $self->{'x-page-id'},
112 );
113 0 my $res = $ua->post('https://cloud.mail.ru/api/v2/folder/add', \%param);
114  
115 0 my $code = $res->code;
116 0 ▶ if ($code eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 200) 0 0 0
117 0 return 1;
118 }
119 0 ▶ if ($code eq '400') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 400) 0 0 0
120 0 carp "Can't create folder $path. Folder exists";
121 0 return;
122 }
123 0 croak "Cant create folder $path. Code: $code";
124  
125 }
126  
127 sub deleteResource {
128 0 ▶ my ($self, %opt) = @_;
Subroutine deleteResource: not called CC=3 SCAR=24.8
Pod documented
129 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
130  
131 0 $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 0 'x-page-id' => $self->{'x-page-id'},
141 );
142  
143 0 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/file/remove', \%param);
144 0 my $code = $res->code;
145  
146 0 ▶ if ($code eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 200) 0 0 0
147 0 return 1;
148 }
149 0 croak "Cant remove $path. Code: $code";
150 }
151  
152 sub emptyTrash {
153 0 ▶ my $self = shift;
Subroutine emptyTrash: not called CC=2 SCAR=17.9
Pod documented
154  
155 0 $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 0 'x-page-id' => $self->{'x-page-id'},
164 );
165  
166 0 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/trashbin/empty', \%param);
167 0 my $code = $res->code;
168  
169 0 ▶ if ($code eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 200) 0 0 0
170 0 return 1;
171 }
172 0 croak "Cant empty trash. Code: $code";
173 }
174  
175 sub listFiles {
176 0 ▶ my ($self, %opt) = @_;
Subroutine listFiles: not called CC=6 SCAR=37.4
Pod documented
177 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
178 0 my $orig_path = $path;
179  
180 0 $self->__isLogin();
181 0 $path = uri_escape($path);
182 0 my $res = $self->{ua}->get('https://cloud.mail.ru/api/v2/folder' . '?token=' . $self->{authToken} . '&home=' . $path);
183 0 my $code = $res->code;
184 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
185 0 my $json_parsed = decode_json($res->content);
186 0 my @list_files;
187  
188 0 for my $item (@{$json_parsed->{body}->{list}}) {
189 my $h = {
190 'type' => $item->{type},
191 'name' => $item->{name},
192 'size' => $item->{size},
193 0 };
194 0 ▶ if ($item->{weblink}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"weblink"}) 0 0 0
195 0 $h->{weblink} = 'https://cloud.mail.ru/public/' . $item->{weblink};
196 }
197 0 push @list_files, $h;
198 }
199 0 return \@list_files;
200 }
201 0 ▶ if ($code eq '404') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 404) 0 0 0
202 0 croak "Folder $orig_path not exists";
203 }
204 0 croak "Cant get file list for path: $orig_path. Code: $code";
205 }
206  
207 sub shareResource {
208 0 ▶ my ($self, %opt) = @_;
Subroutine shareResource: not called CC=3 SCAR=24.8
Pod documented
209 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
210  
211 #Добавим слеш в начало, если его нет
212 0 $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 0 'x-page-id' => $self->{'x-page-id'},
222 );
223  
224 0 my $res = $self->{ua}->post('https://cloud.mail.ru/api/v2/file/publish', \%param);
225 0 my $code = $res->code;
226 0 ▶ if ($code ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200) 0 0 0
227 0 croak "Error on shareResource. Path: $path. Code: $code";
228 }
229 0 my $json = decode_json($res->decoded_content);
230 0 my $link = 'https://cloud.mail.ru/public/' . $json->{body};
231 0 return $link;
232 }
233  
234 sub __upload_file {
235 0 ▶ my ($self, $url, $file) = @_;
Subroutine __upload_file: not called CC=7 SCAR=40.3
236  
237 0 my $u1 = URI->new($url);
238  
239 # $IO::Socket::SSL::DEBUG = 5;
240 0 my $host = $u1->host;
241 0 my $port = $u1->port;
242 0 my $path = $u1->path;
243  
244 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
245 PeerAddr => $host,
246 PeerPort => $port,
247 Proto => 'tcp',
248 ) or croak "Cant connect to $host:$port";
249 0 binmode $sock;
250 0 $sock->autoflush(1);
251  
252 #Generate boundary
253 0 my $boundary = '5';
254 0 for (1..20) {
255 0 $boundary .= int(rand(10) + 1);
256 }
257 0 $boundary = '----------------------------' . $boundary;
258  
259 0 my $content_disposition = 'Content-Disposition: form-data; name="file"; filename="' . basename($file) . '"' . "\n";
260 0 $content_disposition .= "Content-Type: text/plain\n\n";
261 0 my $length = (stat $file)[7];
262  
263 0 my @cookie_arr;
264 0 ▶ $self->{ua}->cookie_jar->scan(sub {push @cookie_arr, "$_[1]=$_[2]"});
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 my $cookie = join('; ', @cookie_arr);
266  
267  
268 0 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 0 for my $head (@headers) {
283 0 $sock->print($head . "\n");
284 }
285  
286 0 $sock->print("Content-Length: $length\n");
287 0 $sock->print("\n");
288  
289  
290 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
291 0 binmode $FH;
292 0 my $filebuf;
293 0 ▶ while (my $bytes = read($FH, $filebuf, $BUFF_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bytes = read($FH, $filebuf, $BUFF_SIZE)) 0 0 0
294 0 $sock->print($filebuf);
295 }
296 0 $sock->print("\n");
297  
298 0 my @answer = $sock->getlines();
299 0 $sock->close();
300  
301 #Если запрос успешен
302 0 ▶ if ($answer[0] =~ /201/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer[0] =~ /201/) 0 0 0
303 #Возврат хэша файла
304 0 return (pop @answer, $length);
305 }
306  
307 0 return;
308 }
309  
310 ################################## ACCESSORS ##########################3
311 #
312 sub get_last_uploaded_file_hash {
313 0 ▶ return $_[0]->{file_hash};
Subroutine get_last_uploaded_file_hash: not called CC=1 SCAR=6.9
Pod undocumented
314 }
315  
316 1 1;
317  
318 __END__
« blib/lib/Mailru/Cloud/Auth.pm ↑ Summary blib/lib/Mailru/Cloud.pm »