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