Weixin/Message/Remote/_get_media.pm

filter: Statementstmt 18.2% 19 / 104 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 11.0% 25 / 227 CC 52 SCAR 71.1
CC
52
Cov
9%
__ANON__
1722.0
_get_media
156.0
BEGIN
1.0
CRAP 1228.5
« Weixin/Plugin/KnowledgeBase.pm ↑ Summary Weixin/Plugin/HwPush.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use File::Temp qw/:seekable/;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶sub Mojo::Weixin::_get_media {
Subroutine _get_media: not called CC=12 SCAR=50.5
8 0 my $self = shift;
9 0 my $msg = shift;
10 0 my $callback = shift;
11  
12 0 my $media_id = (split ":",$msg->media_id)[0];
13 0 my $api;
14 my @query_string;
15 0 my $headers = {};
16 0 ▶ if($msg->media_type eq "image"){
Branch: 5 branches
BranchTrueFalseTotal
if ($msg->media_type eq 'image') { } 0 0 0
elsif ($msg->media_type eq 'voice') { } 0 0 0
elsif ($msg->media_type eq 'video' or $msg->media_type eq 'microvideo') { } 0 0 0
elsif ($msg->media_type eq 'emoticon') { } 0 0 0
elsif ($msg->media_type eq 'file') { } 0 0 0
17 0 $api = 'https://'.$self->domain . '/cgi-bin/mmwebwx-bin/webwxgetmsgimg';
18 0 @query_string = ('' => '',MsgID => $media_id,);
19 0 ▶ push @query_string,(skey=>Mojo::Util::url_escape($self->skey)) if $self->skey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skey 0 0 0
20 }
21 elsif($msg->media_type eq "voice"){
22 0 $api = 'https://'.$self->domain . '/cgi-bin/mmwebwx-bin/webwxgetvoice';
23 0 @query_string = (msgid=>$media_id);
24 0 ▶ push @query_string,(skey=>Mojo::Util::url_escape($self->skey)) if $self->skey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skey 0 0 0
25 }
26 ▶ elsif($msg->media_type eq "video" or $msg->media_type eq "microvideo"){
Condition0%0/3
$msg->media_type eq "video" or $msg->media_type eq "microvideo"
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg->media_type eq "video" or $msg->media_type eq "microvideo"
$msg->media_type eq "video" $msg->media_type eq "microvideo"
Truth table0%0/3
A or B
A= $msg->media_type eq "video"B= $msg->media_type eq "microvideo"
ABresult
1X1
011
000
27 0 $api = 'https://'.$self->domain . '/cgi-bin/mmwebwx-bin/webwxgetvideo';
28 0 @query_string = (msgid=>$media_id);
29 0 ▶ push @query_string,(skey=>Mojo::Util::url_escape($self->skey)) if $self->skey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skey 0 0 0
30 0 $headers = {Range => 'bytes=0-'};
31 }
32 elsif($msg->media_type eq "emoticon"){
33 0 $api = 'https://'.$self->domain . '/cgi-bin/mmwebwx-bin/webwxgetmsgimg';
34 0 @query_string = ('' => '',MsgID => $media_id,type=>'big');
35 0 ▶ push @query_string,(skey=>Mojo::Util::url_escape($self->skey)) if $self->skey;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skey 0 0 0
36 }
37 elsif($msg->media_type eq "file"){
38 0 $api = 'https://file.' . $self->domain . '/cgi-bin/mmwebwx-bin/webwxgetmedia';
39 0 ▶ @query_string = (
Condition0%0/2
$self->pass_ticket || 'undefined'
l!l
00
MC/DC0%0/1
$self->pass_ticket || 'undefined'
$self->pass_ticket
Truth table0%0/2
A
A= $self->pass_ticket
Aresult
11
00
40 sender=>$msg->sender_id,
41 mediaid=>$media_id,
42 filename=>$msg->media_name,
43 fromuser=>$self->wxuin,
44 pass_ticket=>($self->pass_ticket || 'undefined'),
45 webwx_data_ticket=>$self->search_cookie("webwx_data_ticket")
46 );
47 #push @query_string,(skey=>Mojo::Util::url_escape($self->skey)) if $self->skey;
48 }
49 else{
50 0 $self->error("获取media错误:不支持的media类型");
51 0 return;
52 }
53 0 ▶ $self->http_get($self->gen_url($api,@query_string), {Referer=>'https://' . $self->domain .'/'},sub{
Subroutine __ANON__: not called CC=41 SCAR=74.5
54 0 my ($data,$ua,$tx) = @_;
55 0 ▶ if(not defined $data){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
56 0 $self->warn("media[ " . $msg->media_id . " ]下载失败");
57 0 return;
58 }
59 0 ▶ my $mime = $data=~/^GIF8/?"image/gif":$tx->res->headers->content_type;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^GIF8/ ? : 0 0 0
60 0 ▶ my $type = $mime=~/^image\/jpe?g/i ? "jpg"
Branch: 12 branches
BranchTrueFalseTotal
$mime =~ m[^image/jpe?g]i ? : 0 0 0
$mime =~ m[^image/png]i ? : 0 0 0
$mime =~ m[^image/bmp]i ? : 0 0 0
$mime =~ m[^image/gif]i ? : 0 0 0
$mime =~ m[^text/plain]i ? : 0 0 0
$mime =~ m[^text/html]i ? : 0 0 0
$mime =~ m[^text/json]i ? : 0 0 0
$mime =~ m[^application/json]i ? : 0 0 0
$mime =~ m[^video/mp4]i ? : 0 0 0
$mime =~ m[^audio/mp3]i ? : 0 0 0
$mime =~ m[^audio/mpeg]i ? : 0 0 0
$mime =~ m[^application/json]i ? : 0 0 0
61 : $mime=~/^image\/png/i ? "png"
62 : $mime=~/^image\/bmp/i ? "bmp"
63 : $mime=~/^image\/gif/i ? "gif"
64 : $mime=~/^text\/plain/i ? "txt"
65 : $mime=~/^text\/html/i ? "html"
66 : $mime=~/^text\/json/i ? "json"
67 : $mime=~/^application\/json/i ? "json"
68 : $mime=~/^video\/mp4/i ? "mp4"
69 : $mime=~/^audio\/mp3/i ? "mp3"
70 : $mime=~/^audio\/mpeg/i ? "mp3"
71 : $mime=~/^application\/json/i ? "json"
72 : "dat"
73 ;
74 0 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
75 0 $mime=~s/\s*;.*$//g;
76 0 ▶ if($msg->format eq 'media' and $msg->media_type eq 'file'){
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->format eq "media" and $msg->media_type eq "file") 0 0 0
Condition0%0/3
$msg->format eq "media" and $msg->media_type eq "file"
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->format eq "media" and $msg->media_type eq "file"
$msg->format eq "media" $msg->media_type eq "file"
Truth table0%0/3
A and B
A= $msg->format eq "media"B= $msg->media_type eq "file"
ABresult
0X0
100
111
77 0 ▶ if($msg->media_name =~ /^.+?\.([^\.]+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->media_name =~ /^.+?\.([^\.]+)$/) 0 0 0
78 0 ▶ if($1){
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 0 0 0
79 0 $msg->media_ext($1);
80 #$msg->media_mime("application/octet-stream"); #txt文件这样设置mime不合理
81 }
82 }
83 }
84 0 ▶ $msg->media_mime($mime) if not defined $msg->media_mime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg->media_mime 0 0 0
85 0 ▶ $msg->media_ext($type) if not defined $msg->media_ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg->media_ext 0 0 0
86 0 $msg->media_data($data);
87 0 $msg->media_mtime(time);
88 0 $msg->media_size(length($data));
89  
90 0 ▶ if($msg->media_size == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->media_size == 0) 0 0 0
91 0 ▶ $msg->media_path("non-exist-path") if $msg->media_size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $msg->media_size == 0 0 0 0
92 0 $msg->media_name(File::Basename::basename($msg->media_path));
93 0 ▶ $callback->($msg->media_path,$data,$msg) if ref $callback eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback eq "CODE" 0 0 0
94 0 return;
95 }
96 0 ▶ if(defined $self->media_dir and not -d $self->media_dir){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->media_dir and not -d $self->media_dir) 0 0 0
Condition0%0/3
defined $self->media_dir and not -d $self->media_dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->media_dir and not -d $self->media_dir
defined $self->media_dir not -d $self->media_dir
Truth table0%0/3
A and not(B)
A= defined $self->media_dirB= not -d $self->media_dir
ABresult
0X0
100
111
97 0 $self->error("无效的 media_dir: " . $self->media_dir);
98 0 return;
99 }
100 0 ▶ if($msg->media_type eq 'file'){
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->media_type eq 'file') { } 0 0 0
101 0 my $media_name = $msg->media_name;
102 0 ▶ my $media_dir = $self->media_dir // $self->tmpdir;
Condition0%0/3
$self->media_dir // $self->tmpdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->media_dir // $self->tmpdir
$self->media_dir $self->tmpdir
Truth table0%0/3
A // B
A= $self->media_dirB= $self->tmpdir
ABresult
1X1
011
000
103  
104 0 ▶ if($^O eq 'MSWin32'){
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
105 0 $media_name = Encode::encode("gbk",Encode::decode("utf8",$media_name));
106 0 $media_dir = Encode::encode("gbk",Encode::decode("utf8",$media_dir));
107 }
108 0 my $path = File::Spec->catfile($media_dir,$media_name);
109 0 my $i = 1;
110 0 ▶ while( -f $path){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 0 0 0
111 0 ▶ if($i>100){#防止死循环
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 100) 0 0 0
112 0 my $time = int(time);
113 0 $path =~ s/(\(\d\)|)(?=\.[^\.]+$|$)/($time)/;
114 0 last;
115 }
116 0 $path =~ s/(\(\d\)|)(?=\.[^\.]+$|$)/($i)/;
117 0 $i++;
118 }
119 0 eval{
120 0 ▶ open(my $fh,">",$path) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die $! 0 0 0
121 0 ▶ chmod 0644, $fh if $^O ne 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "MSWin32" 0 0 0
122 0 print $fh $data;
123 0 close $fh;
124 0 ▶ $msg->media_path($^O eq 'MSWin32'?Encode::encode("utf8",Encode::decode("gbk",$path)):$path);
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
125 0 ▶ $callback->($path,$data,$msg) if ref $callback eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback eq "CODE" 0 0 0
126 };
127 0 ▶ $self->error("[ ". __PACKAGE__ . " ] $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
128
129 }
130 else{
131 0 my $t = POSIX::strftime('%Y%m%d%H%M%S',localtime());
132 0 my @opt = (
133 TEMPLATE => "mojo_weixin_media_${t}_XXXX",
134 SUFFIX => "." . $msg->media_ext,
135 UNLINK => 0,
136 );
137 0 ▶ defined $self->media_dir?(push @opt,(DIR=>$self->media_dir)):(push @opt,(TMPDIR=>1));
Branch: 1 branch
BranchTrueFalseTotal
defined $self->media_dir ? : 0 0 0
138 0 eval{
139 0 my $tmp = File::Temp->new(@opt);
140 0 binmode $tmp;
141 0 ▶ chmod 0644, $tmp if $^O ne 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "MSWin32" 0 0 0
142 0 print $tmp $data;
143 0 close $tmp;
144 0 $msg->media_path($tmp->filename);
145 0 ▶ $callback->($tmp->filename,$data,$msg) if ref $callback eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback eq "CODE" 0 0 0
146 };
147 0 ▶ $self->error("[ ". __PACKAGE__ . " ] $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
148 }
149 0 });
150 }
151 1 1;
« Weixin/Plugin/KnowledgeBase.pm ↑ Summary Weixin/Plugin/HwPush.pm »