| 1 | package Yukki::Web::Controller::Attachment; | ||||||||||||||||||||||||||||
| 2 | 1 | $Yukki::Web::Controller::Attachment::VERSION = '0.991_007'; # TRIAL | |||||||||||||||||||||||||||
| 3 | 1 | $Yukki::Web::Controller::Attachment::VERSION = '0.991007'; | |||||||||||||||||||||||||||
| 4 | 1 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | with 'Yukki::Web::Controller'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Yukki::Error qw( http_throw ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ABSTRACT: Controller for uploading, downloading, and viewing attachments | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub fire { | ||||||||||||||||||||||||||
|
Subroutine
fire: called CC=6 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | my $action = $ctx->request->path_parameters->{action}; | |||||||||||||||||||||||||||
| 22 | 2 | ▶ | if ($action eq 'download') { $self->download_file($ctx) } | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 23 | 0 | elsif ($action eq 'upload') { $self->upload_file($ctx) } | |||||||||||||||||||||||||||
| 24 | 0 | elsif ($action eq 'view') { $self->view_file($ctx) } | |||||||||||||||||||||||||||
| 25 | 0 | elsif ($action eq 'rename') { $self->rename_file($ctx) } | |||||||||||||||||||||||||||
| 26 | 0 | elsif ($action eq 'remove') { $self->remove_file($ctx) } | |||||||||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||||||||||
| 28 | 1 | http_throw('That attachment action does not exist.', { | |||||||||||||||||||||||||||
| 29 | status => 'NotFound', | ||||||||||||||||||||||||||||
| 30 | }); | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub lookup_file { | ||||||||||||||||||||||||||
|
Subroutine
lookup_file: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 1 | my ($self, $repo_name, $file) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $repository = $self->model('Repository', { name => $repo_name }); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $final_part = pop @$file; | |||||||||||||||||||||||||||
| 41 | 1 | my $filetype; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($final_part =~ s/\.(?<filetype>[a-z0-9]+)$//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | $filetype = $+{filetype}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my $path = join '/', @$file, $final_part; | |||||||||||||||||||||||||||
| 47 | 1 | return $repository->file({ path => $path, filetype => $filetype }); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub download_file { | ||||||||||||||||||||||||||
|
Subroutine
download_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | my $repo_name = $ctx->request->path_parameters->{repository}; | |||||||||||||||||||||||||||
| 55 | 1 | my $path = $ctx->request->path_parameters->{file}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | my $file = $self->lookup_file($repo_name, $path); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | $ctx->response->content_type('application/octet'); | |||||||||||||||||||||||||||
| 60 | 1 | $ctx->response->body([ scalar $file->fetch ]); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub view_file { | ||||||||||||||||||||||||||
|
Subroutine
view_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $repo_name = $ctx->request->path_parameters->{repository}; | |||||||||||||||||||||||||||
| 68 | 0 | my $path = $ctx->request->path_parameters->{file}; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $file = $self->lookup_file($repo_name, $path); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $ctx->response->content_type($file->media_type); | |||||||||||||||||||||||||||
| 73 | 0 | $ctx->response->body([ scalar $file->fetch ]); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub rename_file { | ||||||||||||||||||||||||||
|
Subroutine
rename_file: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $repo_name = $ctx->request->path_parameters->{repository}; | |||||||||||||||||||||||||||
| 81 | 0 | my $path = $ctx->request->path_parameters->{file}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $file = $self->lookup_file($repo_name, $path); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($ctx->request->method eq 'POST') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | my $new_name = $ctx->request->parameters->{yukkiname_new}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $part = qr{[_a-z0-9-.]+(?:\.[_a-z0-9-]+)*}i; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($new_name =~ m{^$part(?:/$part)*$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (my $user = $ctx->session->{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $file->author_name($user->{name}); | |||||||||||||||||||||||||||
| 92 | 0 | $file->author_email($user->{email}); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $new_file = $file->rename({ | |||||||||||||||||||||||||||
| 96 | full_path => $new_name, | ||||||||||||||||||||||||||||
| 97 | comment => 'Renamed ' . $file->full_path . ' to ' . $new_name, | ||||||||||||||||||||||||||||
| 98 | }); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my $parent = $new_file->parent // $file->repository->default_file; | ||||||||||||||||||||||||||
|
Condition0%0/3
$new_file->parent // $file->repository->default_file
MC/DC0%0/2
$new_file->parent // $file->repository->default_file
$new_file->parent
$file->repository->default_file
Truth table0%0/3
A // B
A= $new_file->parentB= $file->repository->default_file
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $ctx->response->redirect(join '/', | |||||||||||||||||||||||||||
| 103 | '/page/edit', $repo_name, $parent->full_path); | ||||||||||||||||||||||||||||
| 104 | 0 | return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | $ctx->add_errors('the new name must contain only letters, numbers, underscores, dashes, periods, and slashes'); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | $ctx->response->body( | ||||||||||||||||||||||||||||
| 112 | 0 | $self->view('Attachment')->rename($ctx, { | |||||||||||||||||||||||||||
| 113 | title => $file->title, | ||||||||||||||||||||||||||||
| 114 | repository => $repo_name, | ||||||||||||||||||||||||||||
| 115 | page => $file->full_path, | ||||||||||||||||||||||||||||
| 116 | file => $file, | ||||||||||||||||||||||||||||
| 117 | }) | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub remove_file { | ||||||||||||||||||||||||||
|
Subroutine
remove_file: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $repo_name = $ctx->request->path_parameters->{repository}; | |||||||||||||||||||||||||||
| 126 | 0 | my $path = $ctx->request->path_parameters->{file}; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $file = $self->lookup_file($repo_name, $path); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $return_to = $file->parent // $file->repository->default_file; | ||||||||||||||||||||||||||
|
Condition0%0/3
$file->parent // $file->repository->default_file
MC/DC0%0/2
$file->parent // $file->repository->default_file
$file->parent
$file->repository->default_file
Truth table0%0/3
A // B
A= $file->parentB= $file->repository->default_file
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($ctx->request->method eq 'POST' and $confirmed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ctx->request->method eq "POST" and $confirmed
MC/DC0%0/2
$ctx->request->method eq "POST" and $confirmed
$ctx->request->method eq "POST"
$confirmed
Truth table0%0/3
A and B
A= $ctx->request->method eq "POST"B= $confirmed
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (my $user = $ctx->session->{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $file->author_name($user->{name}); | |||||||||||||||||||||||||||
| 137 | 0 | $file->author_email($user->{email}); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | $file->remove({ | ||||||||||||||||||||||||||||
| 141 | 0 | comment => 'Removing ' . $file->full_path . ' from repository.', | |||||||||||||||||||||||||||
| 142 | }); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | $ctx->response->redirect(join '/', '/page/view', $repo_name, $return_to->full_path); | |||||||||||||||||||||||||||
| 145 | 0 | return; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $ctx->response->body( | ||||||||||||||||||||||||||||
| 149 | 0 | $self->view('Attachment')->remove($ctx, { | |||||||||||||||||||||||||||
| 150 | title => $file->title, | ||||||||||||||||||||||||||||
| 151 | repository => $repo_name, | ||||||||||||||||||||||||||||
| 152 | page => $file->full_path, | ||||||||||||||||||||||||||||
| 153 | file => $file, | ||||||||||||||||||||||||||||
| 154 | return_link => join('/', '/page/view', $repo_name, $return_to->full_path), | ||||||||||||||||||||||||||||
| 155 | }) | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub upload_file { | ||||||||||||||||||||||||||
|
Subroutine
upload_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 0 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | my $repo_name = $ctx->request->path_parameters->{repository}; | |||||||||||||||||||||||||||
| 164 | 0 | my $path = $ctx->request->path_parameters->{file}; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $file = $self->lookup_file($repo_name, $path); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if (my $user = $ctx->session->{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | $file->author_name($user->{name}); | |||||||||||||||||||||||||||
| 170 | 0 | $file->author_email($user->{email}); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | my $upload = $ctx->request->uploads->{file}; | |||||||||||||||||||||||||||
| 174 | 0 | $file->store({ | |||||||||||||||||||||||||||
| 175 | filename => $upload->tempname, | ||||||||||||||||||||||||||||
| 176 | comment => 'Uploading file ' . $upload->filename, | ||||||||||||||||||||||||||||
| 177 | }); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | $ctx->response->content_type('application/json'); | |||||||||||||||||||||||||||
| 180 | 0 | $ctx->response->body( | |||||||||||||||||||||||||||
| 181 | encode_json({ | ||||||||||||||||||||||||||||
| 182 | viewable => 1, | ||||||||||||||||||||||||||||
| 183 | repository_path => join('/', $repo_name, $file->full_path), | ||||||||||||||||||||||||||||
| 184 | }) | ||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||