StaticShare/Controller.pm

filter: Statementstmt 38.8% 85 / 219 Branchbran 18.4% 28 / 152 Conditioncond 17.4% 23 / 132 MC/DCmcdc 5.6% 4 / 71 Subroutinesub 51.8% 14 / 27 Podpod 0.0% 0 / 8 totaltotal 25.2% 154 / 609 CC 124 SCAR 54.8
CC
124
Cov
27%
post
870.0
file
380.0
dir
60.1
CRAP 239.1
↑ Summary StaticShare.pm »
1 package Mojolicious::Plugin::StaticShare::Controller;
2 1 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::AcceptLanguage;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::Util qw ( decode encode url_unescape xml_escape);#
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::Piece;# module replaces the standard localtime and gmtime functions with implementations that return objects
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #~ use Mojo::Asset::File;
9  
10 1 ▶has plugin => sub { shift->stash('plugin') };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 1 ▶has public_uploads => sub { shift->plugin->public_uploads };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 ▶has is_admin => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
13 1 my $c = shift;
14 1 $c->plugin->is_admin($c);
15 1 };
16  
17 1 ▶sub get {
Subroutine get: called CC=11 SCAR=37.3
Pod undocumented
18 1 my ($c) = @_;
19
20 1 $c->_stash();
21
22 return $c->not_found
23 0 ▶ if !$c->is_admin && grep {/^\./} @{$c->stash('url_path')->parts};
Branch: 1 branch
BranchTrueFalseTotal
if not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;} 0 1 1
Condition33%1/3
not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;}
!ll&&!rl&&r
010
MC/DC0%0/2
not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;}
not $c->is_admin grep {/^\./u;} @{$c->stash("url_path")->parts;}
Truth table33%1/3
not(A) and B
A= not $c->is_adminB= grep {/^\./u;} @{$c->stash("url_path")->parts;}
ABresult
0X0
100
111
24
25 1 ▶ if ($c->plugin->access) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->plugin->access) 0 1 1
26 0 ▶ my $access = ref $c->plugin->access eq 'CODE' ? $c->plugin->access->($c) : $c->plugin->access;
Branch: 1 branch
BranchTrueFalseTotal
ref $c->plugin->access eq 'CODE' ? : 0 0 0
27
28 0 ▶ return $c->not_found
Branch: 1 branch
BranchTrueFalseTotal
unless $access 0 0 0
29 unless $access;
30
31 0 ▶ return $c->render(%$access)
Branch: 1 branch
BranchTrueFalseTotal
if ref $access eq "HASH" 0 0 0
32 if ref $access eq 'HASH';
33 }
34
35 1 ▶ if ($c->is_admin && $c->param('admin')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->is_admin and $c->param("admin")) 0 1 1
Condition33%1/3
$c->is_admin and $c->param("admin")
!ll&&!rl&&r
100
MC/DC0%0/2
$c->is_admin and $c->param("admin")
$c->is_admin $c->param("admin")
Truth table33%1/3
A and B
A= $c->is_adminB= $c->param("admin")
ABresult
0X0
100
111
36 # Temporary Redirect
37 0 $c->res->code(307);
38 0 return $c->redirect_to($c->req->url->to_abs->path);
39 }
40  
41 1 my $file_path = $c->stash('file_path');
42
43 1 ▶ return $c->dir($file_path)
Branch: 1 branch
BranchTrueFalseTotal
if -d $file_path 1 0 1
44 if -d $file_path;
45 0 ▶ return $c->file($file_path)
Branch: 1 branch
BranchTrueFalseTotal
if -f $file_path 0 0 0
46 if -f $file_path;
47  
48 0 $c->not_found;
49 }
50  
51 0 ▶sub post {
Subroutine post: not called CC=29 SCAR=67.7
Pod undocumented
52 0 my ($c) = @_;
53 0 $c->_stash();
54
55 0 my $file_path = $c->stash('file_path');
56 0 my $url_path = $c->stash('url_path');
57
58 0 ▶ $c->app->log->debug($file_path, $url_path->to_route, $c->dumper($c->req->params->to_hash))
Branch: 1 branch
BranchTrueFalseTotal
if $c->plugin->debug 0 0 0
59 if $c->plugin->debug;
60
61 0 ▶ if (my $dir = $c->param('dir')) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $dir = $c->param('dir')) { } 0 0 0
elsif (my $rename = $c->param('rename')) { } 0 0 0
elsif (my $delete = $c->param('delete[]') && $c->every_param('delete[]')) { } 0 0 0
elsif (defined(my $edit = $c->param('edit'))) { } 0 0 0
62 0 ▶ return $c->new_dir($file_path, $dir)
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_admin 0 0 0
63 if $c->is_admin;
64 0 return $c->render(json=>{error=>$c->i18n('you cant create dir')});
65 } elsif (my $rename = $c->param('rename')) {
66 0 ▶ return $c->rename($file_path, $rename)
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_admin 0 0 0
67 if $c->is_admin;
68 0 return $c->render(json=>{error=>$c->i18n('you cant rename')});
69 ▶ } elsif (my $delete = $c->param('delete[]') && $c->every_param('delete[]')) {
Condition0%0/3
$c->param('delete[]') && $c->every_param('delete[]')
!ll&&!rl&&r
000
MC/DC0%0/2
$c->param('delete[]') && $c->every_param('delete[]')
$c->param('delete[]') $c->every_param('delete[]')
Truth table0%0/3
A && B
A= $c->param('delete[]')B= $c->every_param('delete[]')
ABresult
0X0
100
111
70 0 ▶ return $c->delete($file_path, $delete)
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_admin 0 0 0
71 if $c->is_admin;
72 0 return $c->render(json=>{error=>$c->i18n('you cant delete')});
73 } elsif (defined (my $edit = $c->param('edit'))) {
74 0 ▶ return $c->_edit($file_path, $edit)
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_admin 0 0 0
75 if $c->is_admin;
76 0 return $c->render(json=>{error=>$c->i18n('you cant edit')});
77 }
78
79 return $c->render(json=>{error=>$c->i18n('target directory not found')})
80 0 ▶ if !$c->is_admin && grep {/^\./} @{$c->stash('url_path')->parts};
Branch: 1 branch
BranchTrueFalseTotal
if not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;} 0 0 0
Condition0%0/3
not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;}
!ll&&!rl&&r
000
MC/DC0%0/2
not $c->is_admin and grep {/^\./u;} @{$c->stash("url_path")->parts;}
not $c->is_admin grep {/^\./u;} @{$c->stash("url_path")->parts;}
Truth table0%0/3
not(A) and B
A= not $c->is_adminB= grep {/^\./u;} @{$c->stash("url_path")->parts;}
ABresult
0X0
100
111
81
82 0 ▶ return $c->render(json=>{error=>$c->i18n('you cant upload')})
Branch: 1 branch
BranchTrueFalseTotal
unless $c->is_admin or $c->public_uploads 0 0 0
Condition0%0/3
$c->is_admin or $c->public_uploads
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->is_admin or $c->public_uploads
$c->is_admin $c->public_uploads
Truth table0%0/3
A or B
A= $c->is_adminB= $c->public_uploads
ABresult
1X1
011
000
83 unless $c->is_admin || $c->public_uploads;
84
85 0 ▶ my $name = url_unescape($c->param('name') || '');#$file->filename
Condition0%0/2
$c->param('name') || ''
l!l
00
MC/DC0%0/1
$c->param('name') || ''
$c->param('name')
Truth table0%0/2
A
A= $c->param('name')
Aresult
11
00
86 0 utf8::upgrade($name);
87 #~ return $c->render(json=>{error=>$c->i18n('Provide the name of upload file')})
88 #~ unless $name =~ /\S/i;
89
90 0 ▶ return $c->render(json=>{error=>$c->i18n('Cant open target directory')})
Branch: 1 branch
BranchTrueFalseTotal
unless not $name or -w $file_path 0 0 0
Condition0%0/3
not $name or -w $file_path
l!l&&r!l&&!r
000
MC/DC0%0/2
not $name or -w $file_path
not $name -w $file_path
Truth table0%0/3
not(A) or B
A= not $nameB= -w $file_path
ABresult
1X1
011
000
91 unless !$name || -w $file_path;
92 #~ $c->req->max_message_size(0);
93 # Check file size
94 0 ▶ return $c->render(json=>{error=>$c->i18n('upload is too big')}, status=>417)
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->is_limit_exceeded 0 0 0
95 if $c->req->is_limit_exceeded;
96  
97 0 ▶ my $file = $c->req->upload('file')
Branch: 1 branch
BranchTrueFalseTotal
my $file = $c->req->upload("file") or return $c->render("json", {"error", $c->i18n("Where is your upload file?")}) 0 0 0
98 or return $c->render(json=>{error=>$c->i18n('Where is your upload file?')});
99  
100 0 ▶ my $to = $name ? $file_path->child($name) : $file_path;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
101
102 0 ▶ return $c->render(json=>{error=>$c->i18n('path is not a directory')})
Branch: 1 branch
BranchTrueFalseTotal
unless not $name or -d $file_path 0 0 0
Condition0%0/3
not $name or -d $file_path
l!l&&r!l&&!r
000
MC/DC0%0/2
not $name or -d $file_path
not $name -d $file_path
Truth table0%0/3
not(A) or B
A= not $nameB= -d $file_path
ABresult
1X1
011
000
103 unless !$name || -d $file_path;
104 0 ▶ return $c->render(json=>{error=>$c->i18n('path is a directory')})
Branch: 1 branch
BranchTrueFalseTotal
if not $name and -d $to 0 0 0
Condition0%0/3
not $name and -d $to
!ll&&!rl&&r
000
MC/DC0%0/2
not $name and -d $to
not $name -d $to
Truth table0%0/3
not(A) and B
A= not $nameB= -d $to
ABresult
0X0
100
111
105 if !$name && -d $to;
106
107 0 ▶ return $c->render(json=>{error=>$c->i18n('file already exists')})
Branch: 1 branch
BranchTrueFalseTotal
if not $c->param("replace") and -e $to 0 0 0
Condition0%0/3
not $c->param("replace") and -e $to
!ll&&!rl&&r
000
MC/DC0%0/2
not $c->param("replace") and -e $to
not $c->param("replace") -e $to
Truth table0%0/3
not(A) and B
A= not $c->param("replace")B= -e $to
ABresult
0X0
100
111
108 if !$c->param('replace') && -e $to;
109
110 0 ▶ eval { $file->asset->move_to($to) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $file->asset->move_to($to) } } or return $c->render("json", {"error", $@ =~ /(.+) at /u}) 0 0 0
111 or return $c->render(json=>{error=>$@ =~ /(.+) at /});
112
113
114 0 ▶ $url_path->merge($name)
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
115 if $name;
116
117 0 $c->render(json=>{ok=> $url_path->trailing_slash(0)->to_route});
118 }
119  
120 # TODO DELETE request
121 sub delete {
122 my ($c) = @_;
123 $c->_stash();
124
125 my $file_path = $c->stash('file_path');
126 my $url_path = $c->stash('url_path');
127
128 $c->app->log->debug($file_path, $url_path->to_route, $c->dumper($c->req->params->to_hash))
129 if $c->plugin->debug;
130
131 }
132  
133 1 ▶sub _stash {
Subroutine _stash: called CC=3 SCAR=11.1
134 1 my ($c) = @_;
135  
136 1 ▶ my $lang = HTTP::AcceptLanguage->new($c->req->headers->accept_language || 'en;q=0.5');
Condition50%1/2
$c->req->headers->accept_language || 'en;q=0.5'
l!l
01
MC/DC0%0/1
$c->req->headers->accept_language || 'en;q=0.5'
$c->req->headers->accept_language
Truth table50%1/2
A
A= $c->req->headers->accept_language
Aresult
11
00
137 1 $c->stash('language' => $lang);
138
139 1 my $pth = Mojo::Path->new($c->stash('pth'))->leading_slash(0)->trailing_slash(0);
140 1 ▶ $pth = $pth->trailing_slash(1)->merge('.'.$c->stash('format'))
Branch: 1 branch
BranchTrueFalseTotal
if $c->stash("format") 0 1 1
141 if $c->stash('format');
142 1 $c->stash('pth' => $pth);
143 1 my $url_path = $c->plugin->root_url->clone->merge($pth)->trailing_slash(1);
144 1 $c->stash('url_path' => $url_path);
145 #~ $c->stash('file_path' => $c->plugin->root_dir->clone->merge($c->stash('pth')));
146 1 $c->stash('file_path' => path(decode('UTF-8', url_unescape($c->plugin->root_dir->clone->merge($pth)))));
147 1 $c->stash('title' => $c->i18n('Share')." ".$url_path->to_route);
148 }
149  
150  
151 1 ▶sub dir {
Subroutine dir: called CC=28 SCAR=41.0
Pod undocumented
152 1 my ($c, $path) = @_;
153
154 1 ▶ return $c->not_found
Branch: 1 branch
BranchTrueFalseTotal
if ($c->plugin->render_dir // "") eq 0 0 1 1
Condition50%1/2
$c->plugin->render_dir // ""
l!l
01
MC/DC0%0/1
$c->plugin->render_dir // ""
$c->plugin->render_dir
Truth table50%1/2
A
A= $c->plugin->render_dir
Aresult
11
00
155 if ($c->plugin->render_dir // '') eq 0;
156
157 1 my $ex = Mojo::Exception->new($c->i18n(qq{Cant open directory}));
158 1 ▶ opendir(my $dir, $path)
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $path or return $c->render_maybe("Mojolicious-Plugin-StaticShare/exception", "format", "html", "handler", "ep", "status", 500, "exception", $ex) || $c->reply->exception($ex) 1 0 1
Condition0%0/3
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) $c->reply->exception($ex)
Truth table0%0/3
A || B
A= $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex)B= $c->reply->exception($ex)
ABresult
1X1
011
000
159 or return $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', format=>'html', handler=>'ep', status=>500, exception=>$ex)
160 || $c->reply->exception($ex);
161
162 1 my $files = $c->stash('files' => [])->stash('files');
163 1 my $dirs = $c->stash('dirs' => [])->stash('dirs');
164
165 1 ▶ while (readdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readdir $dir)) 16 1 17
166 next
167 16 ▶ if $_ eq '.' || $_ eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "." or $_ eq ".." 2 14 16
Condition100%3/3
$_ eq "." or $_ eq ".."
l!l&&r!l&&!r
1114
MC/DC100%2/2
$_ eq "." or $_ eq ".."
$_ eq "." $_ eq ".."
Truth table100%3/3
A or B
A= $_ eq "."B= $_ eq ".."
ABresult
1X1
011
000
168 next
169 14 ▶ if !$c->is_admin && /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if not $c->is_admin and /^\./u 0 14 14
Condition33%1/3
not $c->is_admin and /^\./u
!ll&&!rl&&r
0140
MC/DC0%0/2
not $c->is_admin and /^\./u
not $c->is_admin /^\./u
Truth table33%1/3
not(A) and B
A= not $c->is_adminB= /^\./u
ABresult
0X0
100
111
170
171 14 my $child = $path->child(decode('UTF-8', $_));
172
173 14 ▶ push @$dirs, decode('UTF-8', $_)
Branch: 1 branch
BranchTrueFalseTotal
if -d $child and -r _ 4 10 14
Condition71%5/7
-d $child && -r _ && (push(@$dirs, decode('UTF-8', $_)) && (next))
!ll
104
-d $child and -r _
!ll&&!rl&&r
1004
push @$dirs, decode("UTF-8", $_) and next
!ll
04
MC/DC0%0/3
-d $child && -r _ && (push(@$dirs, decode('UTF-8', $_)) && (next))
-d $child -r _ push @$dirs, decode("UTF-8", $_)
Truth table25%1/4
A and B && C
A= -d $childB= -r _C= push @$dirs, decode("UTF-8", $_)
ABCresult
0XX0
10X0
1100
1111
174 and next
175 if -d $child && -r _;
176
177 next
178 10 ▶ unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 10 10
179
180 10 my @stat = stat $child;
181
182 10 ▶ push @$files, {
Condition100%2/2
$stat[7] || 0
l!l
91
MC/DC100%1/1
$stat[7] || 0
$stat[7]
Truth table100%2/2
A
A= $stat[7]
Aresult
11
00
183 name => decode('UTF-8', $_),
184 size => $stat[7] || 0,
185 #~ type => $c->plugin->mime->type( (/\.([0-9a-zA-Z]+)$/)[0] || 'txt' ) || 'application/octet-stream',
186 mtime => decode('UTF-8', localtime( $stat[9] )->strftime), #->to_datetime, #to_string(),
187 #~ mode=> $stat[2] & 07777, #-r _,
188 };
189 }
190 1 closedir $dir;
191
192 1 ▶ for my $index ($c->plugin->dir_index ? @{$c->plugin->dir_index} : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$c->plugin->dir_index ? : 1 0 1
193 3 my $file = $path->child($index);
194 next
195 3 ▶ unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 2 1 3
196
197 1 ▶ $c->_stash_markdown($file)
Branch: 1 branch
BranchTrueFalseTotal
if $index =~ $c->plugin->re_markdown 0 1 1
Condition14%1/7
$index =~ $c->plugin->re_markdown && ($c->_stash_markdown($file) && $c->stash('index', $index) && (last))
!ll
10
$c->_stash_markdown($file) and $c->stash("index", $index) and last
!ll
00
$c->_stash_markdown($file) and $c->stash("index", $index)
!ll&&!rl&&r
000
MC/DC0%0/3
$index =~ $c->plugin->re_markdown && ($c->_stash_markdown($file) && $c->stash('index', $index) && (last))
$index =~ $c->plugin->re_markdown $c->_stash_markdown($file) $c->stash("index", $index)
Truth table0%0/4
A && B and C
A= $index =~ $c->plugin->re_markdownB= $c->_stash_markdown($file)C= $c->stash("index", $index)
ABCresult
0XX0
10X0
1100
1111
198 and $c->stash(index=>$index)
199 and last
200 if $index =~ $c->plugin->re_markdown;
201
202 1 ▶ $c->_stash_pod($file)
Branch: 1 branch
BranchTrueFalseTotal
if $index =~ $c->plugin->re_pod 1 0 1
Condition42%3/7
$index =~ $c->plugin->re_pod && ($c->_stash_pod($file) && $c->stash('index', $index) && (last))
!ll
01
$c->_stash_pod($file) and $c->stash("index", $index) and last
!ll
01
$c->_stash_pod($file) and $c->stash("index", $index)
!ll&&!rl&&r
001
MC/DC0%0/3
$index =~ $c->plugin->re_pod && ($c->_stash_pod($file) && $c->stash('index', $index) && (last))
$index =~ $c->plugin->re_pod $c->_stash_pod($file) $c->stash("index", $index)
Truth table0%0/4
A && B and C
A= $index =~ $c->plugin->re_podB= $c->_stash_pod($file)C= $c->stash("index", $index)
ABCresult
0XX0
10X0
1100
1111
203 and $c->stash(index=>$index)
204 and last
205 if $index =~ $c->plugin->re_pod;
206  
207 }
208
209 0 ▶ return $c->render(ref $c->plugin->render_dir ? %{$c->plugin->render_dir} : $c->plugin->render_dir,)
Branch: 2 branches
BranchTrueFalseTotal
if $c->plugin->render_dir 0 1 1
ref $c->plugin->render_dir ? : 0 0 0
210 if $c->plugin->render_dir;
211
212 $c->render_maybe("Mojolicious-Plugin-StaticShare/$_/dir", format=>'html', handler=>'ep',)
213 and return
214 1 ▶ for $c->stash('language')->languages;
Branch: 1 branch
BranchTrueFalseTotal
$c->render_maybe("Mojolicious-Plugin-StaticShare/$_/dir", "format", "html", "handler", "ep") and return 0 1 1
215
216 1 return $c->render('Mojolicious-Plugin-StaticShare/dir', format=>'html', handler=>'ep',);
217  
218
219 #~ $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', format=>'html', handler=>'ep', status=>500,exception=>Mojo::Exception->new(qq{Template rendering for dir content not found}))
220 #~ or $c->reply->exception();
221 }
222  
223 0 ▶sub new_dir {
Subroutine new_dir: not called CC=3 SCAR=24.8
Pod undocumented
224 0 my ($c, $path, $dir) = @_;
225
226 0 my $edir = url_unescape($dir);
227 0 utf8::upgrade($edir);
228
229 0 ▶ return $c->render(json=>{error=>$c->i18n('provide the name of new directory')})
Branch: 1 branch
BranchTrueFalseTotal
unless $edir =~ /\S/u 0 0 0
230 unless $edir =~ /\S/;
231
232 0 my $to = $path->child($edir);#
233
234 0 ▶ return $c->render(json=>{error=>$c->i18n('dir or file exists')})
Branch: 1 branch
BranchTrueFalseTotal
if -e $to 0 0 0
235 if -e $to;
236
237 0 $to->make_path;
238
239 0 $c->render(json=>{ok=> $c->stash('url_path')->clone->merge($dir)->trailing_slash(1)->to_route});
240
241 }
242  
243 0 ▶sub rename {
Subroutine rename: not called CC=4 SCAR=30.0
Pod undocumented
244 0 my ($c, $path, $rename) = @_;
245
246 0 my $ename = url_unescape($rename);
247 0 utf8::upgrade($ename);
248
249 0 ▶ return $c->render(json=>{error=>$c->i18n('provide new name')})
Branch: 1 branch
BranchTrueFalseTotal
unless $ename =~ /\S/u 0 0 0
250 unless $ename =~ /\S/;
251
252 0 my $to = $path->sibling($ename);
253
254 0 ▶ return $c->render(json=>{error=>$c->i18n('dir or file exists')})
Branch: 1 branch
BranchTrueFalseTotal
if -e $to 0 0 0
255 if -e $to;
256
257 0 ▶ my $move = eval { $path->move_to($to) }
Branch: 1 branch
BranchTrueFalseTotal
my $move = eval { do { $path->move_to($to) } } or return $c->render("json", {"error", $@ =~ /(.+) at /u}) 0 0 0
258 or return $c->render(json=>{error=>$@ =~ /(.+) at /});
259
260 0 $c->render(json=>{ok=> $c->stash('url_path')->trailing_slash(0)->to_dir->merge($rename)->to_route});
261
262 }
263  
264 0 ▶sub delete {
Subroutine delete: not called CC=7 SCAR=40.3
Pod undocumented
265 0 my ($c, $path, $delete) = @_;
266 0 my @delete = ();
267 0 for (@$delete) {
268 0 my $del = url_unescape($_);
269 0 utf8::upgrade($del);
270 0 ▶ push @delete, $c->i18n('provide the name of deleted dir')
Branch: 1 branch
BranchTrueFalseTotal
unless $del =~ /\S/u 0 0 0
Condition0%0/5
$del =~ /\S/u || push(@delete, $c->i18n('provide the name of deleted dir')) && (next)
l!l&&r!l&&!r
000
push @delete, $c->i18n("provide the name of deleted dir") and next
!ll
00
MC/DC0%0/2
$del =~ /\S/u || push(@delete, $c->i18n('provide the name of deleted dir')) && (next)
$del =~ /\S/u push @delete, $c->i18n("provide the name of deleted dir")
Truth table0%0/3
A || B
A= $del =~ /\S/uB= push @delete, $c->i18n("provide the name of deleted dir")
ABresult
1X1
011
000
271 and next
272 unless $del =~ /\S/;
273 0 my $d = $path->sibling($del);
274 0 ▶ push @delete, $c->i18n('dir or file does not exists')
Branch: 1 branch
BranchTrueFalseTotal
unless -e $d 0 0 0
Condition0%0/5
-e $d || push(@delete, $c->i18n('dir or file does not exists')) && (next)
l!l&&r!l&&!r
000
push @delete, $c->i18n("dir or file does not exists") and next
!ll
00
MC/DC0%0/2
-e $d || push(@delete, $c->i18n('dir or file does not exists')) && (next)
-e $d push @delete, $c->i18n("dir or file does not exists")
Truth table0%0/3
A || B
A= -e $dB= push @delete, $c->i18n("dir or file does not exists")
ABresult
1X1
011
000
275 and next
276 unless -e $d;
277 0 ▶ push @delete, eval {$d->remove_tree()} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $d->remove_tree } } ? : 0 0 0
278 }
279
280 0 $c->render(json=>{ok=>\@delete});
281 }
282  
283 0 ▶sub file {
Subroutine file: not called CC=19 SCAR=59.4
Pod undocumented
284 0 my ($c, $path) = @_;
285
286 0 my $ex = Mojo::Exception->new($c->i18n(qq{Permission denied}));
287 0 ▶ return $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', format=>'html', handler=>'ep', status=>500,exception=>$ex)
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 0 0
Condition0%0/3
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) $c->reply->exception($ex)
Truth table0%0/3
A || B
A= $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex)B= $c->reply->exception($ex)
ABresult
1X1
011
000
288 || $c->reply->exception($ex)
289 unless -r $path;
290
291 0 ▶ return $c->_edit($path)
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_admin and $c->param("edit") 0 0 0
Condition0%0/3
$c->is_admin and $c->param("edit")
!ll&&!rl&&r
000
MC/DC0%0/2
$c->is_admin and $c->param("edit")
$c->is_admin $c->param("edit")
Truth table0%0/3
A and B
A= $c->is_adminB= $c->param("edit")
ABresult
0X0
100
111
292 if $c->is_admin && $c->param('edit');
293
294 0 my $filename = $path->basename;
295
296 0 ▶ return $c->_markdown($path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->plugin->render_markdown || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_markdown 0 0 0
Condition0%0/8
($c->plugin->render_markdown || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_markdown
l!l&&r!l&&!r
000
($c->plugin->render_markdown || "") eq 0 or $c->param("attachment")
l!l&&r!l&&!r
000
$c->plugin->render_markdown || ''
l!l
00
MC/DC0%0/3
($c->plugin->render_markdown || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_markdown
$c->plugin->render_markdown $c->param("attachment") not $filename =~ $c->plugin->re_markdown
Truth table0%0/4
A or B or not(C)
A= $c->plugin->render_markdownB= $c->param("attachment")C= not $filename =~ $c->plugin->re_markdown
ABCresult
1XX1
01X1
0011
0000
297 unless ($c->plugin->render_markdown || '') eq 0 || $c->param('attachment') || $filename !~ $c->plugin->re_markdown;
298
299 0 ▶ return $c->_pod($path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->plugin->render_pod || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_pod 0 0 0
Condition0%0/8
($c->plugin->render_pod || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_pod
l!l&&r!l&&!r
000
($c->plugin->render_pod || "") eq 0 or $c->param("attachment")
l!l&&r!l&&!r
000
$c->plugin->render_pod || ''
l!l
00
MC/DC0%0/3
($c->plugin->render_pod || "") eq 0 or $c->param("attachment") or not $filename =~ $c->plugin->re_pod
$c->plugin->render_pod $c->param("attachment") not $filename =~ $c->plugin->re_pod
Truth table0%0/4
A or B or not(C)
A= $c->plugin->render_podB= $c->param("attachment")C= not $filename =~ $c->plugin->re_pod
ABCresult
1XX1
01X1
0011
0000
300 unless ($c->plugin->render_pod || '') eq 0 || $c->param('attachment') || $filename !~ $c->plugin->re_pod;
301
302 0 ▶ my $asset = $c->_html($path)
Branch: 1 branch
BranchTrueFalseTotal
unless $c->param("attachment") or not $filename =~ $c->plugin->re_html 0 0 0
Condition0%0/3
$c->param("attachment") or not $filename =~ $c->plugin->re_html
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->param("attachment") or not $filename =~ $c->plugin->re_html
$c->param("attachment") not $filename =~ $c->plugin->re_html
Truth table0%0/3
A or not(B)
A= $c->param("attachment")B= not $filename =~ $c->plugin->re_html
ABresult
1X1
011
000
303 unless $c->param('attachment') || $filename !~ $c->plugin->re_html;
304
305 0 ▶ $c->res->headers->content_disposition($c->param('attachment') ? "attachment; filename=$filename;" : "inline");
Branch: 1 branch
BranchTrueFalseTotal
$c->param('attachment') ? : 0 0 0
306 0 ▶ my $type =$c->plugin->mime->type( ( $path =~ /\.([0-9a-zA-Z]+)$/)[0] || 'txt' ) || $c->plugin->mime->type('txt');#'application/octet-stream';
Condition0%0/5
$c->plugin->mime->type(($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt') || $c->plugin->mime->type('txt')
l!l&&r!l&&!r
000
($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt'
l!l
00
MC/DC0%0/2
$c->plugin->mime->type(($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt') || $c->plugin->mime->type('txt')
$c->plugin->mime->type(($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt') $c->plugin->mime->type('txt')
MC/DC0%0/1
($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt'
($path =~ /\.([0-9a-zA-Z]+)$/u)[0]
Truth table0%0/3
A || B
A= $c->plugin->mime->type(($path =~ /\.([0-9a-zA-Z]+)$/u)[0] || 'txt')B= $c->plugin->mime->type('txt')
ABresult
1X1
011
000
Truth table0%0/2
A
A= ($path =~ /\.([0-9a-zA-Z]+)$/u)[0]
Aresult
11
00
307 0 $c->res->headers->content_type($type);
308 0 ▶ $c->reply->asset($asset || Mojo::Asset::File->new(path => $path));
Condition0%0/3
$asset || 'Mojo::Asset::File'->new('path', $path)
l!l&&r!l&&!r
000
MC/DC0%0/2
$asset || 'Mojo::Asset::File'->new('path', $path)
$asset 'Mojo::Asset::File'->new('path', $path)
Truth table0%0/3
A || B
A= $assetB= 'Mojo::Asset::File'->new('path', $path)
ABresult
1X1
011
000
309 }
310  
311 0 ▶sub _html {# disable scripts inside html
Subroutine _html: not called CC=2 SCAR=17.9
312 0 my ($c, $path) = @_;
313 0 my $file = Mojo::Asset::File->new(path => $path);
314 0 my $content = $file->slurp;
315 0 my $dom = Mojo::DOM->new($content);
316 0 ▶ $dom->find('script')->each(\&_sanitize_script)->size
Branch: 1 branch
BranchTrueFalseTotal
$dom->find("script")->each(\&Mojolicious::Plugin::StaticShare::Controller::_sanitize_script)->size or return $file 0 0 0
317 or return $file;
318
319 0 my $asset = Mojo::Asset::Memory->new;
320 0 $asset->add_chunk($dom)->mtime($file->mtime);
321 0 return $asset;
322 }
323  
324 0 ▶sub _markdown {# file
Subroutine _markdown: not called CC=5 SCAR=34.0
325 0 my ($c, $path) = @_;
326  
327 0 my $ex = Mojo::Exception->new($c->i18n(qq{Please install or verify markdown module (default to Text::Markdown::Hoedown) with markdown(\$str) sub or parse(\$str) method}));
328  
329 0 ▶ $c->_stash_markdown($path)
Branch: 1 branch
BranchTrueFalseTotal
$c->_stash_markdown($path) or return $c->render_maybe("Mojolicious-Plugin-StaticShare/exception", "format", "html", "handler", "ep", "status", 500, "exception", $ex) || $c->reply->exception($ex) 0 0 0
Condition0%0/3
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) || $c->reply->exception($ex)
$c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex) $c->reply->exception($ex)
Truth table0%0/3
A || B
A= $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', 'format', 'html', 'handler', 'ep', 'status', 500, 'exception', $ex)B= $c->reply->exception($ex)
ABresult
1X1
011
000
330 or return $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', format=>'html', handler=>'ep', status=>500,exception=>$ex)
331 || $c->reply->exception($ex);
332
333 return $c->plugin->render_markdown
334 0 ▶ ? $c->render(ref $c->plugin->render_markdown ? %{$c->plugin->render_markdown} : $c->plugin->render_markdown,)
Branch: 2 branches
BranchTrueFalseTotal
$c->plugin->render_markdown ? : 0 0 0
ref $c->plugin->render_markdown ? : 0 0 0
335 : $c->render('Mojolicious-Plugin-StaticShare/markdown', format=>'html', handler=>'ep',);
336 }
337  
338 1 my $layout_re = qr|^(?:\s*%+\s*layouts/(.+)[;\s]+)|;
339  
340 0 ▶sub _stash_markdown {
Subroutine _stash_markdown: not called CC=3 SCAR=24.8
341 0 my ($c, $path) = @_;
342 0 ▶ my $md = $c->plugin->markdown
Branch: 1 branch
BranchTrueFalseTotal
my $md = $c->plugin->markdown or return 0 0 0
343 or return; # not installed
344 0 my $content = decode('UTF-8', $path->slurp);
345 0 $content =~ s|$layout_re|$c->_layout_index($1)|e;#) {# || $content =~ s/(?:%\s*layout\s+"([^"]+)";?)//m
346 0 my $dom = Mojo::DOM->new($md->parse($content));
347 0 $dom->find('script')->each(\&_sanitize_script);
348 0 $dom->find('*')->each(\&_dom_attrs);
349 0 ▶ $c->stash(markdown => $dom->at('body') || $dom);
Condition0%0/3
$dom->at('body') || $dom
l!l&&r!l&&!r
000
MC/DC0%0/2
$dom->at('body') || $dom
$dom->at('body') $dom
Truth table0%0/3
A || B
A= $dom->at('body')B= $dom
ABresult
1X1
011
000
350 #~ my $filename = $path->basename;
351 #~ $c->stash('title'=>$filename);
352 }
353  
354 1 my $layout_ext_re = qr|\.([^/\.;\s]+)?\.?([^/\.;\s]+)?$|; # .html.ep
355  
356 0 ▶sub _layout_index {# from regexp execute
Subroutine _layout_index: not called CC=6 SCAR=37.4
357 0 my ($c, @match) = @_;#
358 0 $match[0] =~ s|[;\s]+$||;
359 #~ utf8::encode($match[0]);
360 0 ▶ push @match, $1, $2
Branch: 1 branch
BranchTrueFalseTotal
if $match[0] =~ s/$layout_ext_re//u 0 0 0
361 if $match[0] =~ s|$layout_ext_re||;
362 0 ▶ my $found = $c->app->renderer->template_path({
Condition0%0/4
$match[1] || 'html'
l!l
00
$match[2] || 'ep'
l!l
00
MC/DC0%0/1
$match[1] || 'html'
$match[1]
MC/DC0%0/1
$match[2] || 'ep'
$match[2]
Truth table0%0/2
A
A= $match[1]
Aresult
11
00
Truth table0%0/2
A
A= $match[2]
Aresult
11
00
363 template => "layouts/$match[0]",
364 format => $match[1] || 'html',
365 handler => $match[2] || 'ep',
366 });
367 0 ▶ $c->layout($match[0])#encode('UTF-8', $match[0]))
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
Condition0%0/5
$found && ($c->layout($match[0]) && (return ''))
!ll&&!rl&&r
000
$c->layout($match[0]) and return ""
!ll
00
MC/DC0%0/2
$found && ($c->layout($match[0]) && (return ''))
$found $c->layout($match[0])
Truth table0%0/3
A && B
A= $foundB= $c->layout($match[0])
ABresult
0X0
100
111
368 and return ''
369 if $found;
370 0 my $err = "layout [$match[0]].$match[1].$match[2] not found";
371 0 $c->app->log->error("$err", "\t app->renderer->paths: ", @{$c->app->renderer->paths});
372 0 return "<div style='color:red;'>$err</div>";
373 }
374  
375 0 ▶sub _sanitize_script {# for markdown
Subroutine _sanitize_script: not called CC=1 SCAR=6.9
376 0 my $el = shift;
377 0 my $text = xml_escape $el->text;
378 0 $el->replace("<code class=script>$text</code>");
379 }
380  
381 162 ▶sub _dom_attrs {# for markdown
Subroutine _dom_attrs: called CC=9 SCAR=30.0
382 # translate ^{...} to id, style, class attributes
383 # берем только первый child и он должен быть текстом
384 162 my $el = shift;
385 162 ▶ my $text = $el->text
Branch: 1 branch
BranchTrueFalseTotal
my $text = $el->text or return 144 18 162
386 or return;
387 144 my $child1 = $el->child_nodes->first;
388 144 my $parent = $child1->parent;
389 return
390 144 ▶ unless $parent && $parent->type eq 'tag' && $child1->type eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $parent and $parent->type eq "tag" and $child1->type eq "text" 3 141 144
Condition50%3/6
$parent and $parent->type eq "tag" and $child1->type eq "text"
!ll&&!rl&&r
03141
$parent and $parent->type eq "tag"
!ll&&!rl&&r
00144
MC/DC33%1/3
$parent and $parent->type eq "tag" and $child1->type eq "text"
$parent $parent->type eq "tag" $child1->type eq "text"
Truth table50%2/4
A and B and C
A= $parentB= $parent->type eq "tag"C= $child1->type eq "text"
ABCresult
0XX0
10X0
1100
1111
391 141 my $content = $child1->content;
392 141 ▶ if ($content =~ s|^(?:\s*\{([^\}]+)\}\s*)||) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/^(?:\s*\{([^\}]+)\}\s*)//u) 0 141 141
393 0 my $attrs = $1;
394 0 utf8::upgrade($attrs);
395 # styles
396 0 ▶ $parent->{style} .= " $1"
Branch: 1 branch
BranchTrueFalseTotal
$attrs =~ s/(\S+\s*:\s*[^;]+;)//u and do { $parent->{"style"} .= " $1" } 0 0 0
397 while $attrs =~ s|(\S+\s*:\s*[^;]+;)||;
398 # id
399 0 ▶ $parent->{id} = $1
Branch: 1 branch
BranchTrueFalseTotal
$attrs =~ s/#(\S+)//u and do { $parent->{"id"} = $1 } 0 0 0
400 while $attrs =~ s|#(\S+)||;
401 # classes
402 0 ▶ $parent->{class} .= " $1"
Branch: 1 branch
BranchTrueFalseTotal
$attrs =~ s/\.?([^\.\s]+)//u and do { $parent->{"class"} .= " $1" } 0 0 0
403 while $attrs =~ s|\.?([^\.\s]+)||;
404 0 $child1->content($content);# replace
405 }
406 }
407  
408 0 ▶sub _pod {# file
Subroutine _pod: not called CC=4 SCAR=30.0
409 0 my ($c, $path) = @_;
410  
411 0 ▶ $c->_stash_pod($path)
Branch: 1 branch
BranchTrueFalseTotal
$c->_stash_pod($path) or return 0 0 0
412 or return;# $c->render_maybe('Mojolicious-Plugin-StaticShare/exception', format=>'html', handler=>'ep', status=>500,exception=>$ex)
413 #~ || $c->reply->exception($ex);
414
415 return $c->plugin->render_pod
416 0 ▶ ? $c->render(ref $c->plugin->render_pod ? %{$c->plugin->render_pod} : $c->plugin->render_pod,)
Branch: 2 branches
BranchTrueFalseTotal
$c->plugin->render_pod ? : 0 0 0
ref $c->plugin->render_pod ? : 0 0 0
417 : $c->render('Mojolicious-Plugin-StaticShare/pod', format=>'html', handler=>'ep',);
418
419 }
420  
421 1 ▶sub _stash_pod {
Subroutine _stash_pod: called CC=3 SCAR=11.7
422 1 my ($c, $path) = @_;
423 return
424 1 ▶ unless $c->app->renderer->helpers->{'pod_to_html'};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->app->renderer->helpers->{"pod_to_html"} 0 1 1
425  
426 1 my $content = decode('UTF-8', $path->slurp);
427 0 $content =~ s|$layout_re|$c->_layout_index($1)|e;#) {# || $content =~ s/(?:%\s*layout\s+"([^"]+)";?)//m
428 1 my $dom = Mojo::DOM->new($c->pod_to_html($content));
429 1 $dom->find('script')->each(\&_sanitize_script);
430 1 $dom->find('*')->each(\&_dom_attrs);
431 1 ▶ $c->stash(pod => $dom->at('body') || $dom);
Condition33%1/3
$dom->at('body') || $dom
l!l&&r!l&&!r
010
MC/DC0%0/2
$dom->at('body') || $dom
$dom->at('body') $dom
Truth table33%1/3
A || B
A= $dom->at('body')B= $dom
ABresult
1X1
011
000
432 #~ my $filename = $path->basename;
433 #~ $c->stash('title'=>$filename);
434 }
435  
436 0 ▶sub not_found {
Subroutine not_found: not called CC=2 SCAR=17.9
Pod undocumented
437 0 my $c = shift;
438 0 ▶ $c->render_maybe('Mojolicious-Plugin-StaticShare/not_found', format=>'html', handler=>'ep', status=>404,)
Branch: 1 branch
BranchTrueFalseTotal
$c->render_maybe("Mojolicious-Plugin-StaticShare/not_found", "format", "html", "handler", "ep", "status", 404) or $c->reply->not_found 0 0 0
439 or $c->reply->not_found;
440
441 };
442  
443 0 ▶sub _edit {
Subroutine _edit: not called CC=2 SCAR=17.9
444 0 my ($c, $path, $edit) = @_;
445 0 ▶ unless (defined $edit) {# get
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $edit) 0 0 0
446 0 $c->stash('edit'=> decode('UTF-8', $path->slurp));
447 0 $c->stash('title' => $c->i18n('Edit')." ".$c->stash('url_path')->to_route);
448 0 return $c->render('Mojolicious-Plugin-StaticShare/edit', format=>'html', handler=>'ep',);
449 }
450
451 # save
452 0 $path->spurt(encode('UTF-8', $edit));
453 0 $c->render(json=>{ok=>$path->to_string});
454 }
455  
456 1 1;
457  
458 =pod
459  
460 =encoding utf8
461  
462 Доброго всем
463  
464 =head1 Mojolicious::Plugin::StaticShare::Controller
465  
466 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
467  
468 =head1 NAME
469  
470 Mojolicious::Plugin::StaticShare::Controller - is an internal controller for L<Mojolicious::Plugin::StaticShare>.
471  
472  
473 =cut
↑ Summary StaticShare.pm »