package MyApp::Controller::Browser::Example;
use Moose;
BEGIN { extends 'Catalyst::Controller::DirectoryDispatch' }
__PACKAGE__->config(
action => { setup => { Chained => '/browser/base', PathPart => 'mydir' } },
root => '/home/andy',
filter => qr{^\.|.conf$},
data_root => 'data',
full_paths => 1,
);
Provides a simple configuration based controller for listing local system directories and dispatching them as URLs.
If you created the controller at http://localhost/mydir and set root to '/home/user1' then browsing to the controller might give the following output:
{
"success":true,
"data":[
"file1",
"file2",
"dir1",
"dir2"
],
}
You could then point your browser to http://localhost/mydir/dir1 to get a directory listing of the folder '/home/user1/dir1' and so on...
The default view for DirectoryDispatch serializes the file list as JSON but it's easy to change it to whatever view you'd like.
__PACKAGE__->config(
'default' => 'text/html',
'map' => {
'text/html' => [ 'View', 'TT' ],
}
);
Then in your template...
[% FOREACH node IN response.data %]
[% node %]
[% END %]
If you need to process the files in anyway before they're passed to the view you can override process_files in your controller.
sub process_files {
my ($self, $c, $files) = @_;
foreach my $file ( @$files ) {
# Modify $file
}
return $files;
}
This is the last thing that happens before the list of files are passed on to the view. $files is sent in as an ArrayRef[Str] but you are free to return any thing you want as long as the serializer you're using can handle it.
is: ro, isa: Str
The folder that will be listed when accessing the controller (default '/').
is: ro, isa: RegexpRef
A regular expression that will remove matching files or folders from the directory listing (default: undef).
is: ro, isa: Str
The name of the key inside $c->stash->{response} where the directory listing will be stored (default: data).
is: ro, isa: Bool
Returns full paths for the directory listing rather than just the names (default: 0).
The design for this module was heavly influenced by the fantastic Catalyst::Controller::DBIC::API.