Server/API/NoAPI.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 67 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 297 CC 20 SCAR 28.3
CC
20
Cov
0%
file_upload
42.0
_validate_filepath
30.0
file_list
20.0
CRAP 16.9
1 package WWW::Suffit::Server::API::NoAPI;
2 use strict;
3 use utf8;
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 WWW::Suffit::Server::API::NoAPI - The Suffit NoAPI controller
10  
11 =head1 SYNOPSIS
12  
13 use WWW::Suffit::Server::API::NoAPI;
14  
15 =head1 DESCRIPTION
16  
17 The Suffit NoAPI controller
18  
19 =head1 METHODS
20  
21 List of internal methods
22  
23 =head2 file_download
24  
25 See L</"GET /api/file/FILEPATH">
26  
27 =head2 file_list
28  
29 See L</"GET /api/file">
30  
31 =head2 file_remove
32  
33 See L</"DELETE /api/file/FILEPATH">
34  
35 =head2 file_upload
36  
37 See L</"PUT /api/file/FILEPATH">
38  
39 =head1 API METHODS
40  
41 List of API methods
42  
43 =head2 DELETE /api/file/FILEPATH
44  
45 Remove file from server
46  
47 # curl -v -H "Authorization: Bearer eyJh...GBew" \
48 -X DELETE \
49 https://localhost:8695/api/file/foo/bar/test123.txt
50  
51 > DELETE /api/file/foo/bar/test123.txt HTTP/1.1
52 > Host: localhost:8695
53 > User-Agent: curl/7.68.0
54 > Accept: */*
55 > Authorization: Bearer eyJh...GBew
56 >
57 < HTTP/1.1 200 OK
58 < Content-Length: 30
59 < Content-Type: application/json;charset=UTF-8
60 < Date: Wed, 14 Aug 2024 11:27:09 GMT
61 < Server: OWL/1.11
62 <
63 {
64 "code": "E0000",
65 "status": true
66 }
67  
68 =head2 GET /api/file
69  
70 Get a list of files on the server (manifest)
71  
72 # curl -v -H "Authorization: Bearer eyJh...s5aM" \
73 https://localhost:8695/api/file
74  
75 > GET /api/file HTTP/1.1
76 > Host: localhost:8695
77 > User-Agent: curl/7.68.0
78 > Accept: */*
79 > Authorization: Bearer eyJh...s5aM
80 >
81 < HTTP/1.1 200 OK
82 < Content-Length: 1081
83 < Content-Type: application/json;charset=UTF-8
84 < Date: Wed, 14 Aug 2024 11:17:42 GMT
85 < Server: OWL/1.11
86 < Vary: Accept-Encoding
87 <
88 {
89 "documentroot": "/home/foo/tmp/owl/public",
90 "manifest": [
91 {
92 "absf": "/",
93 "directory": "/",
94 "filename": "",
95 "id": 9962787,
96 "mdate": 1701365824,
97 "path": "/",
98 "perms": 509,
99 "pid": 0,
100 "size": 4096,
101 "type": "folder"
102 },
103 {
104 "absf": "/foo",
105 "directory": "/",
106 "filename": "foo",
107 "id": 9962956,
108 "mdate": 1690565423,
109 "path": "foo",
110 "perms": 509,
111 "pid": 9962787,
112 "size": 4096,
113 "type": "folder"
114 },
115 {
116 "absf": "/foo/bar",
117 "directory": "/foo",
118 "filename": "bar",
119 "id": 9962957,
120 "mdate": 1723634001,
121 "path": "foo/bar",
122 "perms": 509,
123 "pid": 9962956,
124 "size": 4096,
125 "type": "folder"
126 },
127 {
128 "absf": "/foo/bar/test123.txt",
129 "directory": "/foo/bar",
130 "filename": "test123.txt",
131 "id": 9963558,
132 "mdate": 1723634001,
133 "path": "foo/bar/test123.txt",
134 "perms": 436,
135 "pid": 9962957,
136 "size": 490,
137 "type": "file"
138 },
139 ],
140 "status": true
141 }
142  
143 =head2 GET /api/file/FILEPATH
144  
145 Download file from server
146  
147 # curl -v -H "Authorization: Bearer eyJh...GBew" \
148 https://localhost:8695/api/file/foo/bar/test123.txt
149  
150 > GET /api/file/foo/bar/test123.txt HTTP/1.1
151 > Host: localhost:8695
152 > User-Agent: curl/7.68.0
153 > Accept: */*
154 > Authorization: Bearer eyJh...GBew
155 >
156 < HTTP/1.1 200 OK
157 < Accept-Ranges: bytes
158 < Content-Length: 490
159 < Content-Type: text/plain;charset=UTF-8
160 < Date: Wed, 14 Aug 2024 11:24:32 GMT
161 < ETag: "5946bf06113340241d59cbf2c940eede"
162 < Last-Modified: Wed, 14 Aug 2024 11:13:21 GMT
163 < Server: OWL/1.11
164 <
165 ...content of the file...
166  
167 =head2 PUT /api/file/FILEPATH
168  
169 Upload file to server
170  
171 # curl -v -H "Authorization: Bearer eyJh...GBew" \
172 -X PUT -F size=490 \
173 -F md5=acaa9dd36fe3b6758e91129170910378 -F fileraw=@test123.txt \
174 https://localhost:8695/api/file/foo/bar/test123.txt
175  
176 > PUT /api/file/foo/bar/test123.txt HTTP/1.1
177 > Host: localhost:8695
178 > User-Agent: curl/7.68.0
179 > Accept: */*
180 > Authorization: Bearer eyJh...GBew
181 > Content-Length: 902
182 > Content-Type: multipart/form-data; boundary=...
183 >
184 < HTTP/1.1 200 OK
185 < Content-Length: 182
186 < Content-Type: application/json;charset=UTF-8
187 < Date: Wed, 14 Aug 2024 11:13:21 GMT
188 < Server: OWL/1.11
189 <
190 {
191 "code": "E0000",
192 "file": "/home/foo/tmp/owl/public/foo/bar/test123.txt",
193 "md5": "acaa9dd36fe3b6758e91129170910378",
194 "size": "490",
195 "status": true,
196 "uploaded": "2024-08-14T11:13:21Z"
197 }
198  
199 =head2 ERROR CODES
200  
201 List of NoAPI Suffit API error codes
202  
203 API | HTTP | DESCRIPTION
204 -------+-------+-------------------------------------------------
205 E1110 [400] No file path specified
206 E1111 [400] Incorrect file path
207 E1112 [404] File not found
208 E1113 [500] Can't upload file
209 E1114 [500] Can't upload file: file is lost
210 E1115 [400] File size mismatch
211 E1116 [400] File md5 checksum mismatch
212 E1117 [---] Reserved
213 E1118 [---] Reserved
214 E1119 [---] Reserved
215  
216 B<*> -- this code will be defined later on the interface side
217  
218 See also list of common Suffit API error codes in L<WWW::Suffit::API/"ERROR CODES">
219  
220 =head1 HISTORY
221  
222 See C<Changes> file
223  
224 =head1 TO DO
225  
226 See C<TODO> file
227  
228 =head1 SEE ALSO
229  
230 L<Mojolicious>, L<WWW::Suffit>, L<WWW::Suffit::Server>, L<WWW::Suffit::API>
231  
232 =head1 AUTHOR
233  
234 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
235  
236 =head1 COPYRIGHT
237  
238 Copyright (C) 1998-2026 D&D Corporation
239  
240 =head1 LICENSE
241  
242 This program is distributed under the terms of the Artistic License Version 2.0
243  
244 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
245  
246 =cut
247  
248 use Mojo::Base 'Mojolicious::Controller';
249  
250 use File::Find qw/find/;
251 use File::stat;
252  
253 use Mojo::JSON qw / true false /;
254 use Mojo::File qw/path/;
255 use Mojo::Util qw/decode/;
256 use Mojo::Date;
257  
258 use WWW::Suffit::Util qw/ md5sum /;
259 use WWW::Suffit::Const qw/ USERNAME_REGEXP /;
260  
261 sub file_list {
262 my $self = shift;
263 my $src = $self->app->documentroot;
264 my $root = stat($src);
265  
266 my @manifest = ({
267 id => $root->ino,
268 pid => 0,
269 filename => '',
270 directory => '/',
271 path => '/',
272 perms => $root->mode & 07777,
273 size => $root->size,
274 mdate => $root->mtime,
275 type => 'folder',
276 absf => '/',
277 });
278 find({ follow => 1,
279 follow_skip => 2,
280 wanted => sub
281 {
282 my $fn = $_;
283 my $dir = $File::Find::dir;
284 return 0 if $fn =~ /^\./;
285 return 0 unless -f $fn or -d $fn;
286 my $type = -d $fn ? 'folder' : 'file';
287 my $sb = stat($fn);
288 my $dirf = path($dir)->to_rel($src)->to_string; # Directory (rel)
289 $dirf =~ s/^\.*\/*/\//;
290 my $absf = path("/", $dirf, $fn)->to_string; # File path (abs)
291 my $relf = path($dirf, $fn)->to_string; # File path (rel)
292 $relf =~ s/^\.*\/+//;
293  
294 # Out
295 push @manifest, {
296 id => $sb->ino,
297 pid => stat($dir)->ino,
298 filename => decode('UTF-8', $fn) // '',
299 directory => decode('UTF-8', $dirf) // '',
300 path => decode('UTF-8', $relf) // '',
301 perms => $sb->mode & 07777,
302 size => $sb->size,
303 mdate => $sb->mtime,
304 type => $type,
305 absf => decode('UTF-8', $absf) // '',
306 };
307 }}, $src);
308  
309 # Correct manifest
310 my $unfl = _unflatten(\@manifest);
311 my $fl = _flatten($unfl);
312  
313 return $self->render(json => {
314 status => true,
315 documentroot => $src,
316 manifest => $fl,
317 });
318 }
319 sub file_download {
320 my $self = shift;
321 my $filepath = $self->param('filepath') // '';
322 return 1 if $self->_validate_filepath($filepath); # Validate filepath
323  
324 # Set paths
325 my $path = path($self->app->documentroot, $filepath);
326  
327 # Get full file name
328 my $fullfilename = $path->to_string;
329  
330 # Check file
331 return $self->reply->json_error(404 => "E1112: File not found: \"$filepath\"")
332 unless (-e $fullfilename and -f $fullfilename and -s $fullfilename);
333  
334 # Send file
335 return $self->reply->static($filepath)
336 }
337 sub file_upload {
338 my $self = shift;
339 my $filepath = $self->param('filepath') // '';
340 return 1 if $self->_validate_filepath($filepath); # Validate filepath
341  
342 # Set paths
343 my $path_file = path($self->app->documentroot, $filepath);
344 my $path_dir = $path_file->dirname->make_path;
345 my $path_name = $path_file->to_string;
346  
347 # Upload file @fileraw@
348 my $fileuploaded = $self->req->upload('fileraw');
349 if ($fileuploaded) {
350 my $size = $fileuploaded->size;
351 my $name = $fileuploaded->filename;
352 $fileuploaded->move_to($path_name);
353 my $md5 = md5sum($path_name);
354  
355 # Check uploaded file
356 return $self->reply->json_error(500 => "E1114: Can't upload file \"$filepath\": file is lost")
357 unless -e $path_name;
358  
359 # Check size
360 my $expected_size = $self->param("size") || 0;
361 if ($expected_size && $expected_size != $size) {
362 $path_file->remove;
363 return $self->reply->json_error(400 =>
364 sprintf("E1115: File size mismatch: expected=\"%s\"; got=\"%s\"", $expected_size, $size));
365 }
366  
367 # Check md5sum
368 my $expected_md5 = $self->param("md5") // '';
369 if (length($expected_md5) && $expected_md5 ne $md5) {
370 $path_file->remove;
371 return $self->reply->json_error(400 =>
372 sprintf("E1116: File md5 checksum mismatch: expected=\"%s\"; got=\"%s\"", $expected_md5, $md5));
373 }
374 } else {
375 return $self->reply->json_error(500 => "E1113: Can't upload file \"$filepath\"");
376 }
377  
378 # Result
379 return $self->reply->json_ok({
380 file => $path_name,
381 size => $self->param("size"),
382 md5 => $self->param("md5"),
383 uploaded => Mojo::Date->new(time)->to_datetime, # RFC 3339
384 });
385 }
386 sub file_remove {
387 my $self = shift;
388 my $filepath = $self->param('filepath') // '';
389 return 1 if $self->_validate_filepath($filepath); # Validate filepath
390  
391 # Set paths
392 my $path = path($self->app->documentroot, $filepath);
393  
394 # Get full file name
395 my $fullfilename = $path->to_string;
396  
397 # Check file
398 return $self->reply->json_error(404 => "E1112: File not found: \"$filepath\"")
399 unless (-e $fullfilename and -f $fullfilename and -s $fullfilename);
400  
401 # Delete file
402 $path->remove;
403  
404 # Result
405 return $self->reply->json_ok;
406 }
407  
408 sub _unflatten {
409 my $arr = shift // [];
410 my $parent = shift // {id => 0};
411 my $tree = shift // [];
412  
413 my @children = grep { $_->{pid} == $parent->{id} } @$arr;
414 return $tree unless scalar @children;
415  
416 if ($parent->{id} == 0) {
417 $tree = [@children];
418 } else {
419 $parent->{items} = [@children];
420 }
421  
422 foreach my $child (@children) {
423 &_unflatten($arr, $child);
424 }
425  
426 return $tree;
427 }
428 sub _flatten {
429 my $tree = shift // [];
430 my $arr = [];
431  
432 foreach my $child (sort { $b->{type} cmp $a->{type} || $a->{filename} cmp $b->{filename} } @$tree) {
433 if (exists $child->{items}) {
434 my $children = &_flatten($child->{items});
435 delete $child->{items};
436 push @$arr, $child;
437 push @$arr, @$children;
438 } else {
439 push @$arr, $child
440 }
441 }
442  
443 return $arr;
444 }
445 sub _validate_filepath { # Returns true if invalid!
446 my $self = shift;
447 my $filepath = shift // $self->param('filepath') // '';
448 my @test = split /\//, $filepath;
449 return $self->reply->json_error(400 => "E1110: No file path specified") unless scalar @test;
450 my $status = 1;
451 foreach my $d (@test) {
452 unless (defined($d) && length($d) && (length($d) <= 64) && $d =~ USERNAME_REGEXP) {
453 $status = 0;
454 last;
455 }
456 if (($d =~ /\.{2,}/) || ($d =~ /^[~]/)) {
457 $status = 0;
458 last;
459 }
460 }
461 return $self->reply->json_error(400 => "E1111: Incorrect file path") unless $status;
462 return;
463 }
464  
465 1;
466  
467 __END__