| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::HTTP::SendFile; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::HTTP::SendFile - Send files to browser. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Nile::HTTP::SendFile - Send files to browser. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use Nile::Base; | ||
| 30 | use Module::Load; | ||
| 31 | |||
| 32 | use List::Util qw(min); | ||
| 33 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 34 | sub send_file { | ||
| 35 | |||
| 36 | my ($self, $res, $file, $opt) = @_; | ||
| 37 | |||
| 38 | my %options = ( | ||
| 39 | type => $self->app->mime->for_file($file) || "application/x-download", | ||
| 40 | range => !ref($file), | ||
| 41 | cache => 0, | ||
| 42 | inline => 0, | ||
| 43 | %{$opt || {}}, | ||
| 44 | ); | ||
| 45 | |||
| 46 | if (!$options{cache}) { | ||
| 47 | $res->header(Expires => "Sat, 01 Jan 2000 00:00:00 GMT"); | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $res->remove_header("Expires"); | ||
| 51 | if (!ref($file)) { | ||
| 52 | my $lastmod = $res->http_date((stat $file)[9]); | ||
| 53 | my $ifmod = $ENV{HTTP_IF_MODIFIED_SINCE}; | ||
| 54 | if ($ifmod && $ifmod eq $lastmod) { | ||
| 55 | $res->header(Status => "304 Not Modified"; | ||
| 56 | return \""; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | $res->header('Last-Modified' => $lastmod); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | my $len = ref($file) ? length(${$file}) : -s $file; | ||
| 65 | |||
| 66 | my ($start, $end) = (0, $len-1); | ||
| 67 | |||
| 68 | #if ($options{range}) { | ||
| 69 | # ($start, $end) = $self->parse_range($ENV{HTTP_RANGE}, $len); | ||
| 70 | #} | ||
| 71 | #require PerlIO::subfile; | ||
| 72 | #open my $fh, "<:raw:subfile(start=$start,end=".($end+1).")", $file or return $self->return_403; | ||
| 73 | |||
| 74 | |||
| 75 | if ($options{range} && defined $ENV{HTTP_RANGE} && $ENV{HTTP_RANGE} =~ /\Abytes=(\d*)-(\d*)\z/ixms) { | ||
| 76 | my ($from, $to) = ($1, $2); | ||
| 77 | if ($from ne "" && $to ne "" && $from <= $to && $to < $len) { | ||
| 78 | # 0-0, 0-499, 500-999 | ||
| 79 | $start = $from; | ||
| 80 | $end = $to; | ||
| 81 | } | ||
| 82 | elsif ($from ne "" && $to eq "" && $from < $len) { | ||
| 83 | # 0-, 500-, 999- | ||
| 84 | $start = $from; | ||
| 85 | } | ||
| 86 | elsif ($from eq "" && $to ne "" && $to > 0 && $to <= $len) { | ||
| 87 | # -1, -500, -1000 | ||
| 88 | $start = $len - $to; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | my $size = $end - $start + 1; | ||
| 93 | |||
| 94 | $res->header("Accept-Ranges" => "bytes"); | ||
| 95 | $res->header("Content-Length" => $size); | ||
| 96 | $res->header("Content-Type" => $options{type}); | ||
| 97 | |||
| 98 | # Content-Disposition: attachment; filename="filename.jpg" | ||
| 99 | if (!$options{inline}) { | ||
| 100 | $res->header("Content-Disposition" => "attachment"); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (!($start == 0 && $end == $len-1)) { | ||
| 104 | $res->header("Status" => "206 Partial Content"); | ||
| 105 | $res->header("Content-Range" => "bytes $start-$end/$len"); | ||
| 106 | } | ||
| 107 | |||
| 108 | return _read_block($file, $start, $size); | ||
| 109 | } | ||
| 110 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 111 | sub parse_range { | ||
| 112 | |||
| 113 | my ($self, $range, $len) = @_; | ||
| 114 | |||
| 115 | $range =~ /^(\d*)-(\d*)$/ or return; | ||
| 116 | |||
| 117 | my ($start, $end) = ($1, $2); | ||
| 118 | |||
| 119 | if (length $start and length $end) { | ||
| 120 | return if $start > $end; # "200-100" | ||
| 121 | return if $end >= $len; # "0-0" on a 0-length file | ||
| 122 | return ($start, $end); | ||
| 123 | } | ||
| 124 | elsif (length $start) { | ||
| 125 | return if $start >= $len; # "0-" on a 0-length file | ||
| 126 | return ($start, $len-1); | ||
| 127 | } | ||
| 128 | elsif (length $end) { | ||
| 129 | return if $end > $len; # "-1" on a 0-length file | ||
| 130 | return ($len-$end, $len-1); | ||
| 131 | } | ||
| 132 | |||
| 133 | return; | ||
| 134 | } | ||
| 135 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 136 | sub _read_block { | ||
| 137 | |||
| 138 | my ($file, $start, $size) = @_; | ||
| 139 | |||
| 140 | my $data = ""; | ||
| 141 | |||
| 142 | open my $fh, '<', $file or croak "open: $!"; | ||
| 143 | seek $fh, $start, 0; | ||
| 144 | |||
| 145 | my ($n, $buf); | ||
| 146 | while ($n = read $fh, $buf, min($size, 64*1024)) { | ||
| 147 | $size -= length $buf; | ||
| 148 | $data .= $buf; | ||
| 149 | } | ||
| 150 | |||
| 151 | croak "read: $!" if !defined $n; | ||
| 152 | close $fh or croak "close: $!"; | ||
| 153 | |||
| 154 | return \$data; | ||
| 155 | } | ||
| 156 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 157 | |||
| 158 | =pod | ||
| 159 | |||
| 160 | =head1 Bugs | ||
| 161 | |||
| 162 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 163 | |||
| 164 | =head1 HOMEPAGE | ||
| 165 | |||
| 166 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 167 | |||
| 168 | =head1 SOURCE | ||
| 169 | |||
| 170 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 171 | |||
| 172 | =head1 SEE ALSO | ||
| 173 | |||
| 174 | See L<Nile> for details about the complete framework. | ||
| 175 | |||
| 176 | =head1 AUTHOR | ||
| 177 | |||
| 178 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 179 | Website: http://www.mewsoft.com | ||
| 180 | |||
| 181 | =head1 COPYRIGHT AND LICENSE | ||
| 182 | |||
| 183 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 184 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 185 | |||
| 186 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | 1; |