source: trunk/gsdl/cgi-bin/upload@ 10252

Last change on this file since 10252 was 10252, checked in by mdewsnip, 19 years ago

Unzip.java now takes the zip file path and the directory to unzip into as arguments. This more general functionality is necessary for the West Yorkshire stuff.

The upload script has been changed accordingly, and tidied up a bit.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1#!/usr/bin/perl -w
2
3use gsdlCGI;
4
5sub load_gsdl_utils
6{
7 my ($gsdlhome) = @_;
8
9 require "$gsdlhome/perllib/util.pm";
10}
11
12sub main
13{
14 my $gsdl_cgi = new gsdlCGI();
15
16 $gsdl_cgi->setup_gsdl();
17 my $gsdlhome = $ENV{'GSDLHOME'};
18
19 load_gsdl_utils($gsdlhome);
20
21 my $col = $gsdl_cgi->clean_param("c");
22 if ((!defined $col) || ($col =~ m/^\s*$/)) {
23 $gsdl_cgi->generate_error("No collection name specified");
24 }
25
26 my $full_col = &util::filename_cat($gsdlhome,"collect",$col);
27
28 #If the collection dir on the server somehow got erased, create it.
29 if(!-e $full_col) {
30 &util::mk_dir($full_col);
31 }
32
33 my $dir = $gsdl_cgi->clean_param("dir");
34 if ((!defined $dir) || ($dir =~ m/^\s*$/)) {
35 $gsdl_cgi->generate_error("No directory name specified");
36 }
37
38 my $delete = $gsdl_cgi->clean_param("del");
39 $delete = "all" if((!defined $delete) || ($delete eq ""));
40
41 $dir = "" if (($dir eq ".") || ($dir =~ m/^\.\./));
42
43 if ($col =~ m/^\//) {
44 # leading / at start denotes special "cols" such as "/tmp"
45 $gsdl_cgi->checked_chdir("$gsdlhome");
46 $col =~ s/^\///;
47 }
48 else {
49 $gsdl_cgi->checked_chdir("$gsdlhome/collect");
50 }
51
52 my $col_dir = &util::filename_cat($col,$dir);
53 my $full_col_dir = &util::filename_cat($full_col,$dir);
54
55 if($delete eq "all") {
56 $gsdl_cgi->local_rm_r($col_dir);
57 }
58 elsif($delete eq "files") {
59 my $full_col_dir = &util::filename_cat($gsdlhome,"collect",$col_dir);
60 # delete everything except metadata.xml files
61 &util::filtered_rm_r($full_col_dir,undef,"metadata.xml\$");
62 }
63 elsif($delete eq "metadata") {
64 my $full_col_dir = &util::filename_cat($gsdlhome,"collect",$col_dir);
65 # delete only metadata xml files
66 &util::filtered_rm_r($full_col_dir,"metadata.xml\$",undef);
67 }
68
69 if(!-e $full_col_dir) {
70 &util::mk_dir($full_col_dir);
71 }
72
73 my $zip_file_name = $col . ".zip";
74 my $zip_file_path = &util::filename_cat($full_col_dir, $zip_file_name);
75 if (open(ZOUT, ">$zip_file_path")) {
76 binmode(ZOUT);
77
78 my $ZIN = $gsdl_cgi->upload('zip');
79 binmode($ZIN);
80
81 my $buf;
82 my $num_bytes = 0;
83
84 while (read($ZIN,$buf,1024)>0) {
85 print ZOUT $buf;
86 $num_bytes += length($buf);
87 }
88
89 close($ZIN);
90 close(ZOUT);
91
92 my $java = $gsdl_cgi->check_for_java();
93
94 my $classpath = &util::filename_cat($gsdlhome,"bin","java",
95 "SignedGatherer.jar");
96
97 my $java_cmd = "$java -classpath \"$classpath\" org.greenstone.gatherer.util.Unzip \"$zip_file_path\" \"$full_col_dir\"";
98 $gsdl_cgi->generate_ok_message("Command: $java_cmd\n");
99
100 my $java_output = `$java_cmd`;
101
102 my $status = $?;
103 unlink "$zip_file_path";
104
105 if ($status > 0) {
106 $status = $status/256;
107 my $exit_status = "Exit status: $status\n";
108 my $mess = "Java failed: $java_cmd\n--\n$java_output\n$exit_status";
109 $gsdl_cgi->generate_error($mess);
110 }
111
112 $gsdl_cgi->generate_ok_message("$dir or $col uploaded successfully.");
113 }
114 else {
115 $gsdl_cgi->generate_error("unable to open $zip_file_path");
116 }
117}
118
119&main();
Note: See TracBrowser for help on using the repository browser.