forked from openwebwork/renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticFiles.pm
More file actions
49 lines (38 loc) · 1.46 KB
/
StaticFiles.pm
File metadata and controls
49 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Renderer::Controller::StaticFiles;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use Mojo::File qw(path);
use File::Spec::Functions qw(canonpath);
sub path_is_subdir ($path, $dir) {
return 0 unless $path =~ /^\//;
$path = canonpath($path);
return 0 if $path =~ m#(^\.\.$|^\.\./|/\.\./|/\.\.$)#;
$dir = canonpath($dir);
return 0 unless $path =~ m|^$dir|;
return 1;
}
sub reply_with_file_if_readable ($c, $directory, $file) {
my $filePath = $directory->child($file);
if (-r $filePath && path_is_subdir($filePath, $directory)) {
return $c->reply->file($filePath);
} else {
return $c->render(data => 'File not found', status => 404);
}
}
# Route requests for pg_files/CAPA_Graphics to render root Contrib/CAPA/CAPA_Graphics
sub CAPA_graphics_file ($c) {
return $c->reply_with_file_if_readable($c->app->home->child('Contrib/CAPA/CAPA_Graphics'), $c->stash('static'));
}
# Route requests for pg_files to the render root tmp. The
# only requests should be for files in the temporary directory.
# FIXME: Perhaps this directory should be configurable.
sub temp_file ($c) {
return $c->reply_with_file_if_readable($c->app->home->child('tmp'), $c->stash('static'));
}
# Route request to pg_files to lib/PG/htdocs.
sub pg_file ($c) {
return $c->reply_with_file_if_readable(path($ENV{PG_ROOT}, 'htdocs'), $c->stash('static'));
}
sub public_file ($c) {
return $c->reply_with_file_if_readable($c->app->home->child('public'), $c->stash('static'));
}
1;