Support multiple file uploads

This commit is contained in:
Jamie Cameron
2016-01-23 21:48:42 -08:00
parent bb98a1af45
commit b001855672
3 changed files with 151 additions and 135 deletions
+2
View File
@@ -12,3 +12,5 @@ The download from server form can be used to fetch an entire directory, which is
Added fields for sending an email notification when a background download or file upload completes.
---- Changes since 1.490 ----
Added support for extracting LHArc format files when uploading.
---- Changes since 1.780 ----
When uploading, multiple files can now be selected at once.
+1 -1
View File
@@ -174,7 +174,7 @@ if ($can_upload) {
# Upload fields
$utable = "";
for($i=0; $i<4; $i++) {
$utable .= &ui_upload("upload$i")."\n";
$utable .= &ui_upload("upload$i", 40, 0, undef, 1)."\n";
$utable .= "<br>\n" if ($i%2 == 1);
}
print &ui_table_row($text{'index_upload'}, $utable);
+148 -134
View File
@@ -6,7 +6,10 @@ require './updown-lib.pl';
&error_setup($text{'upload_err'});
&ReadParse(\%getin, "GET");
$upid = $getin{'id'};
&ReadParseMime($upload_max, \&read_parse_mime_callback, [ $upid ]);
&ReadParseMime($upload_max, \&read_parse_mime_callback, [ $upid ], 1);
foreach my $k (keys %in) {
$in{$k} = $in{$k}->[0] if ($k !~ /^upload\d+/);
}
$can_upload || &error($text{'upload_ecannot'});
# Validate inputs
@@ -32,9 +35,12 @@ else {
@uinfo = getpwnam($remote_user);
}
}
for($i=0; defined($d = $in{"upload$i"}); $i++) {
$f = $in{"upload${i}_filename"};
$found++ if ($d && $f);
for($i=0; defined($in{"upload$i"}); $i++) {
for(my $j=0; $j<@{$in{"upload$i"}}; $j++) {
$d = $in{"upload${i}"}->[$j];
$f = $in{"upload${i}_filename"}->[$j];
$found++ if ($d && $f);
}
}
$found || &error($text{'upload_enone'});
&can_write_file($in{'dir'}) ||
@@ -52,145 +58,153 @@ if (!-d $in{'dir'} && $in{'mkdir'}) {
# Save the actual files, showing progress
$msg = undef;
for($i=0; defined($d = $in{"upload$i"}); $i++) {
$f = $in{"upload${i}_filename"};
next if (!$f);
if (-d $in{'dir'}) {
$f =~ /([^\\\/]+)$/;
$path = "$in{'dir'}/$1";
}
else {
$path = $in{'dir'};
}
print &text('upload_saving', "<tt>$path</tt>"),"<br>\n";
if (!&open_tempfile(FILE, ">$path", 1)) {
&error(&text('upload_eopen', "<tt>$path</tt>", $!));
}
&print_tempfile(FILE, $d);
&close_tempfile(FILE);
push(@uploads, $path);
@st = stat($path);
print &text('upload_saved', &nice_size($st[7])),"<p>\n";
$estatus = undef;
if ($in{'zip'}) {
print &text('upload_unzipping', "<tt>$path</tt>"),"<br>\n";
local ($err, $out);
$path =~ /^(\S*\/)/;
local $dir = $1;
local $qdir = quotemeta($dir);
local $qpath = quotemeta($path);
local @files;
&switch_uid_back();
if ($path =~ /\.zip$/i) {
# ZIP file
if (!&has_command("unzip")) {
$err = &text('upload_ecmd', "unzip");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && unzip -o $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^\s*[a-z]+:\s+(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "zip";
}
elsif ($path =~ /\.tar$/i) {
# Un-compressed tar file
if (!&has_command("tar")) {
$err = &text('upload_ecmd', "tar");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && tar xvf $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "tar";
}
elsif ($path =~ /\.(lha|lhz)$/i) {
# LHAarc file
if (!&has_command("lha")) {
$err = &text('upload_ecmd', "lha");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && lha xf $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/(\S[^\t]*\S)\s+\-\s+/) {
push(@files, "/".$1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "lha";
}
elsif ($path =~ /\.(tar\.gz|tgz|tar\.bz|tbz|tar\.bz2|tbz2)$/i) {
# Compressed tar file
local $zipper = $path =~ /bz(2?)$/i ? "bunzip2"
: "gunzip";
if (!&has_command("tar")) {
$err = &text('upload_ecmd', "tar");
}
elsif (!&has_command($zipper)) {
$err = &text('upload_ecmd', $zipper);
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && $zipper -c $qpath | tar xvf -)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = $zipper eq "gunzip" ? "tgz" : "tbz2";
for($i=0; defined($in{"upload$i"}); $i++) {
for(my $j=0; $j<@{$in{"upload$i"}}; $j++) {
$d = $in{"upload${i}"}->[$j];
$f = $in{"upload${i}_filename"}->[$j];
next if (!$f);
if (-d $in{'dir'}) {
$f =~ /([^\\\/]+)$/;
$path = "$in{'dir'}/$1";
}
else {
# Doesn't look possible
$err = $text{'upload_notcomp'};
$path = $in{'dir'};
}
&switch_uid_to($uinfo[2], scalar(@ginfo) ? $ginfo[2] : $uinfo[3]);
if (!$err) {
local $j = join("<br>",
map { "&nbsp;&nbsp;<tt>$_</tt>" } @files);
if ($in{'zip'} == 2) {
unlink($path);
$ext{$path} = $text{'upload_deleted'}."<br>".$j;
print &text('upload_saving', "<tt>$path</tt>"),"<br>\n";
if (!&open_tempfile(FILE, ">$path", 1)) {
&error(&text('upload_eopen', "<tt>$path</tt>", $!));
}
&print_tempfile(FILE, $d);
&close_tempfile(FILE);
push(@uploads, $path);
@st = stat($path);
print &text('upload_saved', &nice_size($st[7])),"<p>\n";
$estatus = undef;
if ($in{'zip'}) {
print &text('upload_unzipping',
"<tt>$path</tt>"),"<br>\n";
local ($err, $out);
$path =~ /^(\S*\/)/;
local $dir = $1;
local $qdir = quotemeta($dir);
local $qpath = quotemeta($path);
local @files;
&switch_uid_back();
if ($path =~ /\.zip$/i) {
# ZIP file
if (!&has_command("unzip")) {
$err = &text('upload_ecmd', "unzip");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && unzip -o $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^\s*[a-z]+:\s+(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "zip";
}
elsif ($path =~ /\.tar$/i) {
# Un-compressed tar file
if (!&has_command("tar")) {
$err = &text('upload_ecmd', "tar");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && tar xvf $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "tar";
}
elsif ($path =~ /\.(lha|lhz)$/i) {
# LHAarc file
if (!&has_command("lha")) {
$err = &text('upload_ecmd', "lha");
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && lha xf $qpath)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/(\S[^\t]*\S)\s+\-\s+/) {
push(@files, "/".$1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = "lha";
}
elsif ($path =~ /\.(tar\.gz|tgz|tar\.bz|tbz|tar\.bz2|tbz2)$/i) {
# Compressed tar file
local $zipper = $path =~ /bz(2?)$/i ? "bunzip2"
: "gunzip";
if (!&has_command("tar")) {
$err = &text('upload_ecmd', "tar");
}
elsif (!&has_command($zipper)) {
$err = &text('upload_ecmd', $zipper);
}
else {
open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && $zipper -c $qpath | tar xvf -)")." 2>&1 </dev/null |");
while(<OUT>) {
$out .= $_;
if (/^(.*)/) {
push(@files, $1);
}
}
close(OUT);
$err = $out if ($?);
}
$fmt = $zipper eq "gunzip" ? "tgz" : "tbz2";
}
else {
$ext{$path} = $text{'upload_extracted'}."<br>".$j;
# Doesn't look possible
$err = $text{'upload_notcomp'};
}
&switch_uid_to($uinfo[2],
scalar(@ginfo) ? $ginfo[2] : $uinfo[3]);
if (!$err) {
my $jn = join("<br>",
map { "&nbsp;&nbsp;<tt>$_</tt>" }
@files);
if ($in{'zip'} == 2) {
unlink($path);
$ext{$path} = $text{'upload_deleted'}.
"<br>".$jn;
}
else {
$ext{$path} = $text{'upload_extracted'}.
"<br>".$jn;
}
}
else {
$ext{$path} = &text('email_eextract', $err);
}
$estatus = $err ? &text('email_extfailed', $err)
: &text('email_extdone_'.$fmt);
print &text('upload_unzipdone', $estatus),"<p>\n";
}
else {
$ext{$path} = &text('email_eextract', $err);
}
$estatus = $err ? &text('email_extfailed', $err)
: &text('email_extdone_'.$fmt);
print &text('upload_unzipdone', $estatus),"<p>\n";
}
# Add to email message
$msg .= &text('email_upfile', $f)."\n";
$msg .= &text('email_uppath', $path)."\n";
$msg .= &text('email_upsize', &nice_size($st[7]))."\n";
if ($estatus) {
$msg .= &text('email_upextract', $estatus)."\n";
# Add to email message
$msg .= &text('email_upfile', $f)."\n";
$msg .= &text('email_uppath', $path)."\n";
$msg .= &text('email_upsize', &nice_size($st[7]))."\n";
if ($estatus) {
$msg .= &text('email_upextract', $estatus)."\n";
}
$msg .= "\n";
}
$msg .= "\n";
}
# Switch back to root