Silki/Controller/File.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 144 CC 8 SCAR 15.9
CC
8
Cov
0%
file_GET_html
12.0
_set_file
6.0
content
6.0
CRAP 4.9
1 package Silki::Controller::File;
2 {
3 $Silki::Controller::File::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Silki::I18N qw( loc );
11 use Silki::Schema::File;
12  
13 use Moose;
14  
15 BEGIN { extends 'Silki::Controller::Base' }
16  
17 with 'Silki::Role::Controller::File';
18  
19 sub _set_file : Chained('/wiki/_set_wiki') : PathPart('file') : CaptureArgs(1) {
20 my $self = shift;
21 my $c = shift;
22 my $file_id = shift;
23  
24 my $file = Silki::Schema::File->new( file_id => $file_id );
25  
26 my $wiki = $c->stash()->{wiki};
27 $c->redirect_and_detach( $wiki->uri( with_host => 1 ) )
28 unless $file && $file->wiki()->wiki_id() == $wiki->wiki_id();
29  
30 $c->stash()->{file} = $file;
31 }
32  
33 sub file : Chained('_set_file') : PathPart('') : Args(0) : ActionClass('+Silki::Action::REST') {
34 }
35  
36 sub file_GET_html {
37 my $self = shift;
38 my $c = shift;
39  
40 my $file = $c->stash()->{file};
41  
42 if ( $file->is_displayable_in_browser() ) {
43 my $name = $file->filename();
44 $name = substr( $name, 0, 16 ) . q{ ...} if length $name > 16;
45  
46 $c->add_tab(
47 {
48 uri => $file->uri(),
49 label => $name,
50 tooltip => loc( 'Contents of %1', $file->filename() ),
51 is_selected => 1,
52 }
53 );
54  
55 $c->stash()->{template} = '/file/view-in-frame';
56 }
57 else {
58 $self->_download( $c, $file, 'attachment' );
59 }
60  
61 return;
62 }
63  
64 sub file_DELETE {
65 my $self = shift;
66 my $c = shift;
67  
68 my $wiki = $c->stash()->{wiki};
69  
70 $self->_require_permission_for_wiki( $c, $wiki, 'Delete' );
71  
72 my $file = $c->stash()->{file};
73  
74 my $msg = loc( 'Deleted the file %1', $file->filename() );
75  
76 $file->delete( user => $c->user() );
77  
78 $c->session_object()->add_message($msg);
79  
80 $c->redirect_and_detach( $wiki->uri( with_host => 1 ) );
81 }
82  
83 sub content : Chained('_set_file') : PathPart('content') : Args(0) {
84 my $self = shift;
85 my $c = shift;
86  
87 my $file = $c->stash()->{file};
88  
89 # If the file type is something like text/x-python, the browser might
90 # prompt the user to save the file. However, if we force text/plain, the
91 # browser displays the file nicely.
92 my $ct = $file->mime_type() =~ /^text/ ? 'text/plain' : $file->mime_type();
93  
94 $self->_download( $c, $file, 'inline', $ct );
95  
96 return;
97 }
98  
99 sub download : Chained('_set_file') : PathPart('content_as_attachment') : Args(0) {
100 my $self = shift;
101 my $c = shift;
102  
103 $self->_download( $c, $c->stash()->{file}, 'attachment' );
104  
105 return;
106 }
107  
108 sub _download {
109 my $self = shift;
110 my $c = shift;
111 my $file = shift;
112 my $disposition = shift;
113 my $content_type = shift || $file->mime_type();
114  
115 $c->response()->status(200);
116  
117 $content_type = 'text/plain'
118 if $disposition eq 'inline' && $content_type =~ /^application/;
119  
120 $c->response()->content_type($content_type);
121  
122 my $name = $file->filename();
123 $name =~ s/\"/\\"/g;
124  
125 $c->response()
126 ->header( 'Content-Disposition' => qq{$disposition; filename="$name"} );
127 $c->response()->content_length( $file->file_size() );
128 $c->response()->header( 'X-Sendfile' => $file->file_on_disk() );
129  
130 $c->detach();
131 }
132  
133 sub small_image : Chained('_set_file') : PathPart('small') : Args(0) {
134 my $self = shift;
135 my $c = shift;
136  
137 $c->status_not_found()
138 unless $c->stash()->{file}->is_browser_displayable_image();
139  
140 $self->_serve_image( $c, $c->stash()->{file}, 'small_image_file' );
141 }
142  
143 sub thumbnail : Chained('_set_file') : PathPart('thumbnail') : Args(0) {
144 my $self = shift;
145 my $c = shift;
146  
147 $c->status_not_found()
148 unless $c->stash()->{file}->is_browser_displayable_image();
149  
150 $self->_serve_image( $c, $c->stash()->{file}, 'thumbnail_file' );
151 }
152  
153 sub delete_confirmation : Chained('_set_file') : PathPart('delete_confirmation') : Args(0) {
154 my $self = shift;
155 my $c = shift;
156  
157 $self->_require_permission_for_wiki( $c, $c->stash()->{wiki}, 'Delete' );
158  
159 $c->stash()->{template} = '/file/delete-confirmation';
160 }
161  
162 __PACKAGE__->meta()->make_immutable();
163  
164 1;
165  
166 # ABSTRACT: Controller class for files
167  
168 __END__