Catalyst-Controller-DirectoryDispatch

 view release on metacpan or  search on metacpan

lib/Catalyst/Controller/DirectoryDispatch.pm  view on Meta::CPAN

package Catalyst::Controller::DirectoryDispatch;
# ABSTRACT: Simple directory listing with built in url dispatching
$Catalyst::Controller::DirectoryDispatch::VERSION = '1.03';
use Moose;
BEGIN { extends 'Catalyst::Controller' }

use JSON;
use Try::Tiny;
use namespace::autoclean;

__PACKAGE__->config(
    'default'   => 'application/json',
    'stash_key' => 'response',
    'map'       => {
        'application/x-www-form-urlencoded' => 'JSON',
        'application/json'                  => 'JSON',
    }
);

has 'root' => (
    is      => 'ro',
    isa     => 'Str',
    default => '/',
);

has 'full_paths' => (
    is      => 'ro',
    isa     => 'Bool',
    default => 0,
);

has 'filter' => (
    is      => 'ro',
    isa     => 'RegexpRef',
);

has 'data_root' => (
    is      => 'ro',
    isa     => 'Str',
    default => 'data',
);

sub setup :Chained('specify.in.subclass.config') :CaptureArgs(0) :PathPart('specify.in.subclass.config') {}

sub list :Chained('setup') :PathPart('') :Args {
    my ( $self, $c, @dirpath ) = @_;

    my $path = join '/', @dirpath;
    $path = "/$path" if ($path);
    my $full_path = $self->root . $path;

    my @files = ();
    try {
        opendir (my $dir, $full_path) or die;
        @files = readdir $dir;
        closedir $dir;
    } catch {
        $c->stash->{response} = {
            "error"   => "Failed to open directory '$full_path'",
            "success" => JSON::false,
        };
        $c->detach('serialize');
    };

    my $regexp = $self->filter;
    @files = grep { !/$regexp/ } @files if ($regexp);

    @files = map { "$path/$_" } @files if ($self->full_paths);



( run in 1.464 second using v1.01-cache-2.11-cpan-39bf76dae61 )