[23138] | 1 | #!/usr/bin/perl -w
|
---|
[23091] | 2 |
|
---|
| 3 | use strict;
|
---|
| 4 |
|
---|
| 5 | use LWP::UserAgent;
|
---|
| 6 | use HTTP::Request::Common;
|
---|
| 7 |
|
---|
| 8 | use CGI::Carp qw(fatalsToBrowser);
|
---|
| 9 | use CGI;
|
---|
| 10 |
|
---|
[23147] | 11 | use File::Basename;
|
---|
| 12 |
|
---|
[23146] | 13 | BEGIN {
|
---|
| 14 | eval('require "./gsdlCGI.pm"');
|
---|
| 15 | if ($@)
|
---|
| 16 | {
|
---|
| 17 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 18 | print STDOUT "ERROR: $@\n";
|
---|
| 19 | exit 0;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | # Line to stop annoying child DOS CMD windows from appearing
|
---|
| 23 | Win32::SetChildShowWindow(0)
|
---|
| 24 | if defined &Win32::SetChildShowWindow;
|
---|
[23091] | 25 | }
|
---|
| 26 |
|
---|
| 27 |
|
---|
[23146] | 28 |
|
---|
| 29 |
|
---|
[23138] | 30 | sub get_infodb_type
|
---|
| 31 | {
|
---|
[23144] | 32 | my ($opt_site,$collect_home,$collect) = @_;
|
---|
[23138] | 33 |
|
---|
[23144] | 34 | my $out = "STDERR";
|
---|
| 35 |
|
---|
| 36 | $collect = &colcfg::use_collection($opt_site, $collect, $collect_home);
|
---|
| 37 |
|
---|
| 38 | if ($collect eq "") {
|
---|
| 39 | print STDERR "Error: failed to find collection $collect in $collect_home\n";
|
---|
| 40 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 41 | print STDOUT "ERROR: Failed to find collection $collect\n";
|
---|
| 42 | exit 0;
|
---|
| 43 |
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | # Read in the collection configuration file.
|
---|
| 47 | my ($config_filename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
|
---|
| 48 | my $collectcfg = &colcfg::read_collection_cfg ($config_filename, $gs_mode);
|
---|
| 49 |
|
---|
| 50 | return $collectcfg->{'infodbtype'};
|
---|
[23138] | 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | sub oid_to_docxml_filename
|
---|
| 55 | {
|
---|
[23144] | 56 | my ($opt_site,$collect_home,$collect,$docid) = @_;
|
---|
[23138] | 57 |
|
---|
[23144] | 58 | my $infodb_type = get_infodb_type($opt_site,$collect_home,$collect);
|
---|
[23138] | 59 |
|
---|
[23147] | 60 | # Derive the archives dir
|
---|
| 61 | my $archive_dir = &util::filename_cat($collect_home,$collect,"archives");
|
---|
[23138] | 62 |
|
---|
| 63 | # Obtain the doc.xml path for the specified docID
|
---|
| 64 | my $arcinfo_doc_filename
|
---|
| 65 | = &dbutil::get_infodb_file_path($infodb_type, "archiveinf-doc",
|
---|
[23147] | 66 | $archive_dir);
|
---|
[23482] | 67 | my $doc_rec
|
---|
[23138] | 68 | = &dbutil::read_infodb_entry($infodb_type, $arcinfo_doc_filename,
|
---|
| 69 | $docid);
|
---|
[23482] | 70 |
|
---|
[23138] | 71 | my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
|
---|
[23147] | 72 | my $assoc_path = dirname($doc_xml_file);
|
---|
| 73 |
|
---|
[23138] | 74 | # The $doc_xml_file is relative to the archives, so now let's get the
|
---|
| 75 | # full path
|
---|
[23147] | 76 | my $doc_xml_filename = &util::filename_cat($archive_dir,$doc_xml_file);
|
---|
[23138] | 77 |
|
---|
[23147] | 78 | return ($doc_xml_filename,$assoc_path);
|
---|
[23138] | 79 | }
|
---|
| 80 |
|
---|
[23147] | 81 | sub zip_up_archives_doc
|
---|
| 82 | {
|
---|
| 83 | my ($gsdl_cgi,$collect_home,$collect,$doc_xml_filename,$assoc_path) = @_;
|
---|
| 84 |
|
---|
| 85 | my $timestamp = time();
|
---|
| 86 | my $lang_env = $gsdl_cgi->clean_param("lr") || "";
|
---|
| 87 |
|
---|
| 88 | my $archive_dir = &util::filename_cat($collect_home,$collect,"archives");
|
---|
| 89 |
|
---|
| 90 | # Zip up the doc_xml file and all the files associated with it
|
---|
| 91 | my $java = $gsdl_cgi->get_java_path();
|
---|
| 92 | my $jar_dir= &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java");
|
---|
| 93 | my $java_classpath = &util::filename_cat($jar_dir,"GLIServer.jar");
|
---|
| 94 |
|
---|
[23516] | 95 | if (!-f $java_classpath) {
|
---|
[23518] | 96 | my $progname = $0;
|
---|
| 97 | $progname =~ s/^.*[\/\\]//;
|
---|
| 98 | my $mess = "$progname:\nFailed to find $java_classpath\n";
|
---|
| 99 | $gsdl_cgi->generate_error($mess);
|
---|
[23516] | 100 | }
|
---|
| 101 |
|
---|
[23147] | 102 | my $zip_file = "$collect-$timestamp.zip";
|
---|
| 103 | my $zip_file_path = &util::filename_cat($archive_dir,$zip_file);
|
---|
| 104 |
|
---|
| 105 | my $java_args = "\"$zip_file_path\" \"$archive_dir\" \"$assoc_path\"";
|
---|
| 106 |
|
---|
| 107 | $ENV{'LANG'} = $lang_env;
|
---|
[23161] | 108 | my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
|
---|
[23147] | 109 |
|
---|
| 110 | my $java_output = `$java_command`;
|
---|
| 111 | my $java_status = $?;
|
---|
| 112 | if ($java_status > 0) {
|
---|
| 113 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | # Check that the zip file was created successfully
|
---|
| 117 | if (!-e $zip_file_path || -z $zip_file_path) {
|
---|
| 118 | $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return $zip_file_path;
|
---|
| 122 |
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[23091] | 125 | sub main
|
---|
| 126 | {
|
---|
| 127 | # Setup greenstone Perl include paths so additional packages can be found
|
---|
| 128 | my $gsdl_cgi = gsdlCGI->new();
|
---|
| 129 | $gsdl_cgi->setup_gsdl();
|
---|
| 130 |
|
---|
[23138] | 131 | my $gsdl_home = $gsdl_cgi->get_gsdl_home();
|
---|
| 132 | my $collect_home = &util::filename_cat($gsdl_home,"collect");
|
---|
| 133 |
|
---|
| 134 | require dbutil;
|
---|
[23091] | 135 | require talkback;
|
---|
[23145] | 136 | require colcfg;
|
---|
[23091] | 137 |
|
---|
[23142] | 138 | my $oid = $gsdl_cgi->param('oid');
|
---|
[23177] | 139 | my $collect = $gsdl_cgi->param('fromCollect');
|
---|
| 140 | my $toCollect = $gsdl_cgi->param('toCollect');
|
---|
[23144] | 141 | my $site = $gsdl_cgi->param('site');
|
---|
[23091] | 142 |
|
---|
[23138] | 143 | # sanity check
|
---|
| 144 | if (!defined $oid || !defined $collect) {
|
---|
| 145 | print STDOUT "Content-type:text/plain\n\n";
|
---|
[23142] | 146 | print STDOUT "ERROR: Malformed CGI argments. Need to specify 'oid' and 'collect'\n";
|
---|
[23138] | 147 | exit 0;
|
---|
[23091] | 148 | }
|
---|
| 149 |
|
---|
[23138] | 150 | my $uniq_prefix = "$collect-$oid";
|
---|
[23091] | 151 |
|
---|
[23147] | 152 | my ($docxml_filename,$assoc_path)
|
---|
| 153 | = oid_to_docxml_filename($site,$collect_home,$collect,$oid);
|
---|
[23138] | 154 |
|
---|
[23147] | 155 | my $zip_filename
|
---|
| 156 | = zip_up_archives_doc($gsdl_cgi,$collect_home,$collect,
|
---|
| 157 | $docxml_filename,$assoc_path);
|
---|
| 158 |
|
---|
[23138] | 159 | my $talktoUploadURL = $gsdl_cgi->param('talktoUpload');
|
---|
| 160 |
|
---|
[23091] | 161 | my $browser = LWP::UserAgent->new(agent => 'Perl File Upload');
|
---|
| 162 |
|
---|
| 163 | my $response = $browser->post(
|
---|
| 164 | $talktoUploadURL,
|
---|
| 165 | [ 'yes_upload' => '1',
|
---|
| 166 | 'process' => '1',
|
---|
[23142] | 167 | 'oid' => $oid,
|
---|
[23177] | 168 | 'toCollect' => $toCollect,
|
---|
[23162] | 169 | 'uploadedfile' => [$zip_filename, "$uniq_prefix-doc.zip"]
|
---|
[23091] | 170 | ],
|
---|
| 171 | 'Content_Type' => 'form-data'
|
---|
| 172 | );
|
---|
| 173 |
|
---|
| 174 | if ($response->is_success) {
|
---|
| 175 | print "Content-type:text/html\n\n";
|
---|
| 176 | print $response->content;
|
---|
| 177 | }
|
---|
| 178 | else {
|
---|
| 179 | print $response->error_as_HTML;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | main();
|
---|
| 185 |
|
---|