MojoMojo
view release on metacpan or search on metacpan
lib/MojoMojo/Controller/Attachment.pm view on Meta::CPAN
$c->forward('check_file');
if ( $c->res->redirect ) {
$c->res->redirect( undef, 200 );
return $c->res->body('1');
}
$c->res->body('0');
}
=head2 attachment
Find and stash an attachment.
=cut
sub attachment : Chained CaptureArgs(1) {
my ( $self, $c, $att ) = @_;
# DBIC complains if find argument is not numeric
if ( $att !~ /^\d+$/ ) {
$c->detach('default');
}
$c->stash->{att} = $c->model("DBIC::Attachment")->find($att)
or $c->detach('default');
}
=head2 defaultaction
Set the default action for an attachment which is forwarding to a view.
=cut
sub defaultaction : PathPart('') Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
$c->forward('view');
}
=head2 view
Render the attachment in the browser (C<Content-Disposition: inline>), with
caching for 1 day.
=cut
sub view : Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
my $att = $c->stash->{att};
$c->detach('unauthorized', ['view']) if not $c->check_view_permission;
# avoid broken binary files
my $io_file = IO::File->new( $att->filename )
or $c->detach('default');
$io_file->binmode;
$c->res->output( $io_file );
$c->res->header( 'content-type', $att->contenttype );
$c->res->header(
"Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $att->name ) );
$c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
}
=head2 download
Forwards to L</view> then forces the attachment to be downloaded
(C<Content-Disposition: attachment>) and disables caching.
=cut
sub download : Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
$c->forward('view');
$c->res->header( "Content-Disposition" => "attachment; filename=" . URI::Escape::uri_escape_utf8( $c->stash->{att}->name ) );
$c->res->header( 'Cache-Control', 'no-cache' );
}
=head2 thumb
Thumb action for attachments. Makes 100x100px thumbnails.
=cut
sub thumb : Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
$c->detach('unauthorized', ['view']) if not $c->check_view_permission;
my $att = $c->stash->{att};
my $photo;
unless ( $photo = $att->photo ) {
return $c->res->body($c->loc('Can only make thumbnails of photos'));
}
$photo->make_thumb() unless -f $att->thumb_filename;
my $io_file = IO::File->new( $att->thumb_filename )
or $c->detach('default');
$io_file->binmode;
$c->res->output( $io_file );
$c->res->header( 'content-type', $att->contenttype );
$c->res->header( "Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $att->name ) );
$c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
}
=head2 inline
Show 800x600 inline versions of photo attachments.
=cut
sub inline : Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
$c->detach('unauthorized', ['view']) if not $c->check_view_permission;
my $att = $c->stash->{att};
my $photo;
unless ( $photo = $att->photo ) {
return $c->res->body($c->loc('Can only make inline version of photos'));
}
$photo->make_inline unless -f $att->inline_filename;
my $io_file = IO::File->new( $att->inline_filename )
or $c->detach('default');
$io_file->binmode;
$c->res->output( $io_file );
$c->res->header( 'content-type', $c->stash->{att}->contenttype );
$c->res->header(
"Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $c->stash->{att}->name ) );
$c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
}
=head2 delete
Delete the attachment from this node. Will leave the original file on the
file system but delete its thumbnail and inline versions.
=cut
sub delete : Chained('attachment') Args(0) {
my ( $self, $c ) = @_;
$c->detach('unauthorized', ['delete']) if not $c->forward('auth');
$c->stash->{att}->delete();
$c->forward('attachments');
}
=head1 AUTHOR
Marcus Ramberg C<marcus@nordaaker.com>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
( run in 0.522 second using v1.01-cache-2.11-cpan-df04353d9ac )