use strict; use warnings; use Plack::Response; use Path::Tiny; use File::MimeInfo; sub return_psgi { my $self = shift; my $path_file = shift; my $content_type = mimetype($path_file); $content_type = 'application/octet-stream' if not defined $content_type; my $content = path($path_file); my $content_download = $content->slurp; require File::Spec::Unix; my $basename = $path_file; $basename =~ s|\\|/|g; $basename = ( File::Spec::Unix->splitpath($basename) )[2]; $basename =~ s|[^\w\.-]+|_|g; my $response = Plack::Response->new(200); $response->headers( [ 'Content-Type' => $content_type, 'Content-Disposition' => qq[attachment; filename="$basename"], ], ); $response->body($content_download); return $response->finalize; } #### open (FILE, "< $path_file") or die "can't open $path_file: $!"; binmode FILE; local $/ = \102400; while () { my $var = scalar $_; $response->body($var); return $response->finalize; } close FILE; #### use IO::Handle; my $BLOCK_SIZE = 1024 * 1024; open(my $fh, "<:raw", $path_file) or die "Can't read from input file $path_file $!\n"; while (read($fh, $buffer, $BLOCK_SIZE)) { $response->body($buffer); $fh->autoflush; } close $fh; return $response->finalize;