From 4210dae7e55c3afc432be87d1b6c093caea91de5 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 27 Apr 2025 13:43:28 -0500 Subject: [PATCH] Add upload conflict prompts --- tinyfilemanager.php | 215 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 196 insertions(+), 19 deletions(-) diff --git a/tinyfilemanager.php b/tinyfilemanager.php index 27c868a..057c8de 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -122,10 +122,11 @@ $max_upload_size_bytes = 5000000000; // size 5,000,000,000 bytes (~5GB) $upload_chunk_size_bytes = 2000000; // chunk size 2,000,000 bytes (~2MB) // Handle file uploads where the file exists +// PROMPT => ask the user to resolve specific conflicts, or leave the temporary file // NEW => newly uploaded file is renamed with a timestamp appended // OLD => old file will be renamed with a timestamp appended // REPLACE => old file will be deleted -$upload_name_conflict_handling = 'OLD'; +$upload_name_conflict_handling = 'PROMPT'; // Possible rules are 'OFF', 'AND' or 'OR' // OFF => Don't check connection IP, defaults to OFF @@ -673,6 +674,46 @@ if ((isset($_SESSION[FM_SESSION_ID]['logged'], $auth_users[$_SESSION[FM_SESSION_ event_callback(array("fail" => $err)); } } + + // Upload Conflict Resolution + if (isset($_POST['type']) && $_POST['type'] == 'conflict') { + $path = $_POST['path']; + $filename = $_POST['filename']; + $resolution = $_POST['resoultion']; + + + $fullPath = $path . '/' . $filename; + $tempPath = "{$fullPath}.part"; + + if (!file_exists($tempPath)) { + echo json_encode(['status' => 'error', 'info' => "Temporary file not found. $tempPath"]); + exit; + } + + $ext = pathinfo($filename, PATHINFO_FILENAME) != '' ? strtolower(pathinfo($filename, PATHINFO_EXTENSION)) : ''; + $fullPathTarget = $fullPath; + $ext_1 = $ext ? '.' . $ext : ''; + $datedPath = $path . '/' . basename($filename, $ext_1) . '_' . date('ymdHis') . $ext_1; + + if (fm_is_excluded($filename, $path)) { + $fullPathTarget = $datedPath; + } else switch ($_POST['resolution']) { + case 'OLD': + fm_rename($fullPath, $datedPath); + break; + case 'REPLACE': + if (fm_rdelete($fullPath)) break; + case 'NEW': + default: + $fullPathTarget = $datedPath; + } + fm_rename($tempPath, $fullPathTarget); + + echo json_encode(['status' => 'resolved', + 'info' => "RESOLVED WITH: $path , $filename, $resolution"]); + exit; + } + exit(); } @@ -1051,24 +1092,30 @@ if (!empty($_FILES) && !FM_READONLY) { } if ($chunkIndex == $chunkTotal - 1) { - $fullPathTarget = $fullPath; + $fullPathTarget = $fullPath; + $ext_1 = $ext ? '.' . $ext : ''; + $datedPath = $path . '/' . basename($fullPathInput, $ext_1) . '_' . date('ymdHis') . $ext_1; if (file_exists($fullPath)) { - $ext_1 = $ext ? '.' . $ext : ''; - $datedPath = $path . '/' . basename($fullPathInput, $ext_1) . '_' . date('ymdHis') . $ext_1; - if(fm_is_exclude_items($fullPath)){ - $fullPathTarget = $datedPath; // excluded items should not be replaced or renamed - }else{ - switch($upload_name_conflict_handling) - { - case 'OLD': - fm_rename($fullPath,$datedPath); - break; - case 'REPLACE': - if(fm_rdelete($fullPath)) break; - case 'NEW': - default: - $fullPathTarget = $datedPath; - } + if (fm_is_excluded($fullPathInput, $path)) { + $fullPathTarget = $datedPath; // excluded items should not be replaced, renamed, or prompted + } else switch ($upload_name_conflict_handling) { + case 'PROMPT': + $response = array( + 'status' => 'conflict', + 'info' => "file upload successful with conflict", + 'filename' => $fullPathInput, + 'path' => $path + ); + echo json_encode($response); + exit(); + case 'OLD': + fm_rename($fullPath,$datedPath); + break; + case 'REPLACE': + if (fm_rdelete($fullPath)) break; + case 'NEW': + default: + $fullPathTarget = $datedPath; } } fm_rename("{$fullPath}.part", $fullPathTarget); @@ -1441,6 +1488,36 @@ if (isset($_GET['upload']) && !FM_READONLY) { + +