Controller/SimpleCAS.pm

filter: Statementstmt 31.6% 75 / 237 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 48.9% 23 / 47 Podpod 100% 14 / 14 totaltotal 26.1% 112 / 429 CC 55 SCAR 37.5
CC
55
Cov
21%
fetch_content
132.0
add
90.0
add_resize_image
90.0
CRAP 42.4
« Controller/SimpleCAS/Role/TextTranscode.pm ↑ Summary Controller/SimpleCAS/CSS/Simple.pm »
1 package Catalyst::Controller::SimpleCAS;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: General-purpose content-addressed storage (CAS) for Catalyst
6  
7 1 our $VERSION = '1.002';
8  
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶BEGIN { extends 'Catalyst::Controller' }
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 with 'Catalyst::Controller::SimpleCAS::Role::TextTranscode';
15  
16 1 ▶use Catalyst::Controller::SimpleCAS::Content;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Path::Class qw(file dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use String::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 0 ▶has store_class => ( is => 'ro', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 '+File'
30 1 });
31  
32 0 ▶has store_path => ( is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my $self = shift;
34 0 my $c = $self->_app;
35 # Default Cas Store path if none was supplied in the config:
36 0 return dir( Catalyst::Utils::home($c), 'cas_store' )->stringify;
37 1 });
38  
39 0 ▶has store_args => ( is => 'ro', isa => 'HashRef', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 my $self = shift;
41 return {
42 0 store_dir => $self->store_path,
43 };
44 1 });
45  
46 has Store => (
47 does => 'Catalyst::Controller::SimpleCAS::Store',
48 is => 'ro',
49 lazy => 1,
50 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my $self = shift;
52 0 my $class = $self->store_class;
53 0 ▶ if ($class =~ m/^\+([\w:]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^\+([\w:]+)/) 0 0 0
54 0 $class = 'Catalyst::Controller::SimpleCAS::Store::'.$1;
55 }
56 0 Module::Runtime::require_module($class);
57 return $class->new(
58 simplecas => $self,
59 0 %{$self->store_args},
60 );
61 },
62 1 handles => [qw(
63 file_checksum
64 calculate_checksum
65 )],
66 );
67  
68  
69 ### ----------------------------------------------------------------------
70 ### New sugar/convenience methods:
71 ###
72 0 ▶sub fetch {
Subroutine fetch: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($self, $cas_id) = @_;
74 0 $self->uri_find_Content($cas_id)
75 }
76  
77 0 ▶sub fetch_fh {
Subroutine fetch_fh: not called CC=2 SCAR=17.9
Pod documented
78 0 my ($self, $cas_id) = @_;
79 0 ▶ my $checksum = $self->_find_prune_checksum($cas_id) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->_find_prune_checksum($cas_id) or return undef 0 0 0
80 0 $self->Store->fetch_content_fh($checksum)
81 }
82  
83 0 ▶sub add {
Subroutine add: not called CC=9 SCAR=45.0
Pod documented
84 0 my $self = shift;
85 0 my $cnt = shift;
86
87 0 my $content = '';
88
89 0 ▶ if(my $type = ref $cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = ref $cnt) { } 0 0 0
90 0 ▶ if($type eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR') { } 0 0 0
elsif (builtin::blessed($cnt) and $cnt->can('getline')) { } 0 0 0
91 0 $content = $$cnt;
92 }
93 ▶ elsif(blessed $cnt && $cnt->can('getline')) {
Condition0%0/3
builtin::blessed($cnt) and $cnt->can("getline")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($cnt) and $cnt->can("getline")
builtin::blessed($cnt) $cnt->can("getline")
Truth table0%0/3
A and B
A= builtin::blessed($cnt)B= $cnt->can("getline")
ABresult
0X0
100
111
94 0 ▶ while(my $line = $cnt->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $cnt->getline) 0 0 0
95 0 $content .= $line;
96 }
97 }
98 else {
99 0 die "Bad content argument $cnt!";
100 }
101 }
102 else {
103 0 $content = $cnt;
104 }
105
106 # Is this a file name?
107 0 ▶ return $self->Store->add_content_file($content) if (
Branch: 1 branch
BranchTrueFalseTotal
if length $content < 1024 and not $content =~ /\n/ and -f $content 0 0 0
Condition0%0/6
length $content < 1024 and not $content =~ /\n/ and -f $content
!ll&&!rl&&r
000
length $content < 1024 and not $content =~ /\n/
!ll&&!rl&&r
000
MC/DC0%0/3
length $content < 1024 and not $content =~ /\n/ and -f $content
length $content < 1024 not $content =~ /\n/ -f $content
Truth table0%0/4
A and not(B) and C
A= length $content < 1024B= not $content =~ /\n/C= -f $content
ABCresult
0XX0
10X0
1100
1111
108 length($content) < 1024 &&
109 !($content =~ /\n/) &&
110 -f $content
111 );
112  
113 0 return $self->Store->add_content($content)
114 }
115 ###
116 ### ----------------------------------------------------------------------
117  
118  
119 #has 'fetch_url_path', is => 'ro', isa => 'Str', default => '/simplecas/fetch_content/';
120  
121 0 ▶sub Content {
Subroutine Content: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 my $checksum = shift;
124 0 my $filename = shift; #<-- optional
125 0 return Catalyst::Controller::SimpleCAS::Content->new(
126 Store => $self->Store,
127 checksum => $checksum,
128 filename => $filename
129 );
130 }
131  
132 # Accepts a free-form string and tries to extract a Cas checksum string from it. If the
133 # checksum exists, thr pruned checksum string is returned
134 0 ▶sub _find_prune_checksum {
Subroutine _find_prune_checksum: not called CC=5 SCAR=34.0
135 0 my $self = shift;
136 0 ▶ my $uri = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or return undef 0 0 0
137 0 my @parts = split(/\//,$uri);
138
139 0 ▶ while (scalar @parts > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parts > 0) 0 0 0
140 0 my $checksum = shift @parts;
141 0 ▶ next unless ($checksum =~ /^[0-9a-f]{40}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $checksum =~ /^[0-9a-f]{40}$/ 0 0 0
142 0 ▶ return $checksum if ($self->Store->content_exists($checksum));
Branch: 1 branch
BranchTrueFalseTotal
if $self->Store->content_exists($checksum) 0 0 0
143 }
144 0 return undef;
145 }
146  
147 # Accepts a free-form string and tries to extract a Cas checksum and
148 # filename from it. If it is successfully, and the checksum exists in
149 # the Store, returns the Content object
150 0 ▶sub uri_find_Content {
Subroutine uri_find_Content: not called CC=6 SCAR=37.4
Pod documented
151 0 my $self = shift;
152 0 ▶ my $uri = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or return undef 0 0 0
153 0 my @parts = split(/\//,$uri);
154
155 0 ▶ while (scalar @parts > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parts > 0) 0 0 0
156 0 my $checksum = shift @parts;
157 0 ▶ next unless ($checksum =~ /^[0-9a-f]{40}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $checksum =~ /^[0-9a-f]{40}$/ 0 0 0
158 0 ▶ my $filename = (scalar @parts == 1) ? $parts[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @parts == 1 ? : 0 0 0
159 0 ▶ my $Content = $self->Content($checksum,$filename) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $Content = $self->Content($checksum, $filename) or next 0 0 0
160 0 return $Content;
161 }
162 0 return undef;
163 }
164  
165 1 ▶sub base :Chained :PathPrefix :CaptureArgs(0) {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine base: not called CC=1 SCAR=0.0
Pod documented
166  
167 0 ▶sub fetch_content :Chained('base') :Args {
Subroutine fetch_content: not called CC=11 SCAR=48.8
Pod documented
168 0 my ($self, $c, $checksum, $filename) = @_;
169
170 0 my $disposition = 'inline;filename="' . $checksum . '"';
171
172 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
173 0 $filename =~ s/\"/\'/g;
174 0 $disposition = 'attachment; filename="' . $filename . '"';
175 }
176
177 0 ▶ unless($self->Store->content_exists($checksum)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->Store->content_exists($checksum)) 0 0 0
178 0 $c->res->body('Does not exist');
179 0 return;
180 }
181
182 0 ▶ my $type = $self->Store->content_mimetype($checksum) or die "Error reading mime type";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->Store->content_mimetype($checksum) or die "Error reading mime type" 0 0 0
183
184 # type overrides for places where File::MimeInfo::Magic is known to guess wrong
185 0 ▶ if($type eq 'application/vnd.ms-powerpoint' || $type eq 'application/zip') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "application/vnd.ms-powerpoint" or $type eq "application/zip") 0 0 0
Condition0%0/3
$type eq "application/vnd.ms-powerpoint" or $type eq "application/zip"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "application/vnd.ms-powerpoint" or $type eq "application/zip"
$type eq "application/vnd.ms-powerpoint" $type eq "application/zip"
Truth table0%0/3
A or B
A= $type eq "application/vnd.ms-powerpoint"B= $type eq "application/zip"
ABresult
1X1
011
000
186 0 my $Content = $self->Content($checksum,$filename);
187 0 my $ext = lc($Content->file_ext);
188 0 ▶ $type =
Branch: 5 branches
BranchTrueFalseTotal
$ext eq 'doc' ? : 0 0 0
$ext eq 'xls' ? : 0 0 0
$ext eq 'docx' ? : 0 0 0
$ext eq 'xlsx' ? : 0 0 0
$ext eq 'pptx' ? : 0 0 0
189 $ext eq 'doc' ? 'application/msword' :
190 $ext eq 'xls' ? 'application/vnd.ms-excel' :
191 $ext eq 'docx' ? 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' :
192 $ext eq 'xlsx' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' :
193 $ext eq 'pptx' ? 'application/vnd.openxmlformats-officedocument.presentationml.presentation' :
194 $type;
195 }
196
197 0 $c->response->header('Content-Type' => $type);
198 0 $c->response->header('Content-Disposition' => $disposition);
199 0 return $c->res->body( $self->Store->fetch_content_fh($checksum) );
200 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202  
203 0 ▶sub upload_content :Chained('base') :Args {
Subroutine upload_content: not called CC=3 SCAR=24.8
Pod documented
204 0 my ($self, $c) = @_;
205  
206 0 ▶ my $upload = $c->req->upload('Filedata') or die "no upload object";
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $c->req->upload("Filedata") or die "no upload object" 0 0 0
207 0 ▶ my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content";
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content" 0 0 0
208
209 0 return $c->res->body($checksum);
210 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212  
213 0 ▶sub upload_image :Chained('base') :Args {
Subroutine upload_image: not called CC=3 SCAR=24.8
Pod documented
214 0 my ($self, $c, $maxwidth, $maxheight) = @_;
215  
216 0 ▶ my $upload = $c->req->upload('Filedata') or die "no upload object";
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $c->req->upload("Filedata") or die "no upload object" 0 0 0
217  
218 0 my ($type,$subtype) = split(/\//,$upload->type);
219
220 0 my $resized = \0;
221 0 my $shrunk = \0;
222
223 0 my ($checksum,$width,$height,$orig_width,$orig_height);
224  
225 0 ▶ if($self->_is_image_resize_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_image_resize_available) { } 0 0 0
226 # When Image::Resize is available:
227 0 ($checksum,$width,$height,$resized,$orig_width,$orig_height)
228 = $self->add_resize_image($upload->tempname,$type,$subtype,$maxwidth,$maxheight);
229 }
230 else {
231 # Fall-back calculates new image size without actually resizing it. The img
232 # tag will still be smaller, but the image file will be original dimensions
233 0 ($checksum,$width,$height,$shrunk,$orig_width,$orig_height)
234 = $self->add_size_info_image($upload->tempname,$type,$subtype,$maxwidth,$maxheight);
235 }
236
237
238 0 unlink $upload->tempname;
239
240 #my $tag = '<img src="/simplecas/fetch_content/' . $checksum . '"';
241 #$tag .= ' width=' . $width . ' height=' . $height if ($width and $height);
242 #$tag .= '>';
243
244 # TODO: fix this API!!!
245
246 0 my $packet = {
247 success => \1,
248 checksum => $checksum,
249 height => $height,
250 width => $width,
251 resized => $resized,
252 shrunk => $shrunk,
253 orig_width => $orig_width,
254 orig_height => $orig_height,
255 filename => $self->safe_filename($upload->filename),
256 };
257
258 0 return $self->_json_response($c, $packet);
259 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
260  
261 0 ▶sub _is_image_resize_available {
Subroutine _is_image_resize_available: not called CC=1 SCAR=6.9
262 0 my $flag = 1;
263 0 ▶ try { Module::Runtime::require_module('Image::Resize') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ▶ catch { $flag = 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 $flag
266 }
267  
268  
269 0 ▶sub add_resize_image :Private {
Subroutine add_resize_image: not called CC=9 SCAR=45.0
Pod documented
270 0 my ($self,$file,$type,$subtype,$maxwidth,$maxheight) = @_;
271
272 0 ▶ my $checksum = $self->Store->add_content_file($file) or die "Failed to add content";
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content_file($file) or die "Failed to add content" 0 0 0
273
274 0 my $resized = \0;
275
276 0 my ($width,$height) = $self->Store->image_size($checksum);
277 0 my ($orig_width,$orig_height) = ($width,$height);
278 0 ▶ if (defined $maxwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxwidth) 0 0 0
279
280 0 my ($newwidth,$newheight) = ($width,$height);
281
282 0 ▶ if($width > $maxwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width > $maxwidth) 0 0 0
283 0 my $ratio = $maxwidth/$width;
284 0 $newheight = int($ratio * $height);
285 0 $newwidth = $maxwidth;
286 }
287
288 0 ▶ if(defined $maxheight and $newheight > $maxheight) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxheight and $newheight > $maxheight) 0 0 0
Condition0%0/3
defined $maxheight and $newheight > $maxheight
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxheight and $newheight > $maxheight
defined $maxheight $newheight > $maxheight
Truth table0%0/3
A and B
A= defined $maxheightB= $newheight > $maxheight
ABresult
0X0
100
111
289 0 my $ratio = $maxheight/$newheight;
290 0 $newwidth = int($ratio * $newwidth);
291 0 $newheight = $maxheight;
292 }
293
294 0 ▶ unless ($newwidth == $width && $newheight == $height) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newwidth == $width and $newheight == $height) 0 0 0
Condition0%0/3
$newwidth == $width and $newheight == $height
!ll&&!rl&&r
000
MC/DC0%0/2
$newwidth == $width and $newheight == $height
$newwidth == $width $newheight == $height
Truth table0%0/3
A and B
A= $newwidth == $widthB= $newheight == $height
ABresult
0X0
100
111
295
296 0 my $image = Image::Resize->new($self->Store->checksum_to_path($checksum));
297 0 my $gd = $image->resize($newwidth,$newheight);
298
299 0 my $method = 'png';
300 0 ▶ $method = $subtype if ($gd->can($subtype));
Branch: 1 branch
BranchTrueFalseTotal
if $gd->can($subtype) 0 0 0
301
302 0 my $tmpfile = file(
303 Catalyst::Utils::class2tempdir($self->_app,1),
304 String::Random->new->randregex('[a-z0-9A-Z]{15}')
305 );
306
307 0 $tmpfile->spew( $gd->$method );
308
309 0 my $newchecksum = $self->Store->add_content_file_mv($tmpfile->stringify);
310
311 0 ($checksum,$width,$height) = ($newchecksum,$newwidth,$newheight);
312 0 $resized = \1;
313 }
314 }
315
316 0 return ($checksum,$width,$height,$resized,$orig_width,$orig_height);
317 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
318  
319  
320 # New method, uses the same API as 'add_resize_image' above, but doesn't
321 # do any actual resizing (just calculates smaller height/width for better
322 # display). This method is used when Image::Resize is not available.
323 # Added for RapidApp Github Issue #42
324 0 ▶sub add_size_info_image :Private {
Subroutine add_size_info_image: not called CC=8 SCAR=42.8
Pod documented
325 0 my ($self,$file,$type,$subtype,$maxwidth,$maxheight) = @_;
326  
327 0 ▶ my $checksum = $self->Store->add_content_file($file) or die "Failed to add content";
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content_file($file) or die "Failed to add content" 0 0 0
328  
329 0 my $shrunk = \0;
330  
331 0 my ($width,$height) = $self->Store->image_size($checksum);
332  
333 0 my ($orig_width,$orig_height) = ($width,$height);
334 0 ▶ if (defined $maxwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxwidth) 0 0 0
335
336 0 my ($newwidth,$newheight) = ($width,$height);
337
338 0 ▶ if($width > $maxwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width > $maxwidth) 0 0 0
339 0 my $ratio = $maxwidth/$width;
340 0 $newheight = int($ratio * $height);
341 0 $newwidth = $maxwidth;
342 }
343
344 0 ▶ if(defined $maxheight and $newheight > $maxheight) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxheight and $newheight > $maxheight) 0 0 0
Condition0%0/3
defined $maxheight and $newheight > $maxheight
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxheight and $newheight > $maxheight
defined $maxheight $newheight > $maxheight
Truth table0%0/3
A and B
A= defined $maxheightB= $newheight > $maxheight
ABresult
0X0
100
111
345 0 my $ratio = $maxheight/$newheight;
346 0 $newwidth = int($ratio * $newwidth);
347 0 $newheight = $maxheight;
348 }
349
350 0 ▶ unless ($newwidth == $width && $newheight == $height) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newwidth == $width and $newheight == $height) 0 0 0
Condition0%0/3
$newwidth == $width and $newheight == $height
!ll&&!rl&&r
000
MC/DC0%0/2
$newwidth == $width and $newheight == $height
$newwidth == $width $newheight == $height
Truth table0%0/3
A and B
A= $newwidth == $widthB= $newheight == $height
ABresult
0X0
100
111
351 0 ($width,$height) = ($newwidth,$newheight);
352 0 $shrunk = \1;
353 }
354 }
355  
356 0 return ($checksum,$width,$height,$shrunk,$orig_width,$orig_height);
357 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
358  
359  
360 0 ▶sub upload_file :Chained('base') :Args {
Subroutine upload_file: not called CC=3 SCAR=24.8
Pod documented
361 0 my ($self, $c) = @_;
362
363 0 ▶ my $upload = $c->req->upload('Filedata') or die "no upload object";
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $c->req->upload("Filedata") or die "no upload object" 0 0 0
364 0 ▶ my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content";
Branch: 1 branch
BranchTrueFalseTotal
my $checksum = $self->Store->add_content_file_mv($upload->tempname) or die "Failed to add content" 0 0 0
365 0 my $Content = $self->Content($checksum,$upload->filename);
366
367 0 my $packet = {
368 success => \1,
369 filename => $self->safe_filename($upload->filename),
370 checksum => $Content->checksum,
371 mimetype => $Content->mimetype,
372 css_class => $Content->filelink_css_class,
373 };
374
375 0 return $self->_json_response($c, $packet);
376 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
377  
378  
379 0 ▶sub safe_filename {
Subroutine safe_filename: not called CC=1 SCAR=6.9
Pod documented
380 0 my $self = shift;
381 0 my $filename = shift;
382
383 0 my @parts = split(/[\\\/]/,$filename);
384 0 return pop @parts;
385 }
386  
387  
388 0 ▶sub upload_echo_base64 :Chained('base') :Args {
Subroutine upload_echo_base64: not called CC=2 SCAR=17.9
Pod documented
389 0 my ($self, $c) = @_;
390  
391 0 ▶ my $upload = $c->req->upload('Filedata') or die "no upload object";
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $c->req->upload("Filedata") or die "no upload object" 0 0 0
392
393 0 my $base64 = encode_base64($upload->slurp,'');
394
395 0 my $packet = {
396 success => \1,
397 echo_content => $base64
398 };
399
400 0 return $self->_json_response($c, $packet);
401 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
402  
403  
404 0 ▶has '_json_view_name', is => 'ro', isa => Maybe[Str], lazy => 1, default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
405 0 my $self = shift;
406 0 my $c = $self->_app;
407 0 my %views = map {$_=>1} $c->views;
408
409 # If we're in a RapidApp application (or the RapidApp::JSON view is available),
410 # use it. This is needed to do the special embedded iframe encoding when the
411 # RequestContentType => 'text/x-rapidapp-form-response' header is present. This
412 # is set from the RapidApp/ExtJS client when doing uploads for things like 'Insert Image'
413 0 my $vn = 'RapidApp::JSON';
414
415 0 ▶ $views{$vn} ? $vn : undef
Branch: 1 branch
BranchTrueFalseTotal
$views{$vn} ? : 0 0 0
416 1 };
417  
418  
419 0 ▶sub _json_response {
Subroutine _json_response: not called CC=3 SCAR=24.8
420 0 my ($self, $c, $packet) = @_;
421
422 0 $c->stash->{jsonData} = encode_json($packet);
423
424 0 ▶ if(my $vn = $self->_json_view_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vn = $self->_json_view_name) { } 0 0 0
425 0 ▶ my $view = $c->view( $vn ) or die "No such view name '$vn'";
Branch: 1 branch
BranchTrueFalseTotal
my $view = $c->view($vn) or die "No such view name '${vn}'" 0 0 0
426 0 $c->forward( $view );
427 }
428 else {
429 0 $c->res->content_type('application/json; charset=utf-8');
430 0 $c->res->body( $c->stash->{jsonData} );
431 }
432 }
433  
434 1 1;
435  
436  
437 __END__
« Controller/SimpleCAS/Role/TextTranscode.pm ↑ Summary Controller/SimpleCAS/CSS/Simple.pm »