InflateColumn::File vs. InflateColumn::FS

May 24, 2009 at 4:12 pm

In my last Perl post I said IC::File fits my needs better and let me serve files using Static::Simple.  Well, it did that, but issues with properly handling deletes and updates forced me to re-evaluate my decision and switch to InflateColumn::FS.

The application will manage an archive of discussion papers, including abstracts, BibTeX citations, and supplemental data files.  I need to store uploaded files and provide links to download them from the application.

Retooling for IC::FS included subclassing IC::FS to tune its operation for my needs and writing my own utility method to serve up the files instead of using Static::Simple.  IC::FS uses UUIDs for filenames in the filesystem and I want the original filenames in the URLs in the application.  Serving up files directly isn’t difficult so I wrote my own Catalyst controller method, cribbing code from Static::Simple:

sub send_file : Private
{
    my ($self, $c, $file, $filename) = @_;

    my $type;
    my $stat = $file->stat;

    # Get MIME Type - swiped from Static::Simple
    if ( $filename =~ /.*\.(\S{1,})$/xms )
    {
        my $ext = $1;
        my $types = MIME::Types->new(only_complete => 1);
        $type = $types->mimeTypeOf($ext);
    }
    else
    {
        # Don't know type, so punt
        $type = 'text/plain';
    }

    $c->res->headers->content_type($type);
    $c->res->headers->content_length($stat->size);
    $c->res->headers->last_modified($stat->mtime);
    $c->res->headers->header("Content-Disposition" =>
                             "filename=\"$filename\"");

    # More code swiped from Static::Simple
    if ( Catalyst->VERSION le '5.33' )
    {
        # old File::Slurp method
        my $content = File::Slurp::read_file( $file );
        $c->res->body( $content );
    }
    else
    {
        # new method, pass an IO::File object to body
        my $fh = $file->openr;
        if ( defined $fh )
        {
            binmode $fh;
            $c->res->body( $fh );
        }
        else
        {
            Catalyst::Exception->throw(
                                       message => "Unable to open $file for reading" );
        }
    }
    return 1;
}

Categories: Perl

Leave a comment

Leave a comment

Feed

http://rambles.bearcircle.net / InflateColumn::File vs. InflateColumn::FS