OpenInteract2/File.pm

filter: Statementstmt 26.7% 19 / 71 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 5 totaltotal 19.0% 24 / 126 CC 18 SCAR 35.6
CC
18
Cov
18%
save_file
56.0
get_mime_type
56.0
create_filename
30.0
CRAP 35.3
« OpenInteract2/Observer/Controller/GetFiltersFromRequest.pm ↑ Summary OpenInteract2/URL.pm »
1 package OpenInteract2::File;
2  
3 # $Id: File.pm,v 1.12 2005/03/17 14:57:58 sjn Exp $
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 $OpenInteract2::File::VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/);
12  
13 # If $filename exists in the website, return the full path; otherwise
14 # undef
15  
16 0 ▶sub check_filename {
Subroutine check_filename: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my ( $class, $filename ) = @_;
18 0 my $full_filename = $class->create_filename( $filename );
19 0 ▶ return ( -f $full_filename ) ? $full_filename : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $full_filename ? : 0 0 0
20 }
21  
22  
23 # Save $filehandle to $filename under the website
24  
25 0 ▶sub save_file {
Subroutine save_file: not called CC=7 SCAR=40.3
Pod undocumented
26 0 my ( $class, $filehandle, $filename, $overwrite ) = @_;
27 0 my $full_filename = $class->create_filename( $filename );
28  
29 0 ▶ if ( -f $full_filename and ! $overwrite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $full_filename and not $overwrite) 0 0 0
Condition0%0/3
-f $full_filename and not $overwrite
!ll&&!rl&&r
000
MC/DC0%0/2
-f $full_filename and not $overwrite
-f $full_filename not $overwrite
Truth table0%0/3
A and not(B)
A= -f $full_filenameB= not $overwrite
ABresult
0X0
100
111
30 0 my $file_count = 0;
31 0 ▶ while ( -f $full_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $full_filename) 0 0 0
32 0 $file_count++;
33 0 $full_filename =~ s/(_x\d+)?(\.\w+)$/_x$file_count$2/;
34 }
35 }
36  
37 0 ▶ eval { open( OUT, '>', $full_filename ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", $full_filename or die $! 0 0 0
38 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
39 0 oi_error "Cannot save file [$full_filename]: $@";
40 }
41 0 binmode( OUT );
42 0 my ( $buf );
43 0 ▶ while ( read( $filehandle, $buf, 1024 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $filehandle, $buf, 1024) 0 0 0
44 0 print OUT $buf;
45 }
46 0 close( OUT );
47 0 return $full_filename;
48 }
49  
50  
51 # Creates a filename under the website
52  
53 0 ▶sub create_filename {
Subroutine create_filename: not called CC=5 SCAR=34.0
Pod undocumented
54 0 my ( $class, $filename ) = @_;
55 0 my $website_dir = CTX->lookup_directory( 'website' );
56  
57 0 my ( $vol, $dir, $file ) = File::Spec->splitpath( $filename );
58  
59 0 my @all_dirs = File::Spec->splitdir( $dir );
60 0 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
61  
62 # First see if the first directory is specified; if not, shift
63 # it off as this is a root directory request
64  
65 0 ▶ if ( $all_dirs[0] eq '' or $all_dirs[0] eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_dirs[0] eq "" or $all_dirs[0] eq ".") 0 0 0
Condition0%0/3
$all_dirs[0] eq "" or $all_dirs[0] eq "."
l!l&&r!l&&!r
000
MC/DC0%0/2
$all_dirs[0] eq "" or $all_dirs[0] eq "."
$all_dirs[0] eq "" $all_dirs[0] eq "."
Truth table0%0/3
A or B
A= $all_dirs[0] eq ""B= $all_dirs[0] eq "."
ABresult
1X1
011
000
66 0 shift @all_dirs;
67 }
68  
69 # Otherwise, ensure that the first directory specified is under
70 # the website_dir; if not, set the file to be saved under the
71 # upload dir
72  
73 0 my $test_dir = File::Spec->catdir( $website_dir, $all_dirs[0] );
74 0 ▶ if ( -d $test_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $test_dir) { } 0 0 0
75 0 $dir = File::Spec->catdir( $website_dir, @all_dirs );
76 }
77 else {
78 0 $dir = File::Spec->catdir( CTX->lookup_directory( 'upload' ), @all_dirs );
79 }
80 }
81 else {
82 0 $dir = CTX->lookup_directory( 'upload' );
83 }
84 0 return File::Spec->catpath( $vol, $dir, $file );
85 }
86  
87 ########################################
88 # MIME STUFF
89 ########################################
90  
91 # File::MMagic can be wrong sometimes, so we preempt it
92  
93 3 my %SIMPLE_TYPES = (
94 avi => 'video/x-msvideo',
95 bmp => 'image/bmp',
96 css => 'text/css',
97 gif => 'image/gif',
98 gz => 'application/gzip',
99 html => 'text/html',
100 htm => 'text/html',
101 jpg => 'image/jpeg',
102 jpeg => 'image/jpeg',
103 js => 'application/javascript',
104 midi => 'audio/midi',
105 mp3 => 'audio/mpeg',
106 mpeg => 'video/mpeg',
107 mpg => 'video/mpeg',
108 mov => 'video/quicktime',
109 pdf => 'application/pdf',
110 png => 'image/png',
111 ppt => 'application/vnd.ms-powerpoint',
112 rtf => 'text/rtf',
113 tif => 'image/tif',
114 tiff => 'image/tif',
115 txt => 'text/plain',
116 xls => 'application/vnd.ms-excel',
117 xml => 'text/xml',
118 wav => 'audio/x-wav',
119 zip => 'application/zip',
120 );
121  
122 3 my ( $MAGIC );
123 0 ▶sub init_magic {
Subroutine init_magic: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my ( $class ) = @_;
125 0 require File::MMagic;
126 0 $MAGIC = File::MMagic->new;
127 }
128  
129  
130 # Get the content type of content, a filehandle or a filename
131  
132 0 ▶sub get_mime_type {
Subroutine get_mime_type: not called CC=7 SCAR=40.3
Pod undocumented
133 0 my ( $class, $params ) = @_;
134 0 ▶ $class->init_magic unless ( $MAGIC );;
Branch: 1 branch
BranchTrueFalseTotal
unless $MAGIC 0 0 0
135 0 ▶ if ( $params->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"content"}) 0 0 0
136 0 return $MAGIC->checktype_contents( $params->{content} );
137 }
138 0 ▶ if ( $params->{filehandle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"filehandle"}) 0 0 0
139 0 return $MAGIC->checktype_filehandle( $params->{filehandle} );
140 }
141 0 my $filename = $params->{filename};
142 0 ▶ unless ( $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
143 0 oi_error "Pass 'content', 'filehandle' or 'filename' to get MIME type";
144 }
145 0 my ( $extension ) = $filename =~ /\.(\w+)$/;
146 0 ▶ if ( $SIMPLE_TYPES{ lc $extension } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SIMPLE_TYPES{lc $extension}) 0 0 0
147 0 return $SIMPLE_TYPES{ lc $extension };
148 }
149 0 my $full_filename = $class->check_filename( $filename );
150 0 ▶ unless ( $full_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($full_filename) 0 0 0
151 0 oi_error "File '$filename' does not exist.";
152 }
153 0 return $MAGIC->checktype_filename( $full_filename );
154 }
155  
156 3 1;
157  
158 __END__
« OpenInteract2/Observer/Controller/GetFiltersFromRequest.pm ↑ Summary OpenInteract2/URL.pm »