clean
[sgn.git] / lib / SGN / Controller / Download.pm
blob8a2dc85347901d6ac261122ebe003a93ee16d303
1 =head1 NAME
3 SGN::Controller::Download - serve file downloads
5 =cut
7 package SGN::Controller::Download;
8 use Moose;
9 use namespace::autoclean;
11 use File::Basename;
13 BEGIN { extends 'Catalyst::Controller' }
15 =head1 PUBLIC ACTIONS
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.
24 =cut
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
40 Private.
42 Same as download above, but sends an email to the address in the
43 'bugs_email' conf var.
45 =cut
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');
69 =head2 download
71 Private.
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
75 absolute path.
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')>).
82 =cut
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
99 Private.
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.
106 Does NOT set the
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')>).
110 =cut
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" );
117 } else {
118 $c->res->headers->header( 'Content-Disposition' => 'attachment' );