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

Annotation of /FigWebServices/cluster_stage.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (view) (download)

1 : olson 1.1 use strict;
2 :    
3 :     #use Common::Errors;
4 :    
5 :     =head1 Cluster Staging Interface
6 :    
7 :     This module provides a CGI interface for retrieving staged input files via HTTP.
8 :    
9 :     The client must provide the job id and work id for the staging work that we
10 :     are retrieving.
11 :    
12 :     =cut
13 :    
14 :     use strict;
15 :     use FIG;
16 :     use CGI;
17 :    
18 :     use Cluster;
19 :    
20 :     my $cgi = new CGI;
21 :    
22 :     sub fail($)
23 :     {
24 :     my($mesg) = @_;
25 :    
26 :     print $cgi->header(-type => "text/html",
27 :     -status => "500 error");
28 :     print "Staging failed: $mesg\n";
29 :     exit;
30 :     }
31 :    
32 : olson 1.2 warn "Environment:\n";
33 :     for my $k (sort keys(%ENV))
34 :     {
35 :     warn "$k=$ENV{$k}\n";
36 :     }
37 :    
38 :     warn "Params:\n";
39 :     for my $k ($cgi->param())
40 :     {
41 :     warn "$k=" . $cgi->param($k) . "\n";
42 :     }
43 : olson 1.1
44 :     my $fig = new FIG;
45 :     my $job_id = $cgi->param("job_id");
46 :     my $work_id = $cgi->param("work_id");
47 : olson 1.2 my $filename = $cgi->param("file");
48 : olson 1.1
49 :     $job_id =~ /^\d+/ or fail "Invalid job_id\n";
50 :     $work_id =~ /^\d+/ or fail "Invalid work_id\n";
51 :    
52 : olson 1.2 #
53 :     # If filename has been give, we're uploading. Otherwise
54 :     # this is a download.
55 :    
56 : olson 1.1 my $c = new Cluster::Broker($fig);
57 :    
58 : olson 1.2 if ($filename ne "")
59 :     {
60 :     warn "Doing upload\n";
61 :     eval {
62 :     do_upload($c, $fig, $job_id, $work_id, $filename);
63 :     };
64 :     if ($@)
65 :     {
66 :     fail "upload failed: $@";
67 :     }
68 :     }
69 :     else
70 :     {
71 :     warn "Doing download\n";
72 :     do_download($c, $fig, $job_id, $work_id);
73 :     }
74 :    
75 :     sub do_upload
76 :     {
77 :     my($c, $fig, $job_id, $work_id, $filename) = @_;
78 : olson 1.1
79 : olson 1.2 warn " UPLOAD: jobid=$job_id work_id=$work_id filename=$filename\n";
80 : olson 1.1
81 : olson 1.2 #
82 :     # Determine location of output file.
83 :     #
84 :    
85 :     my $output_fh = $c->open_output_file($job_id, $work_id, $filename);
86 :    
87 :     #
88 :     # File is passed as the cgi parameter named "file".
89 :     #
90 :     my $fh = $cgi->upload('file');
91 :     warn "Got fh $fh\n";
92 :    
93 :     my($n, $buf);
94 :     while ($n = read($fh, $buf, 4096))
95 :     {
96 :     print $output_fh $buf;
97 :     }
98 :     close($fh);
99 :     close($output_fh);
100 : olson 1.1
101 : olson 1.2 print $cgi->header(-type => "text/plain");
102 :     print "File uploaded OK\n";
103 :     }
104 : olson 1.1
105 : olson 1.2 sub do_download
106 : olson 1.1 {
107 : olson 1.2 my($c, $fig, $job_id, $work_id) = @_;
108 :    
109 :     my ($fh, $size, $basename);
110 :    
111 :     my $ok = eval {
112 :     ($fh, $size, $basename) = $c->open_staging_file($job_id, $work_id);
113 :     };
114 :    
115 :     $ok or fail "$@";
116 :     $fh or fail "Cannot find staging file";
117 :    
118 :     print $cgi->header(-type => "application/octet-stream",
119 :     -Content_Length => $size,
120 :     -Content_Disposition => "attachment;filename=\"$basename\"",
121 :     );
122 :    
123 :     my $buf;
124 :     while (my $n = read($fh, $buf, 4096))
125 :     {
126 :     print $buf;
127 :     }
128 :    
129 :     close($fh);
130 : olson 1.1 }
131 :    
132 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3