blib/lib/App/BlurFill/Web.pm

filter: Statementstmt 80.3% 45 / 56 Branchbran 42.8% 6 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 70.7% 63 / 89 CC 11 SCAR 18.9
CC
11
Cov
70%
__ANON__
11.8
__ANON__
7.5
BEGIN
1.0
CRAP 6.6
↑ Summary
1 =head1 NAME
2  
3 App::BlurFill::Web - The web interface to App::BlurFill
4  
5 =head1 SYNOPSIS
6  
7 # In a PSGI environment
8 use App::BlurFill::Web;
9  
10 App::BlurFill::Web->to_app;
11  
12 =head1 DESCRIPTION
13  
14 App::BlurFill::Web is a web interface for the App::BlurFill module. It allows users
15 to upload an image file, specify the desired width and height, and receive a blurred
16 image file in response.
17  
18 =head1 ROUTES
19  
20 =head2 GET /
21  
22 This route displays a web form where users can upload an image and specify
23 the desired width and height for the output. The form submits to the POST /blur
24 route.
25  
26 =head2 POST /blur
27  
28 This route accepts an image file upload and optional width and height parameters.
29 It processes the image and returns an HTML page displaying the blurred image with
30 a download link and an option to create another image.
31  
32 =head2 GET /download/:filename
33  
34 This route serves the processed image file for download. The filename parameter
35 should match a previously processed image stored in the temporary directory.
36  
37 =head1 PARAMETERS
38  
39 =head2 image
40  
41 The image file to be processed. This parameter is required.
42 It should be a valid image file format (e.g., JPEG, PNG, GIF).
43  
44 =head2 width
45  
46 The desired width of the output image. Default is 650 pixels.
47  
48 =head2 height
49  
50 The desired height of the output image. Default is 350 pixels.
51  
52 =head1 EXAMPLE
53  
54 POST /blur
55 Content-Type: multipart/form-data
56  
57 image: <binary image data>
58 width: 800
59 height: 600
60  
61 =head2 Using C<curl>
62  
63 # This will return HTML with the results page
64 curl -X POST -F "image=@path/to/image.jpg" -F "width=800" -F "height=600" http://localhost:3000/blur
65
66 # To download the image directly
67 curl -OJ http://localhost:3000/download/image_blur.png
68  
69 =head1 RESPONSE
70  
71 The POST /blur response will be an HTML page displaying the blurred image with
72 download options. The GET /download/:filename response will be the actual image file.
73  
74 =cut
75  
76 2 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 package App::BlurFill::Web;
79 2 ▶use Dancer2;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 2 our $VERSION = '0.1.0';
82  
83 2 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use App::BlurFill;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # Create a persistent temp directory for storing processed images
89 2 my $TEMP_DIR = File::Temp::tempdir(CLEANUP => 1);
90  
91 1 ▶sub _get_css {
Subroutine _get_css: called CC=1 SCAR=0.0
92 1 return <<'CSS';
93 * {
94 margin: 0;
95 padding: 0;
96 box-sizing: border-box;
97 }
98
99 body {
100 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
101 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
102 min-height: 100vh;
103 display: flex;
104 align-items: center;
105 justify-content: center;
106 padding: 20px;
107 }
108
109 .container {
110 background: white;
111 border-radius: 16px;
112 box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
113 max-width: 600px;
114 width: 100%;
115 padding: 40px;
116 }
117
118 h1 {
119 color: #333;
120 font-size: 32px;
121 font-weight: 700;
122 margin-bottom: 12px;
123 text-align: center;
124 }
125
126 .subtitle {
127 color: #666;
128 font-size: 16px;
129 text-align: center;
130 margin-bottom: 32px;
131 line-height: 1.5;
132 }
133
134 .form-group {
135 margin-bottom: 24px;
136 }
137
138 label {
139 display: block;
140 color: #444;
141 font-weight: 600;
142 margin-bottom: 8px;
143 font-size: 14px;
144 }
145
146 input[type="file"] {
147 display: block;
148 width: 100%;
149 padding: 12px;
150 border: 2px dashed #667eea;
151 border-radius: 8px;
152 background: #f8f9ff;
153 cursor: pointer;
154 transition: all 0.3s ease;
155 }
156
157 input[type="file"]:hover {
158 border-color: #764ba2;
159 background: #f0f1ff;
160 }
161
162 input[type="number"] {
163 width: 100%;
164 padding: 12px 16px;
165 border: 2px solid #e0e0e0;
166 border-radius: 8px;
167 font-size: 16px;
168 transition: all 0.3s ease;
169 }
170
171 input[type="number"]:focus {
172 outline: none;
173 border-color: #667eea;
174 box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
175 }
176
177 .dimensions {
178 display: grid;
179 grid-template-columns: 1fr 1fr;
180 gap: 16px;
181 }
182
183 button, .button {
184 width: 100%;
185 padding: 16px;
186 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
187 color: white;
188 border: none;
189 border-radius: 8px;
190 font-size: 18px;
191 font-weight: 600;
192 cursor: pointer;
193 transition: all 0.3s ease;
194 box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
195 text-decoration: none;
196 display: inline-block;
197 text-align: center;
198 }
199
200 button:hover, .button:hover {
201 transform: translateY(-2px);
202 box-shadow: 0 6px 16px rgba(102, 126, 234, 0.5);
203 }
204
205 button:active, .button:active {
206 transform: translateY(0);
207 }
208
209 .info {
210 background: #f8f9ff;
211 border-left: 4px solid #667eea;
212 padding: 16px;
213 margin-top: 24px;
214 border-radius: 4px;
215 }
216
217 .info p {
218 color: #555;
219 font-size: 14px;
220 line-height: 1.6;
221 margin-bottom: 8px;
222 }
223
224 .info p:last-child {
225 margin-bottom: 0;
226 }
227
228 .info strong {
229 color: #333;
230 }
231  
232 .credits {
233 padding-top: 1em;
234 color: #999;
235 text-align: center;
236 }
237  
238 .credits a:link {
239 color: #c9c;
240 }
241  
242 .credits a:visited {
243 color: #969;
244 }
245  
246 .result-image {
247 margin: 24px 0;
248 text-align: center;
249 border-radius: 8px;
250 overflow: hidden;
251 box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
252 }
253  
254 .result-image img {
255 max-width: 100%;
256 height: auto;
257 display: block;
258 }
259  
260 .success-message {
261 background: #f0fdf4;
262 border-left: 4px solid #10b981;
263 padding: 16px;
264 margin-bottom: 24px;
265 border-radius: 4px;
266 color: #065f46;
267 }
268  
269 .action-buttons {
270 display: grid;
271 grid-template-columns: 1fr 1fr;
272 gap: 16px;
273 margin-top: 24px;
274 }
275  
276 .button-secondary {
277 background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);
278 }
279
280 @media (max-width: 640px) {
281 .container {
282 padding: 24px;
283 }
284
285 h1 {
286 font-size: 24px;
287 }
288
289 .dimensions, .action-buttons {
290 grid-template-columns: 1fr;
291 }
292 }
293 CSS
294 }
295  
296 1 ▶get '/' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
297 1 my $css = _get_css();
298 1 return <<"HTML";
299 <!DOCTYPE html>
300 <html lang="en">
301 <head>
302 <meta charset="UTF-8">
303 <meta name="viewport" content="width=device-width, initial-scale=1.0">
304 <title>BlurFill - Perfect crops, zero letterboxing: smart blur-fill from your source image.</title>
305 <style>
306 $css
307 </style>
308 </head>
309 <body>
310 <div class="container">
311 <h1>BlurFill</h1>
312 <p class="subtitle">Perfect crops, zero letterboxing: smart blur-fill from your source image.</p>
313
314 <form action="/blur" method="POST" enctype="multipart/form-data">
315 <div class="form-group">
316 <label for="image">Select Image</label>
317 <input type="file" id="image" name="image" accept="image/jpeg,image/jpg,image/png,image/gif" required>
318 </div>
319
320 <div class="form-group">
321 <label>Output Dimensions</label>
322 <div class="dimensions">
323 <div>
324 <label for="width">Width (px)</label>
325 <input type="number" id="width" name="width" value="650" min="1" max="4000">
326 </div>
327 <div>
328 <label for="height">Height (px)</label>
329 <input type="number" id="height" name="height" value="350" min="1" max="4000">
330 </div>
331 </div>
332 </div>
333
334 <button type="submit">Generate resized image</button>
335 </form>
336
337 <div class="info">
338 <p><strong>How it works:</strong></p>
339 <p>1. Upload your image (JPEG, PNG, or GIF)</p>
340 <p>2. Set your desired output dimensions</p>
341 <p>3. Click "Generate" to create a resized image with your source image centered and filled</p>
342 <p>4. Your processed image will be displayed with a download link</p>
343 </div>
344 <div class="credits">
345 Version $VERSION /
346 Made by <a href="https://links.davecross.co.uk/">Dave Cross</a> /
347 Code <a href="https://github.com/davorg-cpan/app-blurfill-web">on GitHub</a>
348 </div>
349 </div>
350 </body>
351 </html>
352 HTML
353 2 };
354  
355 1 ▶post '/blur' => sub {
Subroutine __ANON__: called CC=8 SCAR=24.7
356 1 ▶ my $upload = upload('image')
Branch: 1 branch
BranchTrueFalseTotal
my $upload = upload("image") or return status(400, {"error", "Missing image file"}) 1 0 1
357 or return status 400, { error => 'Missing image file' };
358  
359 1 my $orig_name = $upload->filename;
360 1 my ($name, $path, $ext) =
361 File::Basename::fileparse($orig_name, qr/\.[^.]*$/);
362  
363 1 ▶ return status 400, { error => 'Uploaded file must have a file extension' }
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 0 1 1
364 unless $ext;
365  
366 1 my $format = lc $ext;
367 1 $format =~ s/^\.//;
368  
369 1 my %mime = (
370 jpg => 'image/jpeg',
371 jpeg => 'image/jpeg',
372 png => 'image/png',
373 gif => 'image/gif',
374 );
375  
376 return status 400, { error => "Unsupported file format: .$format" }
377 1 ▶ unless exists $mime{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mime{$format} 0 1 1
378  
379 1 ▶ my $width = query_parameters->get('width') || 650;
Condition50%1/2
query_parameters()->get('width') || 650
l!l
01
MC/DC0%0/1
query_parameters()->get('width') || 650
query_parameters()->get('width')
Truth table50%1/2
A
A= query_parameters()->get('width')
Aresult
11
00
380 1 ▶ my $height = query_parameters->get('height') || 350;
Condition50%1/2
query_parameters()->get('height') || 350
l!l
01
MC/DC0%0/1
query_parameters()->get('height') || 350
query_parameters()->get('height')
Truth table50%1/2
A
A= query_parameters()->get('height')
Aresult
11
00
381  
382 1 my $in_dir = File::Temp::tempdir;
383 1 my $in_path = "$in_dir/$name$ext";
384 1 $upload->copy_to($in_path);
385  
386 1 my $outfile;
387 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $blur = "App::BlurFill"->new("file", $in_path, "width", $width, "height", $height); $outfile = $blur->process } } or return status(500, {"error", "Processing failed: $@"}) 0 1 1
388 1 my $blur = App::BlurFill->new(
389 file => $in_path,
390 width => $width,
391 height => $height,
392 );
393 0 $outfile = $blur->process;
394 } or return status 500, { error => "Processing failed: $@" };
395  
396 0 my ($out_name) = File::Basename::fileparse($outfile);
397
398 # Copy the processed file to our persistent temp directory
399 0 my $persistent_path = File::Spec->catfile($TEMP_DIR, $out_name);
400 0 ▶ File::Copy::copy($outfile, $persistent_path) or die "Copy failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($outfile, $persistent_path) or die "Copy failed: $!" 0 0 0
401  
402 # Display results page with image preview and download link
403 0 my $css = _get_css();
404 0 return <<"HTML";
405 <!DOCTYPE html>
406 <html lang="en">
407 <head>
408 <meta charset="UTF-8">
409 <meta name="viewport" content="width=device-width, initial-scale=1.0">
410 <title>BlurFill - Result</title>
411 <style>
412 $css
413 </style>
414 </head>
415 <body>
416 <div class="container">
417 <h1>BlurFill</h1>
418 <p class="subtitle">Your resized image is ready!</p>
419
420 <div class="success-message">
421 <strong>✓ Success!</strong> Your image has been processed successfully.
422 </div>
423
424 <div class="result-image">
425 <img src="/download/$out_name" alt="Resized image preview">
426 </div>
427
428 <div class="action-buttons">
429 <a href="/download/$out_name" class="button" download>Download image</a>
430 <a href="/" class="button button-secondary">Create another</a>
431 </div>
432
433 <div class="info">
434 <p><strong>What's next?</strong></p>
435 <p>• Click "Download image" to save your resized background</p>
436 <p>• Click "Create another" to process a new image</p>
437 </div>
438 </div>
439 </body>
440 </html>
441 HTML
442 2 };
443  
444 1 ▶get '/download/:filename' => sub {
Subroutine __ANON__: called CC=4 SCAR=20.1
445 1 my $filename = route_parameters->get('filename');
446
447 # Security: only allow filenames without path traversal
448 1 ▶ return status 400, { error => 'Invalid filename' }
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ m([/\\])u 0 1 1
449 if $filename =~ m{[/\\]};
450
451 1 my $filepath = File::Spec->catfile($TEMP_DIR, $filename);
452
453 1 ▶ return status 404, { error => 'File not found' }
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filepath 1 0 1
454 unless -f $filepath;
455
456 # Determine content type from extension
457 0 my $ext = lc($filename);
458 0 $ext =~ s/.*\.//;
459
460 0 my %mime = (
461 jpg => 'image/jpeg',
462 jpeg => 'image/jpeg',
463 png => 'image/png',
464 gif => 'image/gif',
465 );
466
467 0 ▶ my $content_type = $mime{$ext} || 'application/octet-stream';
Condition0%0/2
$mime{$ext} || 'application/octet-stream'
l!l
00
MC/DC0%0/1
$mime{$ext} || 'application/octet-stream'
$mime{$ext}
Truth table0%0/2
A
A= $mime{$ext}
Aresult
11
00
468
469 0 send_file(
470 $filepath,
471 system_path => 1,
472 content_type => $content_type,
473 );
474 2 };
475  
476 =head1 AUTHOR
477  
478 Dave Cross <dave@perlhacks.com>
479  
480 =head1 COPYRIGHT AND LICENSE
481  
482 This software is copyright (c) 2025, Magnum Solutions Ltd. All rights reserved.
483  
484 This is free software; you can redistribute it and/or modify it under the
485 same terms as the Perl 5 programming language system itself.
486  
487 =cut
↑ Summary