Zapp/Type/File.pm

filter: Statementstmt 34.8% 15 / 43 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 4 / 12 Podpod 100% 4 / 4 totaltotal 33.8% 23 / 68 CC 4 SCAR 14.2
CC
4
Cov
30%
_save_upload
12.0
_digest_dir
6.0
process_config
2.0
CRAP 4.2
« Zapp/Task/Action/Confirm.pm ↑ Summary Zapp/Formula.pm »
1 package Zapp::Type::File;
2 2 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mojo::Base 'Zapp::Type', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶has path => sub( $self ) { $self->app->home->child( 'public' ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
8 0 ▶has url => sub( $self ) { '/' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9  
10 # "die" for validation errors
11  
12 ▶sub _digest_dir( $self, $asset ) {
Subroutine _digest_dir: not called CC=2 SCAR=17.9
13 0 my $sha = Digest->new( 'SHA-1' );
14 0 ▶ if ( $asset->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asset->is_file) { } 0 0 0
15 0 ; say "File path: " . $asset->path;
16 0 $sha->addfile( $asset->path );
17 }
18 else {
19 0 $sha->add( $asset->slurp );
20 }
21 0 my $digest = $sha->b64digest =~ tr{+/}{-_}r;
22 0 my @parts = split /(.{2})/, $digest, 3;
23 0 my $dir = $self->path->child( @parts );
24 0 $dir->make_path;
25 0 return $dir;
26 }
27  
28 ▶sub _save_upload( $self, $c, $upload ) {
Subroutine _save_upload: not called CC=3 SCAR=24.8
29 0 ▶ return undef if !defined $upload->filename || $upload->filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $upload->filename or $upload->filename eq "" 0 0 0
Condition0%0/3
not defined $upload->filename or $upload->filename eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $upload->filename or $upload->filename eq ""
not defined $upload->filename $upload->filename eq ""
Truth table0%0/3
not(A) or B
A= not defined $upload->filenameB= $upload->filename eq ""
ABresult
1X1
011
000
30 0 my $dir = $self->_digest_dir( $upload->asset );
31 0 my $file = $dir->child( $upload->filename );
32 #; $c->log->debug( "Saving file: $file" );
33 0 $upload->move_to( $file );
34 0 return $file->to_rel( $self->path );
35 }
36  
37 # No default allowed for these values (for now)
38 ▶sub process_config( $self, $c, $form_value ) {
Subroutine process_config: not called CC=1 SCAR=6.9
Pod documented
39 0 return {};
40 }
41  
42 # Form value -> Type value
43 ▶sub process_input( $self, $c, $config_value, $form_value ) {
Subroutine process_input: not called CC=1 SCAR=6.9
Pod documented
44 0 return $self->_save_upload( $c, $form_value );
45 }
46  
47 # Type value -> Task value
48 ▶sub task_input( $self, $config_value, $input_value ) {
Subroutine task_input: not called CC=1 SCAR=6.9
Pod documented
49 0 ; say "Task input (file): $input_value";
50 0 return $self->path->child( $input_value )->to_abs;
51 }
52  
53 # Task value -> Type value
54 ▶sub task_output( $self, $config_value, $task_value ) {
Subroutine task_output: not called CC=1 SCAR=6.9
Pod documented
55 0 ; say "Task output (file): $task_value";
56 # Task gave us a path. Save the path and return the saved path.
57 0 my $path = Mojo::File->new( $task_value );
58 0 my $output_file = Mojo::Asset::File->new( path => "$path" );
59 0 my $dir = $self->_digest_dir( $output_file );
60 0 my $task_file = $dir->child( $path->basename );
61 0 $output_file->move_to( $task_file );
62 0 return $task_file->to_rel( $self->path );
63 }
64  
65 2 1;
66  
67 =pod
68  
69 =head1 NAME
70  
71 Zapp::Type::File
72  
73 =head1 VERSION
74  
75 version 0.004
76  
77 =head1 AUTHOR
78  
79 Doug Bell <preaction@cpan.org>
80  
81 =head1 COPYRIGHT AND LICENSE
82  
83 This software is copyright (c) 2021 by Doug Bell.
84  
85 This is free software; you can redistribute it and/or modify it under
86 the same terms as the Perl 5 programming language system itself.
87  
88 =cut
89  
90 __DATA__
« Zapp/Task/Action/Confirm.pm ↑ Summary Zapp/Formula.pm »