Add overwrite control when uploading a file in File Manager

This commit is contained in:
Ilia Rostovtsev
2021-12-20 21:47:34 +03:00
parent 216b8ea2e4
commit fb0b5fac8f
5 changed files with 27 additions and 1 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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>

View File

@@ -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>

View File

@@ -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;