Weixin/Plugin/ImageStore.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 47 CC 3 SCAR 19.5
CC
3
Cov
0%
call
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 7.0
1 package Mojo::Weixin::Plugin::ImageStore;
2 our $PRIORITY = 100;
3 use POSIX qw(strftime);
4 use File::Path qw(make_path);
5 use File::Basename qw(basename);
6 sub call{
7 my $client = shift;
8 my $data = shift;
9 my $callback = sub{
10 my($client,$path,$bin,$msg) = @_;
11 my $date = strftime("%Y-%m-%d",localtime($msg->time));
12 my $dir = ($data->{media_dir} || $client->media_dir);
13 $dir=~s/\/$//;
14 $dir .= "/" . $date;
15 eval{
16 make_path($dir);
17 my $filename = strftime("%Y%m%d%H%M%S",localtime($msg->time));
18 my $file_path = "$dir/$filename" . "." . $msg->media_ext;
19 while(-f $file_path){
20 if($file_path =~ /\.(\d+)\.[^\.]+$/){
21 my $n = $1+1;
22 $file_path =~ s/\.\d+(\.[^\.]+$)/.$n$1/;
23 }
24 else{
25 $file_path =~ s/(\.[^\.]+$)/.1$1/;
26 }
27 }
28 $client->spurt($bin,$file_path);
29 $msg->media_path($file_path);
30 $msg->media_name(basename($file_path));
31 $msg->content("[media](". $msg->media_path . ")");
32 };
33 $client->warn("保存图片失败:$@") if $@;
34 };
35  
36 $client->on(receive_media=>$callback,send_media=>$callback);
37 }
38 1;