[Bio] / FigWebServices / cluster_stage.cgi Repository:
ViewVC logotype

View of /FigWebServices/cluster_stage.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (annotate)
Sun Feb 27 14:06:19 2005 UTC (15 years, 3 months ago) by olson
Branch: MAIN
CVS Tags: merge-trunktag-bobdev_news-2, Root-bobdev_news, merge-bobdev_news-1, merge-trunktag-bobdev_news-1, merge-bodev_news-3, caBIG-00-00-00, merge-bobdev_news-2, merge-trunktag-bodev_news-3
Branch point for: Branch-bobdev_news
Changes since 1.2: +3 -0 lines
More cluster fixes.

use strict;

#use Common::Errors;

=head1 Cluster Staging Interface

This module provides a CGI interface for retrieving staged input files via HTTP.

The client must provide the job id and work id for the staging work that we 
are retrieving.

=cut

use strict;
use FIG;
use CGI;

use Cluster;

my $cgi = new CGI;

sub fail($)
{
    my($mesg) = @_;

    print $cgi->header(-type => "text/html",
		       -status => "500 error");
    print "Staging failed: $mesg\n";
    exit;
}

if (0)
{
warn "Environment:\n";
for my $k (sort keys(%ENV))
{
    warn "$k=$ENV{$k}\n";
}

warn "Params:\n";
for my $k ($cgi->param())
{
    warn "$k=" . $cgi->param($k) . "\n";
}
}

my $fig = new FIG;
my $job_id = $cgi->param("job_id");
my $work_id = $cgi->param("work_id");
my $filename = $cgi->param("file");

$job_id =~ /^\d+/ or fail "Invalid job_id\n";
$work_id =~ /^\d+/ or fail "Invalid work_id\n";

#
# If filename has been give, we're uploading. Otherwise
# this is a download.

my $c = new Cluster::Broker($fig);

if ($filename ne "")
{
    warn "Doing upload\n";
    eval {
	do_upload($c, $fig, $job_id, $work_id, $filename);
    };
    if ($@)
    {
	fail "upload failed: $@";
    }
}
else
{
    warn "Doing download\n";
    do_download($c, $fig, $job_id, $work_id);
}

sub do_upload
{
    my($c, $fig, $job_id, $work_id, $filename) = @_;

    warn " UPLOAD: jobid=$job_id work_id=$work_id filename=$filename\n";

    #
    # Determine location of output file.
    #

    my $output_fh = $c->open_output_file($job_id, $work_id, $filename);

    #
    # File is passed as the cgi parameter named "file".
    #
    my $fh = $cgi->upload('file');
    warn "Got fh $fh\n";

    my($n, $buf);
    while ($n = read($fh, $buf, 4096))
    {
	print $output_fh $buf;
    }
    close($fh);
    close($output_fh);

    print $cgi->header(-type => "text/plain");
    print "File uploaded OK\n";
}

sub do_download
{
    my($c, $fig, $job_id, $work_id) = @_;

    my ($fh, $size, $basename);
    
    my $ok = eval {
	($fh, $size, $basename) = $c->open_staging_file($job_id, $work_id);
    };
    
    $ok or fail "$@";	 
    $fh or fail "Cannot find staging file";
    
    print $cgi->header(-type => "application/octet-stream",
		       -Content_Length => $size,
		       -Content_Disposition => "attachment;filename=\"$basename\"",
		      );
    
    my $buf;
    while (my $n = read($fh, $buf, 4096))
    {
	print $buf;
    }

    close($fh);
}



MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3