File Coverage

blib/lib/Webqq/Client/Method/_get_offpic.pm
Criterion Covered Total %
statement 9 37 24.3
branch 0 24 0.0
condition n/a
subroutine 3 5 60.0
pod n/a
total 12 66 18.1


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         1  
  1         41  
2 1     1   4 use File::Temp qw/:seekable/;
  1         2  
  1         118  
3 1     1   4 use Webqq::Client::Util qw(console);
  1         2  
  1         597  
4             sub Webqq::Client::_get_offpic {
5 0     0     my $self = shift;
6 0 0         return if $self->{type} ne 'smartqq';
7 0           my $file_path = shift;
8 0           my $from_uin = shift;
9 0           my $cb = shift;
10 0           my $api = 'http://w.qq.com/d/channel/get_offpic2';
11 0 0         if(ref $cb eq 'CODE'){
12 0           my @query_string = (
13             file_path => $file_path,
14             f_uin => $from_uin,
15             clientid => $self->{qq_param}{clientid},
16             psessionid => $self->{qq_param}{psessionid},
17             );
18             my $callback = sub{
19 0     0     my $response = shift;
20 0 0         if($response->is_success){
21 0 0         return unless $response->header("content-type") =~/^image\/(.*)/;
22 0 0         my $type = $1=~/jpe?g/i ? ".jpg"
    0          
    0          
    0          
23             : $1=~/png/i ? ".png"
24             : $1=~/bmp/i ? ".bmp"
25             : $1=~/gif/i ? ".gif"
26             : undef
27             ;
28 0 0         return unless defined $type;
29 0           my $tmp = File::Temp->new(
30             TEMPLATE => "webqq_offpic_XXXX",
31             SUFFIX => $type,
32             TMPDIR => 1,
33             UNLINK => 1,
34             );
35 0           binmode $tmp;
36 0           print $tmp $response->content();
37 0           close $tmp;
38 0           eval{
39 0 0         open(my $fh,"<:raw",$tmp->filename) or die $!;
40 0           $cb->($fh,$tmp->filename);
41 0           close $fh;
42             };
43 0 0         console "[Webqq::Client::_get_offpic] $@" if $@;
44             }
45 0           };
46 0           require URI;
47 0           my $uri = URI->new('http:');
48 0           $uri->query_form(\@query_string);
49 0 0         print "GET $api?" . $uri->query() . "\n" if $self->{debug};
50 0           $self->{asyn_ua}->get($api ."?". $uri->query(),$callback);
51             }
52             };
53             1;