Controller/SimpleCAS/Store.pm

filter: Statementstmt 45.6% 42 / 92 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 12 / 21 Podpod 100% 9 / 9 totaltotal 42.9% 64 / 149 CC 13 SCAR 20.4
CC
13
Cov
37%
image_size
20.0
content_mimetype
20.0
fetch_content_fh
12.0
CRAP 7.7
« Controller/SimpleCAS/Store/DBIC.pm ↑ Summary Controller/SimpleCAS/Content.pm »
1 package Catalyst::Controller::SimpleCAS::Store;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Path::Class qw( file dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 requires qw(
17 content_exists
18 fetch_content
19 add_content
20 checksum_to_path
21 );
22  
23 0 ▶sub add_content_base64 {
Subroutine add_content_base64: not called CC=2 SCAR=17.9
Pod documented
24 0 my $self = shift;
25 0 ▶ my $data = decode_base64(shift) or die "Error decoding base64 data. $@";
Branch: 1 branch
BranchTrueFalseTotal
my $data = &decode_base64(shift()) or die "Error decoding base64 data. $@" 0 0 0
26 0 return $self->add_content($data);
27 }
28  
29 0 ▶sub add_content_file {
Subroutine add_content_file: not called CC=2 SCAR=17.9
Pod documented
30 0 my $self = shift;
31 0 my $file = shift;
32  
33 0 my $checksum = $self->file_checksum($file);
34  
35 0 ▶ return $checksum if ($self->content_exists($checksum));
Branch: 1 branch
BranchTrueFalseTotal
if $self->content_exists($checksum) 0 0 0
36  
37 0 return $self->add_content(scalar(io($file)->slurp));
38 }
39  
40 0 ▶sub add_content_file_mv {
Subroutine add_content_file_mv: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42 0 my $file = shift;
43  
44 0 my $result = $self->add_content_file($file);
45  
46 0 ▶ die "SimpleCAS: Failed to move file '$file' into storage" unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
47  
48 0 unlink $file;
49
50 0 return $result;
51 }
52  
53 0 ▶sub image_size {
Subroutine image_size: not called CC=4 SCAR=30.0
Pod documented
54 0 my $self = shift;
55 0 my $checksum = shift;
56
57 0 ▶ my $content_type = $self->content_mimetype($checksum) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $content_type = $self->content_mimetype($checksum) or return undef 0 0 0
58 0 my ($mime_type,$mime_subtype) = split(/\//,$content_type);
59 0 ▶ return undef unless ($mime_type eq 'image');
Branch: 1 branch
BranchTrueFalseTotal
unless $mime_type eq "image" 0 0 0
60
61 0 ▶ my ($width,$height) = imgsize($self->checksum_to_path($checksum)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = imgsize($self->checksum_to_path($checksum)) or return undef 0 0 0
62 0 return ($width,$height);
63 }
64  
65 0 ▶sub content_mimetype {
Subroutine content_mimetype: not called CC=4 SCAR=30.0
Pod documented
66 0 my $self = shift;
67 0 my $checksum = shift;
68
69 # See if this is an actual MIME file with a defined Content-Type:
70 0 ▶ my $MIME = try{
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my $fh = $self->fetch_content_fh($checksum);
72 # only read the begining of the file, enough to make it past the Content-Type header:
73 0 my $buf; $fh->read($buf,1024); $fh->close;
74
75 # This will frequently produce uninitialized value warnings from Email::Simple::Header,
76 # and I haven't been able to figure out how to stop it
77 0 return Email::MIME->new($buf);
78 0 };
79 0 ▶ if($MIME && $MIME->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MIME and $MIME->content_type) 0 0 0
Condition0%0/3
$MIME and $MIME->content_type
!ll&&!rl&&r
000
MC/DC0%0/2
$MIME and $MIME->content_type
$MIME $MIME->content_type
Truth table0%0/3
A and B
A= $MIMEB= $MIME->content_type
ABresult
0X0
100
111
80 0 my ($type) = split(/\s*\;\s*/,$MIME->content_type);
81 0 return $type;
82 }
83  
84 # Otherwise, guess the mimetype from the file on disk
85 0 my $file = $self->checksum_to_path($checksum);
86  
87 0 ▶ return undef unless ( -f $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
88 0 return mimetype($file);
89 }
90  
91 0 ▶sub content_size {
Subroutine content_size: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 my $checksum = shift;
94
95 0 my $file = $self->checksum_to_path($checksum);
96
97 0 return file($file)->stat->size;
98 }
99  
100 0 ▶sub fetch_content_fh {
Subroutine fetch_content_fh: not called CC=3 SCAR=24.8
Pod documented
101 0 my $self = shift;
102 0 my $checksum = shift;
103  
104 0 my $file = $self->checksum_to_path($checksum);
105 0 ▶ return undef unless ( -f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
106
107 0 my $fh = IO::File->new();
108 0 ▶ $fh->open($file, '<:raw') or die "Failed to open $file for reading.";
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file, "<:raw") or die "Failed to open $file for reading." 0 0 0
109
110 0 return $fh;
111 }
112  
113 # All stores should use SHA-1, in order to be able to swap Store modules later on.
114 1 ▶sub file_checksum {
Subroutine file_checksum: called CC=2 SCAR=7.0
Pod documented
115 1 my $self = shift;
116 1 my $file = shift;
117
118 1 my $FH = IO::File->new();
119 1 ▶ $FH->open($file, '<:raw') or die "$! : $file\n";
Branch: 1 branch
BranchTrueFalseTotal
$FH->open($file, "<:raw") or die "$! : $file\n" 1 0 1
120  
121 1 my $sha1 = Digest::SHA1->new->addfile($FH)->hexdigest;
122 1 $FH->close;
123 1 return $sha1;
124 }
125  
126 0 ▶sub calculate_checksum {
Subroutine calculate_checksum: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self = shift;
128 0 my $data = shift;
129
130 0 my $sha1 = Digest::SHA1->new->add($data)->hexdigest;
131 0 return $sha1;
132 }
133  
134 2 1;
135  
136 =head1 NAME
137  
138 Catalyst::Controller::SimpleCAS::Store - Base Role for all SimpleCAS Store
139  
140 =head1 SYNOPSIS
141  
142 =head1 DESCRIPTION
143  
144 Used as role for all storages of SimpleCAS.
145  
146 =head1 METHODS
147  
148 =head2 add_content
149  
150 =head2 add_content_base64
151  
152 =head2 add_content_file
153  
154 =head2 add_content_file_mv
155  
156 =head2 calculate_checksum
157  
158 =head2 checksum_to_path
159  
160 =head2 content_exists
161  
162 =head2 content_mimetype
163  
164 =head2 content_size
165  
166 =head2 fetch_content
167  
168 =head2 fetch_content_fh
169  
170 =head2 file_checksum
171  
172 =head2 image_size
173  
174 =head2 init_store_dir
175  
176 =head2 split_checksum
177  
178 =head1 SEE ALSO
179  
180 =over
181  
182 =item *
183  
184 L<Catalyst::Controller::SimpleCAS>
185  
186 =back
187  
188 =head1 AUTHOR
189  
190 Henry Van Styn <vanstyn@cpan.org>
191  
192 Torsten Raudssus <torsten@raudss.us>
193  
194 =head1 COPYRIGHT AND LICENSE
195  
196 This software is copyright (c) 2014 by IntelliTree Solutions llc.
197  
198 This is free software; you can redistribute it and/or modify it under
199 the same terms as the Perl 5 programming language system itself.
200  
201 =cut
« Controller/SimpleCAS/Store/DBIC.pm ↑ Summary Controller/SimpleCAS/Content.pm »