$Yukki::Web::Controller::Attachment::VERSION
=
'0.991_002'
;
$Yukki::Web::Controller::Attachment::VERSION
=
'0.991002'
;
use
v5.24;
sub
fire {
my
(
$self
,
$ctx
) =
@_
;
my
$action
=
$ctx
->request->path_parameters->{action};
if
(
$action
eq
'download'
) {
$self
->download_file(
$ctx
) }
elsif
(
$action
eq
'upload'
) {
$self
->upload_file(
$ctx
) }
elsif
(
$action
eq
'view'
) {
$self
->view_file(
$ctx
) }
elsif
(
$action
eq
'rename'
) {
$self
->rename_file(
$ctx
) }
elsif
(
$action
eq
'remove'
) {
$self
->remove_file(
$ctx
) }
else
{
http_throw(
'That attachment action does not exist.'
, {
status
=>
'NotFound'
,
});
}
}
sub
lookup_file {
my
(
$self
,
$repo_name
,
$file
) =
@_
;
my
$repository
=
$self
->model(
'Repository'
, {
name
=>
$repo_name
});
my
$final_part
=
pop
@$file
;
my
$filetype
;
if
(
$final_part
=~ s/\.(?<filetype>[a-z0-9]+)$//) {
$filetype
= $+{filetype};
}
my
$path
=
join
'/'
,
@$file
,
$final_part
;
return
$repository
->file({
path
=>
$path
,
filetype
=>
$filetype
});
}
sub
download_file {
my
(
$self
,
$ctx
) =
@_
;
my
$repo_name
=
$ctx
->request->path_parameters->{repository};
my
$path
=
$ctx
->request->path_parameters->{file};
my
$file
=
$self
->lookup_file(
$repo_name
,
$path
);
$ctx
->response->content_type(
'application/octet'
);
$ctx
->response->body([
scalar
$file
->fetch ]);
}
sub
view_file {
my
(
$self
,
$ctx
) =
@_
;
my
$repo_name
=
$ctx
->request->path_parameters->{repository};
my
$path
=
$ctx
->request->path_parameters->{file};
my
$file
=
$self
->lookup_file(
$repo_name
,
$path
);
$ctx
->response->content_type(
$file
->media_type);
$ctx
->response->body([
scalar
$file
->fetch ]);
}
sub
rename_file {
my
(
$self
,
$ctx
) =
@_
;
my
$repo_name
=
$ctx
->request->path_parameters->{repository};
my
$path
=
$ctx
->request->path_parameters->{file};
my
$file
=
$self
->lookup_file(
$repo_name
,
$path
);
if
(
$ctx
->request->method eq
'POST'
) {
my
$new_name
=
$ctx
->request->parameters->{yukkiname_new};
my
$part
=
qr{[_a-z0-9-.]+(?:\.[_a-z0-9-]+)*}
i;
if
(
$new_name
=~ m{^
$part
(?:/
$part
)*$}) {
if
(
my
$user
=
$ctx
->session->{user}) {
$file
->author_name(
$user
->{name});
$file
->author_email(
$user
->{email});
}
my
$new_file
=
$file
->
rename
({
full_path
=>
$new_name
,
comment
=>
'Renamed '
.
$file
->full_path .
' to '
.
$new_name
,
});
my
$parent
=
$new_file
->parent //
$file
->repository->default_file;
$ctx
->response->redirect(
join
'/'
,
'/page/edit'
,
$repo_name
,
$parent
->full_path);
return
;
}
else
{
$ctx
->add_errors(
'the new name must contain only letters, numbers, underscores, dashes, periods, and slashes'
);
}
}
$ctx
->response->body(
$self
->view(
'Attachment'
)->
rename
(
$ctx
, {
title
=>
$file
->title,
repository
=>
$repo_name
,
page
=>
$file
->full_path,
file
=>
$file
,
})
);
}
sub
remove_file {
my
(
$self
,
$ctx
) =
@_
;
my
$repo_name
=
$ctx
->request->path_parameters->{repository};
my
$path
=
$ctx
->request->path_parameters->{file};
my
$file
=
$self
->lookup_file(
$repo_name
,
$path
);
my
$return_to
=
$file
->parent //
$file
->repository->default_file;
my
$confirmed
=
$ctx
->request->body_parameters->{confirmed};
if
(
$ctx
->request->method eq
'POST'
and
$confirmed
) {
if
(
my
$user
=
$ctx
->session->{user}) {
$file
->author_name(
$user
->{name});
$file
->author_email(
$user
->{email});
}
$file
->remove({
comment
=>
'Removing '
.
$file
->full_path .
' from repository.'
,
});
$ctx
->response->redirect(
join
'/'
,
'/page/view'
,
$repo_name
,
$return_to
->full_path);
return
;
}
$ctx
->response->body(
$self
->view(
'Attachment'
)->remove(
$ctx
, {
title
=>
$file
->title,
repository
=>
$repo_name
,
page
=>
$file
->full_path,
file
=>
$file
,
return_link
=>
join
(
'/'
,
'/page/view'
,
$repo_name
,
$return_to
->full_path),
})
);
}
sub
upload_file {
my
(
$self
,
$ctx
) =
@_
;
my
$repo_name
=
$ctx
->request->path_parameters->{repository};
my
$path
=
$ctx
->request->path_parameters->{file};
my
$file
=
$self
->lookup_file(
$repo_name
,
$path
);
if
(
my
$user
=
$ctx
->session->{user}) {
$file
->author_name(
$user
->{name});
$file
->author_email(
$user
->{email});
}
my
$upload
=
$ctx
->request->uploads->{file};
$file
->store({
filename
=>
$upload
->tempname,
comment
=>
'Uploading file '
.
$upload
->filename,
});
$ctx
->response->content_type(
'application/json'
);
$ctx
->response->body(
encode_json({
viewable
=> 1,
repository_path
=>
join
(
'/'
,
$repo_name
,
$file
->full_path),
})
);
}
1;