FastAPI/Response/File.pm

filter: Statementstmt 100% 48 / 48 Branchbran 90.0% 27 / 30 Conditioncond 69.2% 9 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 87.1% 95 / 109 CC 22 SCAR 21.3
CC
22
Cov
92%
_guess_content_type
12.0
file_response
9.4
prepare_headers
3.0
CRAP 8.4
« FastAPI/ResponseModel.pm ↑ Summary FastAPI/Cookies.pm »
1 package PAGI::FastAPI::Response::File;
2  
3 24 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 our $VERSION = qv('v1.7.3');
8 24 our $AUTHORITY = 'cpan:MANWAR';
9  
10 24 ▶use PAGI::FastAPI::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 our @EXPORT_OK = qw(file_response);
14  
15 # Built on PAGI::FastAPI::Response's documented public contract
16 # (status/body/headers + prepare_headers), the same extension point
17 # PAGI::FastAPI::Response::HTML uses.
18 #
19 # DELIBERATE SCOPE LIMIT: this reads the whole file into memory rather than
20 # streaming it in chunks. PAGI::FastAPI::Response::SSE achieves true
21 # streaming via a dispatch($scope,$receive,$send) method that talks to the
22 # raw PAGI protocol directly, but it does so through PAGI::SSE, part of
23 # the base PAGI framework distribution, whose response-side protocol
24 # internals this module doesn't rely on or assume. Reusing the well-defined
25 # Response base-class contract instead is the tradeoff: correct and safe
26 # for small/moderate files (templates, generated reports, small assets),
27 # not appropriate for huge files or video. A true chunked-streaming version
28 # is a reasonable future upgrade for whoever wants to build directly on the
29 # response-side PAGI protocol the way Response::SSE does.
30  
31 24 class PAGI::FastAPI::Response::File :isa(PAGI::FastAPI::Response) {
32 24 field $filename :param = undef;
33  
34 15 ▶ method prepare_headers ($c, $default_content_type = 'application/octet-stream') {
Subroutine prepare_headers: called CC=3 SCAR=11.0
Pod undocumented
35 15 $self->SUPER::prepare_headers($c, $default_content_type);
36 15 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 14 1 15
37 14 (my $safe_name = $filename) =~ s/"/\\"/g;
38 14 $c->set_header('content-disposition' => qq{attachment; filename="$safe_name"});
39 }
40 24 }
41 }
42  
43 # A plain sub with a signature must come AFTER a `class ... :isa(...)` block
44 # in this Perl version, see PAGI::FastAPI::Response::Redirect for the
45 # isolated reproduction of why.
46 #
47 # Reads $path entirely into memory and returns a File response. Guesses a
48 # small set of common content types from the extension; pass content_type
49 # explicitly for anything else.
50 17 ▶sub file_response ($path, %opts) {
Subroutine file_response: called CC=9 SCAR=22.4
Pod documented
51 17 ▶ die "file_response: '$path' does not exist or is not readable\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path and -r _ 1 16 17
Condition66%2/3
-e $path and -r _
!ll&&!rl&&r
1016
MC/DC50%1/2
-e $path and -r _
-e $path -r _
Truth table66%2/3
A and B
A= -e $pathB= -r _
ABresult
0X0
100
111
52 unless -e $path && -r _;
53  
54 16 ▶ open my $fh, '<:raw', $path or die "file_response: cannot open '$path': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "file_response: cannot open '${path}': $!\n" 16 0 16
55 16 local $/;
56 16 my $data = <$fh>;
57 16 close $fh;
58  
59 16 ▶ my $content_type = $opts{content_type} // _guess_content_type($path);
Condition66%2/3
$opts{"content_type"} // _guess_content_type($path)
l!l&&r!l&&!r
1150
MC/DC0%0/2
$opts{"content_type"} // _guess_content_type($path)
$opts{"content_type"} _guess_content_type($path)
Truth table66%2/3
A // B
A= $opts{"content_type"}B= _guess_content_type($path)
ABresult
1X1
011
000
60 16 ▶ my $filename = $opts{filename} // do { (my $n = $path) =~ s{.*/}{}; $n };
Condition66%2/3
$opts{"filename"} // do { (my $n = $path) =~ s[.*/][]u; $n }
l!l&&r!l&&!r
2140
MC/DC0%0/2
$opts{"filename"} // do { (my $n = $path) =~ s[.*/][]u; $n }
$opts{"filename"} do { (my $n = $path) =~ s[.*/][]u; $n }
Truth table66%2/3
A // B
A= $opts{"filename"}B= do { (my $n = $path) =~ s[.*/][]u; $n }
ABresult
1X1
011
000
61  
62 return PAGI::FastAPI::Response::File->new(
63 body => $data,
64 status => $opts{status} // 200,
65 headers => [ ['content-type', $content_type] ],
66 16 ▶ filename => (($opts{as_attachment} // 1) ? $filename : undef),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'as_attachment'} // 1 ? : 15 1 16
Condition75%3/4
$opts{"status"} // 200
l!l
016
$opts{"as_attachment"} // 1
l!l
115
MC/DC0%0/1
$opts{"status"} // 200
$opts{"status"}
MC/DC100%1/1
$opts{"as_attachment"} // 1
$opts{"as_attachment"}
Truth table50%1/2
A
A= $opts{"status"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"as_attachment"}
Aresult
11
00
67 );
68 }
69  
70 15 ▶sub _guess_content_type ($path) {
Subroutine _guess_content_type: called CC=12 SCAR=24.9
71 15 ▶ return 'text/css; charset=utf-8' if $path =~ /\.css$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.css$/iu 0 15 15
72 15 ▶ return 'application/javascript; charset=utf-8' if $path =~ /\.js$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.js$/iu 0 15 15
73 15 ▶ return 'text/plain; charset=utf-8' if $path =~ /\.te?xt$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.te?xt$/iu 1 14 15
74 14 ▶ return 'text/html; charset=utf-8' if $path =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.html?$/iu 1 13 14
75 13 ▶ return 'application/json' if $path =~ /\.json$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.json$/iu 1 12 13
76 12 ▶ return 'text/csv' if $path =~ /\.csv$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.csv$/iu 1 11 12
77 11 ▶ return 'application/pdf' if $path =~ /\.pdf$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.pdf$/iu 5 6 11
78 6 ▶ return 'image/png' if $path =~ /\.png$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.png$/iu 1 5 6
79 5 ▶ return 'image/jpeg' if $path =~ /\.jpe?g$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.jpe?g$/iu 2 3 5
80 3 ▶ return 'image/gif' if $path =~ /\.gif$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.gif$/iu 1 2 3
81 2 ▶ return 'image/svg+xml' if $path =~ /\.svg$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.svg$/iu 1 1 2
82 1 return 'application/octet-stream';
83 }
84  
85 =encoding utf-8
86  
87 =head1 NAME
88  
89 PAGI::FastAPI::Response::File - File Download Response for PAGI::FastAPI
90  
91 =head1 VERSION
92  
93 Version v1.7.3
94  
95 =head1 SYNOPSIS
96  
97 use PAGI::FastAPI::Response::File qw(file_response);
98  
99 $app->get('/reports/{id}',
100 handler => async sub ($c) {
101 my $path = "/var/reports/" . $c->path_param('id') . ".pdf";
102 return file_response($path);
103 }
104 );
105  
106 # Inline (view in browser) instead of "Save As" download prompt:
107 $app->get('/logo.png',
108 handler => async sub ($c) {
109 return file_response('/var/assets/logo.png', as_attachment => 0);
110 }
111 );
112  
113 # Or construct directly:
114 use PAGI::FastAPI::Response::File;
115  
116 my $res = PAGI::FastAPI::Response::File->new(
117 body => $binary_data,
118 headers => [ ['content-type', 'application/pdf'] ],
119 filename => 'report.pdf',
120 );
121  
122 =head1 FUNCTIONS
123  
124 =head2 C<file_response($path, %opts)>
125  
126 Exported on request. Reads C<$path> from disk and returns a ready-to-return
127 L<PAGI::FastAPI::Response::File> instance.
128  
129 =over 4
130  
131 =item * C<content_type> - (Optional) Overrides the guessed MIME type.
132 Recognises C<.txt>, C<.html>, C<.json>, C<.csv>, C<.pdf>, C<.png>, C<.jpg>/C<.jpeg>,
133 C<.gif>, C<.svg> by extension; falls back to C<application/octet-stream>.
134  
135 =item * C<filename> - (Optional) Overrides the C<Content-Disposition>
136 filename. Defaults to the basename of C<$path>.
137  
138 =item * C<as_attachment> - (Optional) Boolean, defaults to true. When true,
139 sends C<Content-Disposition: attachment; filename="...">, prompting a
140 download. Set to false for inline display (e.g. images shown directly in
141 the browser).
142  
143 =item * C<status> - (Optional) HTTP status code. Defaults to C<200>.
144  
145 =back
146  
147 Dies if C<$path> doesn't exist or isn't readable.
148  
149 =head1 METHODS
150  
151 =head2 C<new(%options)>
152  
153 =over 4
154  
155 =item * C<body> - (Required, inherited) The raw file content.
156  
157 =item * C<headers> - (Optional, inherited) ArrayRef of C<[name, value]> pairs;
158 set C<content-type> here.
159  
160 =item * C<filename> - (Optional) If set, adds a C<Content-Disposition:
161 attachment; filename="...">-header automatically.
162  
163 =item * C<status> - (Optional, inherited) Defaults to C<200>.
164  
165 =back
166  
167 =head1 SEE ALSO
168  
169 L<PAGI::FastAPI::Response>, L<PAGI::FastAPI::Response::HTML>, L<PAGI::FastAPI::Response::SSE>
170  
171 =head1 AUTHOR
172  
173 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
174  
175 =head1 REPOSITORY
176  
177 L<https://github.com/manwar/PAGI-FastAPI>
178  
179 =head1 BUGS
180  
181 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>.
182 I will be notified and then you'll automatically be notified of progress on your
183 bug as I make changes.
184  
185 =head1 SUPPORT
186  
187 You can find documentation for this module with the perldoc command.
188  
189 perldoc PAGI::FastAPI::Response::File
190  
191 You can also look for information at:
192  
193 =over 4
194  
195 =item * BUG Report
196  
197 L<https://github.com/manwar/PAGI-FastAPI/issues>
198  
199 =item * Search MetaCPAN
200  
201 L<https://metacpan.org/dist/PAGI-FastAPI/>
202  
203 =back
204  
205 =head1 LICENSE AND COPYRIGHT
206  
207 Copyright (C) 2026 Mohammad Sajid Anwar.
208  
209 This program is free software; you can redistribute it and/or modify it under
210 the terms of the Artistic License (2.0).
211  
212 =cut
213  
214 24 1; # End of PAGI::FastAPI::Response::File
« FastAPI/ResponseModel.pm ↑ Summary FastAPI/Cookies.pm »