Dancer-Plugin-DirectoryView

 view release on metacpan or  search on metacpan

lib/Dancer/Plugin/DirectoryView.pm  view on Meta::CPAN

    if (index($real_path, $root_dir) != 0) {
        return send_error("Not allowed", 403);
    }
    
    if (-f $real_path) {
        #
        # Regular file
        #
        send_file($real_path, system_path => $system_path);
    }
    elsif (-d $real_path) {
        #
        # Directory -- show contents
        #
        my @files = ();
        
        if ($real_path ne $root_dir) {
            push(@files, {
                url => "../",
                name => "Up to parent directory",
                size => '',
                mime_type => '',
                mtime => '',
                class => 'parent-directory'
            });
        }
        
        my $dh = DirHandle->new($real_path);
        my @entries;
        while (defined(my $entry = $dh->read)) {
            next if $entry eq '.' || $entry eq '..';
            next if $entry =~ /^\./ && !$show_hidden_files;
            push @entries, $entry;
        }
        
        # Mapping of MIME types to CSS class names
        my %classes = (
            'directory' => 'directory',
            'application/javascript' => 'file-application-javascript',
            'application/pdf' => 'file-application-pdf',
            'application/vnd.ms-excel' => 'file-application-vnd-ms-excel',
            'application/vnd.oasis.opendocument.spreadsheet' => 
                'file-application-vnd-oasis-opendocument-spreadsheet',
            'application/vnd.oasis.opendocument.text' => 
                'file-application-vnd-oasis-opendocument-text',
            'application/x-httpd-php' => 'file-application-x-php',
            'application/x-msword' => 'file-application-msword',
            'application/x-perl' => 'file-application-x-perl',
            'application/xml' => 'file-application-xml',
            'application/zip' => 'file-application-zip',
            'image/jpeg' => 'file-image-x-generic',
            'image/png' => 'file-image-x-generic',
            'text/html' => 'file-text-html',
            'text/plain' => 'file-text-plain',
            'text/x-csrc' => 'file-text-x-csrc'
        );
        
        for my $name (sort { $a cmp $b } @entries) {
            my $file = catfile($real_path, $name);
            my $url = $name;
            $url = join '/', map { uri_escape($_) } split m!/!, $url;
            
            my $is_dir = -d $file;
            my @stat = stat(_);
            
            if ($is_dir) {
                $name .= '/';
                $url .= '/';
            }
            
            my $mime_type = $is_dir ? 'directory' : $mime->for_file($name) 
                || '';
                
            push(@files, {
                url => $url,
                name => $name,
                size => $is_dir ? '' : _format_size($stat[7]),
                mime_type => $mime_type,
                mtime => HTTP::Date::time2str($stat[9]),
                class => $classes{$mime_type} || 'file-unknown'
            });
        }

        if ($template) {
            # Get a new instance of Dancer::Template::Simple
            my $template_simple = Dancer::Engine->build(template => 'simple');
            $template_simple->start_tag('<%');
            $template_simple->stop_tag('%>');

            my $template_dir;

            # Look for the template files in the application's views directory
            if (-d catfile($views_dir, $template)) {
                $template_dir = catfile($views_dir, $template);
            }
            # Then, try the plugin's views directory
            elsif (-d catfile($dist_dir, 'views', $template)) {
                $template_dir = catfile($dist_dir, 'views', $template);
            }
            else {
                # TODO: Template not found -- handle error
            }
            
            my $file_tpl = catfile($template_dir, 'file.tt');
            my $listing_tpl = catfile($template_dir, 'listing.tt');
            my $layout_tpl = catfile($template_dir, 'layout.tt');
            
            # Render the list of files
            my $files_html = '';
            for my $file (@files) {
                $files_html .= $template_simple->render($file_tpl,
                    { file => $file });
            }
            
            # Insert the rendered list into the listing container
            my $listing_html = $template_simple->render($listing_tpl,
                { path => '/' . $path, files => $files_html });
            
            if ($options{layout}) {
                # Is there a corresponding layout file in the views directory?
                if (-f catfile($views_dir, 'layouts',



( run in 0.701 second using v1.01-cache-2.11-cpan-71847e10f99 )