FilePump bug fixes
Restricted file uris to uploads and extras dirs Validation messages for invalid file uris Updated i18n Added more tests
This commit is contained in:
parent
b149367c11
commit
7110febecd
4 changed files with 188 additions and 24 deletions
|
|
@ -13,6 +13,7 @@ package WebGUI::Macro::FilePump;
|
|||
use strict;
|
||||
use WebGUI::FilePump::Bundle;
|
||||
use Path::Class;
|
||||
use WebGUI::International;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
|
|
@ -39,7 +40,8 @@ $bundleName, the name of a File Pump bundle.
|
|||
|
||||
=item *
|
||||
|
||||
$type, the type of files from the Bundle that you are accessing. Either JS or javascript, or CSS or css.
|
||||
$type, the type of files from the Bundle that you are accessing.
|
||||
Either JS or javascript, or CSS or css (case-insensitive).
|
||||
|
||||
=back
|
||||
|
||||
|
|
@ -62,15 +64,39 @@ sub process {
|
|||
|
||||
my $bundle = WebGUI::FilePump::Bundle->new($session, $bundleId->[0]);
|
||||
return '' unless $bundle;
|
||||
|
||||
my $bundleDir = $bundle->getPathClassDir;
|
||||
|
||||
# Sometimes, when migrating sites, restoring from backup, etc., you can
|
||||
# get into the situation where the bundle is defined in the db but doesn't
|
||||
# exist on the filesystem. In this case, simply generate a warning and
|
||||
# trigger a bundle rebuild.
|
||||
if (!-e $bundleDir) {
|
||||
$session->log->warn("Bundle is marked as built, but does not exist on filesystem. Attempting to rebuild: $bundleDir");
|
||||
my ($code, $error) = $bundle->build;
|
||||
if ($error) {
|
||||
my $i18n = WebGUI::International->new($session, 'FilePump');
|
||||
$error = sprintf $i18n->get('build error'), $error;
|
||||
$session->log->error("Rebuild failed with error: $error");
|
||||
return $error;
|
||||
} else {
|
||||
$session->log->warn("Rebuild succeeded, continuing with macro processing");
|
||||
}
|
||||
}
|
||||
|
||||
my $uploadsDir = Path::Class::Dir->new($session->config->get('uploadsPath'));
|
||||
my $extrasDir = Path::Class::Dir->new($session->config->get('extrasPath'));
|
||||
my $uploadsUrl = Path::Class::Dir->new($session->config->get('uploadsURL'));
|
||||
my $extrasUrl = Path::Class::Dir->new($session->config->get('extrasURL'));
|
||||
|
||||
##Normal mode
|
||||
if (! $session->var->isAdminOn) {
|
||||
# Built files live at /path/to/uploads/filepump/bundle.timestamp/ which is
|
||||
# a sub-dir of uploadsDir, so resolve the dir relative to uploads
|
||||
my $dir = $bundle->getPathClassDir->relative($uploadsDir);
|
||||
if ($type eq 'js' || $type eq 'javascript') {
|
||||
my $file = $uploadsUrl->file($dir, $bundle->bundleUrl . '.js');
|
||||
return sprintf qq|<script type="text/javascript" src="%s">\n|, $file->stringify;
|
||||
return sprintf qq|<script type="text/javascript" src="%s"></script>\n|, $file->stringify;
|
||||
}
|
||||
elsif ($type eq 'css') {
|
||||
my $file = $uploadsUrl->file($dir, $bundle->bundleUrl . '.css');
|
||||
|
|
@ -85,7 +111,7 @@ sub process {
|
|||
my $template;
|
||||
my $files;
|
||||
if ($type eq 'js' || $type eq 'javascript') {
|
||||
$template = qq|<script type="text/javascript" src="%s">\n|;
|
||||
$template = qq|<script type="text/javascript" src="%s"></script>\n|;
|
||||
$files = $bundle->get('jsFiles');
|
||||
}
|
||||
elsif ($type eq 'css') {
|
||||
|
|
@ -103,10 +129,19 @@ sub process {
|
|||
$url = $uri->opaque;
|
||||
}
|
||||
elsif ($scheme eq 'file') {
|
||||
my $file = Path::Class::File->new($uri->path);
|
||||
my $uploadsRelFile = $file->relative($uploadsDir);
|
||||
$url = $uploadsUrl->file($uploadsRelFile)->stringify;
|
||||
|
||||
my $file = $bundle->resolveFilePath($uri->path);
|
||||
|
||||
# Un-built files live inside either uploads or extras
|
||||
if ($uploadsDir->subsumes($file)) {
|
||||
my $relFile = $file->relative($uploadsDir);
|
||||
$url = $uploadsUrl->file($relFile)->stringify;
|
||||
} elsif ($extrasDir->subsumes($file)) {
|
||||
my $relFile = $file->relative($extrasDir);
|
||||
$url = $extrasUrl->file($relFile)->stringify;
|
||||
} else {
|
||||
$session->log->warn("Invalid file: $file");
|
||||
next;
|
||||
}
|
||||
}
|
||||
elsif ($scheme eq 'http' or $scheme eq 'https') {
|
||||
$url = $uri->as_string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue