Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

in a hurry, no time for commit msg

  • Loading branch information...
commit 7b8e34cf19695992dab418df3fb16af5e499e713 1 parent 038908b
David Schmidt authored
View
2  Changes
@@ -0,0 +1,2 @@
+0.001_001 2011-12-14
+ * initial release
View
19 dist.ini
@@ -1,12 +1,11 @@
-name = Catalyst-TraitFor-Controller-Sendfile
-author = David Schmidt <davewood@cpan.org>
-author = Florian Ragwitz <rafl@debian.org>
-author = Tomas Doran (t0m) <bobtfish@bobtfish.net>
-license = Perl_5
-copyright_holder = David Schmidt
-copyright_year = 2011
-
-version = 0.001_001
+name = Catalyst-TraitFor-Controller-Sendfile
+version = 0.001_001
+author = David Schmidt <davewood@cpan.org>
+author = Florian Ragwitz <rafl@debian.org>
+author = Tomas Doran (t0m) <bobtfish@bobtfish.net>
+license = Perl_5
+copyright_holder = David Schmidt
+copyright_year = 2011
[@Basic]
[PkgVersion]
@@ -25,7 +24,7 @@ perltidyrc = perltidy.rc
[Prereqs / RuntimeRequires]
Moose = 0
-Catalyst::Runtime = 5.80030
+Catalyst::Runtime = 5.90007
MIME::Types = 0
Method::Signatures::Simple = 0
MooseX::Types::Path::Class = 0
View
43 lib/Catalyst/TraitFor/Controller/Sendfile.pm
@@ -23,7 +23,7 @@ use namespace::autoclean;
sub some_action : Local {
my ($self, $c) = @_;
- $self->sendfile($c, file(qw/ path to file/));
+ $self->sendfile($c, file(qw/ path to file/), 'image/jpeg');
}
=head1 DESCRIPTION
@@ -67,32 +67,41 @@ has '_mime_types' => (
=cut
-method set_content_type_for_file ($c, $file) {
- my ($ext) = $file->basename =~ /\.(.+?)$/;
- if (defined $ext) {
- my $content_type = $self->_mime_types->mimeTypeOf($ext);
- if (defined $content_type) {
- $c->res->content_type($content_type);
- }
- else {
- die("No content-type found for '$ext'");
- }
+method set_content_type_for_file ($c, $file, $content_type) {
+ if (!$content_type) {
+ my ($ext) = $file->basename =~ /\.(.+?)$/;
+
+ die "Could not find file extension. (" . $file->basename . ")"
+ unless defined $ext;
+
+ $content_type = $self->_mime_types->mimeTypeOf($ext);
+
+ die "No content-type found for '$ext'"
+ unless defined $content_type;
}
+
+ die "No content-type found. (" . $file->basename . ")"
+ unless defined $content_type;
+
+ $c->res->content_type($content_type);
}
=head2 sendfile
-You call sendfile with $c and a Path::Class::File object. The file path can't be seen
-by the client. Your webserver should check if the 'X-Sendfile' header is set and if so deliver the file.
+You call sendfile with $c, Path::Class::File object and an optional content_type.
+The file path can't be seen by the client.
+Your webserver should check if the 'X-Sendfile' header is set and if so deliver the file.
+If you do not define a content_type it will be guessed by the file extension.
=cut
-method sendfile ($c, $file) {
- die("No file supplied to sendfile with") unless $file;
+method sendfile ($c, $file, $content_type) {
+ die "No file supplied to sendfile with" unless $file;
my $file_ob = to_File($file);
- die("Not supplied with a Path::Class::File or something that can be coerced to be one ($file)") unless $file = $file_ob;
+ die "Not supplied with a Path::Class::File or something that can be coerced to be one ($file)"
+ unless $file = $file_ob;
- $self->set_content_type_for_file($c, $file);
+ $content_type = $self->set_content_type_for_file($c, $file, $content_type);
my $engine = $ENV{CATALYST_ENGINE} || 'HTTP';
View
11 t/01_basic.t
@@ -2,13 +2,19 @@ use strict;
use warnings;
use Test::More;
use HTTP::Request::Common;
-
use FindBin;
use lib "$FindBin::Bin/lib";
use Path::Class;
+use Catalyst::Test 'TestApp';
+BEGIN { use_ok('TestApp'); }
-use Catalyst::Test 'TestApp';
+my $testapp = new_ok ( 'TestApp' );
+
+{
+ my $controller = $testapp->controller('Root');
+ can_ok ( $controller, 'sendfile' );
+}
{
local $ENV{CATALYST_ENGINE} = 'FastCGI';
@@ -31,4 +37,3 @@ use Catalyst::Test 'TestApp';
}
done_testing;
-
Please sign in to comment.
Something went wrong with that request. Please try again.