blib/lib/PEF/Front/File.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 128 CC 7 SCAR 19.8
CC
7
Cov
0%
append
20.0
new
12.0
DESTROY
6.0
CRAP 7.2
1 package PEF::Front::File;
2 use strict;
3 use warnings;
4 use File::Basename;
5 use PEF::Front::Config;
6  
7 my $uid_func = $INC{'Coro/AnyEvent.pm'}
8 ? sub {
9 no warnings 'once';
10 "$Coro::current" =~ /(0x[0-9a-f]+)/i;
11 $$ . '-' . hex $1;
12 }
13 : sub {
14 $$
15 };
16  
17 sub new {
18 my ($class, %args) = @_;
19 my $upload_path = cfg_upload_dir . '/' . $uid_func->();
20 if (!-d $upload_path) {
21 mkdir($upload_path, 0700)
22 or die {
23 result => 'INTERR',
24 answer => "Misconfigured upload directory: $!"
25 };
26 }
27 my $fname = $args{filename} || 'unknown_upload';
28 my ($name, $path, $suffix) = fileparse($fname, q|\.[^\.]*|);
29 if (-e "$upload_path/$name$suffix") {
30 my $i = 1;
31 ++$i while -e "$upload_path/$name.$i$suffix";
32 $fname = "$name.$i$suffix";
33 }
34 my $self = bless {
35 content_type => $args{content_type},
36 upload_path => $upload_path,
37 size => (delete $args{size}) || -1,
38 filename => $fname,
39 }, $class;
40 open(my $fh, "+>", "$self->{upload_path}/$self->{filename}")
41 or die {
42 result => 'INTERR',
43 answer => "Misconfigured upload directory: $!"
44 };
45 binmode $fh;
46 $self->{fh} = $fh;
47 $self;
48 }
49  
50 sub filename {$_[0]->{filename}}
51 sub size {$_[0]->{size}}
52 sub content_type {$_[0]->{content_type}}
53 sub upload_path {$_[0]->{upload_path}}
54  
55 sub append {
56 my $self = $_[0];
57 return if $_[1] eq '';
58 my $rc = syswrite($self->{fh}, $_[1]);
59 die {result => 'INTERR', answer => "Failed upload: $!"} if not defined $rc;
60 die {
61 result => 'INTERR',
62 answer => "Partial upload write: $rc != " . length $_[1]
63 }
64 if $rc != length $_[1];
65 }
66  
67 sub finish {
68 my $self = $_[0];
69 my $size = sysseek($self->{fh}, 0, 2);
70 $self->{size} = $size;
71 }
72  
73 sub value {
74 my $self = $_[0];
75 sysseek($self->{fh}, 0, 0);
76 my $ret = '';
77 sysread($self->{fh}, $ret, -s $self->{fh});
78 return $ret;
79 }
80  
81 sub fh {$_[0]->{fh}}
82  
83 sub DESTROY {
84 my $self = $_[0];
85 close($self->{fh});
86 $self->{fh} = undef;
87 unlink "$self->{upload_path}/$self->{filename}";
88 my $dir;
89 opendir $dir, $self->{upload_path} and do {
90 my @rest = grep {$_ ne '.' and $_ ne '..'} readdir $dir;
91 rmdir $self->{upload_path} if !@rest;
92 };
93 }
94  
95 1;
96  
97 __END__