mirror of
https://github.com/webmin/webmin.git
synced 2026-02-03 14:13:29 +00:00
Add overwrite control when uploading a file in File Manager
This commit is contained in:
@@ -487,7 +487,7 @@ foreach my $fref (@{$files_to_extract}) {
|
||||
my $name = $fref->{'file'};
|
||||
|
||||
my $extract_to = "$cwd/" . fileparse("$cwd/$name", qr/\.[^.]*/);
|
||||
if (-e $extract_to) {
|
||||
if (-e $extract_to && !$in{'overwrite_existing'}) {
|
||||
$extract_to .= "_" . int(rand(1000)) . $$;
|
||||
}
|
||||
mkdir($extract_to);
|
||||
|
||||
@@ -166,3 +166,4 @@ index_return=file listing
|
||||
upload_dirs=Directory Upload
|
||||
extract_uploaded=Extract Compressed
|
||||
extract_cmd_not_avail=Could not extract $1 file as $2 command is missing
|
||||
overwrite_existing=Overwrite Existing
|
||||
|
||||
@@ -337,6 +337,7 @@
|
||||
</div>
|
||||
<input type="checkbox" data-id="webkitdirectory" onchange="document.querySelector('#upload-form > #upfiles').toggleAttribute('webkitdirectory')"> $text{'upload_dirs'}
|
||||
<input type="checkbox" data-id="extract_uploaded" onchange="t=document.querySelector('#upload-form'),v=t.getAttribute('action'),x='&extract_uploaded=1',this.checked?t.setAttribute('action', t.getAttribute('action') + x):t.setAttribute('action',t.getAttribute('action').replace(x,''))"> $text{'extract_uploaded'}
|
||||
<input type="checkbox" data-id="overwrite_existing" onchange="t=document.querySelector('#upload-form'),v=t.getAttribute('action'),x='&overwrite_existing=1',this.checked?t.setAttribute('action', t.getAttribute('action') + x):t.setAttribute('action',t.getAttribute('action').replace(x,''))"> $text{'overwrite_existing'}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" onclick="uploadFiles()">$text{'upload_files'}</button>
|
||||
|
||||
@@ -111,6 +111,8 @@
|
||||
<input type="checkbox" id="filetype" onchange="this.parentElement.querySelector('#upfiles').toggleAttribute('webkitdirectory')"><label for="filetype"> $text{'upload_dirs'}</label>
|
||||
<br>
|
||||
<input type="checkbox" id="extract_upload" onchange="t=document.querySelector('#upload-form'),v=t.getAttribute('action'),x='&extract_uploaded=1',this.checked?t.setAttribute('action', t.getAttribute('action') + x):t.setAttribute('action',t.getAttribute('action').replace(x,''))"><label for="extract_upload"> $text{'extract_uploaded'}</label>
|
||||
<br>
|
||||
<input type="checkbox" id="overwrite_existing" onchange="t=document.querySelector('#upload-form'),v=t.getAttribute('action'),x='&overwrite_existing=1',this.checked?t.setAttribute('action', t.getAttribute('action') + x):t.setAttribute('action',t.getAttribute('action').replace(x,''))"><label for="overwrite_existing"> $text{'overwrite_existing'}</label>
|
||||
</form>
|
||||
<div id="readyForUploadList" class="well" style="max-height: 180px;">
|
||||
</div>
|
||||
|
||||
@@ -9,6 +9,7 @@ get_paths();
|
||||
|
||||
my @errors;
|
||||
my @uploaded_files;
|
||||
my $uploaded_dir;
|
||||
$line = "";
|
||||
|
||||
# Use Webmin's callback function to track progress
|
||||
@@ -57,6 +58,18 @@ MAINLOOP: while(index($line,"$boundary--") == -1) {
|
||||
if ($dir) {
|
||||
my @dirs = split('/', $dir);
|
||||
$dir = '/';
|
||||
# If overwriting is not allowed check for dupes
|
||||
if (!$in{'overwrite_existing'}) {
|
||||
if ($dirs[0] && -e "$cwd/$dirs[0]") {
|
||||
# As only one directory upload at a time allowed
|
||||
# check if parent exists and if it does add a suffix
|
||||
if (!$uploaded_dir) {
|
||||
$uploaded_dir = $dirs[0] . "_" . int(rand(1000)) . $$;
|
||||
}
|
||||
$file =~ s/^(\Q$dirs[0]\E)/$uploaded_dir/;
|
||||
$dirs[0] = $uploaded_dir;
|
||||
}
|
||||
}
|
||||
foreach my $updir (@dirs) {
|
||||
$dir .= "$updir/";
|
||||
if (!-e "$cwd$dir") {
|
||||
@@ -66,6 +79,15 @@ MAINLOOP: while(index($line,"$boundary--") == -1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
# In case of a regular file check for dupes
|
||||
if (!$in{'overwrite_existing'}) {
|
||||
if ($file && -e "$cwd/$file") {
|
||||
# If file exists add a suffix
|
||||
my ($file_name, $file_extension) = $file =~ /(?|(.*)\.([^.]+$)|(.*))/;
|
||||
$file = $file_name . "_" . int(rand(1000)) . $$ . ($file_extension ? ".$file_extension" : "");
|
||||
}
|
||||
}
|
||||
|
||||
# OK, we have a file, let`s save it
|
||||
my $full = "$cwd/$file";
|
||||
my $newfile = !-e $full;
|
||||
|
||||
Reference in New Issue
Block a user