3 SGN::Controller::Download - serve file downloads
7 package SGN
::Controller
::Download
;
9 use namespace
::autoclean
;
13 BEGIN { extends
'Catalyst::Controller' }
17 =head2 download_static
19 Public path: /download/path/to/file/under/site/root.ext
21 Try to find a file relative to the site root and serve it with the
22 proper headers to trigger download dialog in the user's browser.
26 sub download_static
:Path
('/download') {
27 my ( $self, $c, @path ) = @_;
29 my $file = $c->path_to( $c->config->{root
}, @path );
30 $c->stash->{download_filename
} = $file;
32 $c->forward('download');
36 =head1 PRIVATE ACTIONS
38 =head2 reported_download
42 Same as download above, but sends an email to the address in the
43 'bugs_email' conf var.
47 sub reported_download
:Private
{
48 my ( $self, $c ) = @_;
50 my $info_string = Data
::Dump
::dump( $c->stash->{download_info
} || 'no additional information' );
52 # send an email with the information
53 $c->stash->{email
} = {
54 to
=> $c->config->{bugs_email
},
55 from
=> 'sgn-reported-downloads@solgenomics.net',
56 subject
=> "File download: ".($c->stash->{download_filename
} || '(no filename set)'),
57 body
=> join( '', map "$_\n",
58 "File : ".($c->stash->{download_filename
} || '(no filename set)'),
59 "User info : $info_string",
60 $c->view('Email::ErrorEmail')->summary_text($c),
63 $c->forward('View::Email');
65 # serve the downloaded file
66 $c->forward('download');
73 Serves the file named in C<$c-E<gt>stash-E<gt>{download_filename}> to
74 the user's browser as a download. C<download_filename> should be an
77 Sets the Content-disposition response headers appropriate to trigger a
78 file-download behavior in the client browser. Does NOT set the
79 content-type, you should do that before forwarding to this
80 (e.g. C<$c-E<gt>res-E<gt>content_type('text/plain')>).
84 sub download
:Private
{
85 my ( $self, $c ) = @_;
87 my $file = $c->stash->{download_filename
};
89 $c->throw_404 unless defined( $file ) && -e
$file;
91 $c->forward('set_download_headers');
92 $c->log->debug("static file download: $file") if $c->debug;
93 $c->serve_static_file( $file );
97 =head2 set_download_headers
101 Sets the Content-disposition response headers appropriate to trigger a
102 file-download behavior in the client browser. If
103 C<$c-E<gt>stash-E<gt>{download_filename}> is set, will set the download's
104 filename to the basename of that path.
107 content-type, you should do that before forwarding to this
108 (e.g. C<$c-E<gt>res-E<gt>content_type('text/plain')>).
112 sub set_download_headers
:Private
{
113 my ( $self, $c ) = @_;
115 if( my $bn = basename
( $c->stash->{download_filename
} ) ) {
116 $c->res->headers->header( 'Content-Disposition' => "attachment; filename=$bn" );
118 $c->res->headers->header( 'Content-Disposition' => 'attachment' );