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