diff --git a/script/rename-preserve-datetime.sh b/script/rename-preserve-datetime.sh deleted file mode 100755 index 192def8..0000000 --- a/script/rename-preserve-datetime.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -# Define the filename and new filename -old_filename="$1" -new_filename="$2" - -# Preserve metadata: Access and modification time -access_time=$(stat --format='%X' "$old_filename") -modification_time=$(stat --format='%Y' "$alter_dateiname") - -# Rename file -mv "$old_filename" "$new_filename" - -# Restore access and modification time -touch -a -d @$access_time "$new_filename" -touch -m -d @$modification_time "$new_filename" - -echo "File successfully renamed and access/modification time restored." diff --git a/www/private/views/api/file-rename.php b/www/private/views/api/file-rename.php index 5ecb4dd..5238017 100755 --- a/www/private/views/api/file-rename.php +++ b/www/private/views/api/file-rename.php @@ -1,8 +1,4 @@ fileatime($original_filename), // Access time + 'modification_time' => filemtime($original_filename) // Modification time + ]; +} + $file_info = file_get_verified_fileinfo('/scans/', urldecode($file)); $original_filename = $file_info['full_path']; @@ -26,7 +31,6 @@ if ($data !== null) { - // Access the data and perform operations $new_filename = $data['new_filename']; $new_filename_prefix = $data['new_filename_prefix']; @@ -51,19 +55,20 @@ $final_filename = $file_info['dir'] . '/' . $file_info['date_from_file'] . '-' . $file_info['time_from_file'] . ' ' . $target_filename; } - - $UID = escapeshellarg($UID); // Ensure UID is safe - $original_filename = escapeshellarg($original_filename); // Escape the filename for shell - $final_filename = escapeshellarg($final_filename); // Escape the new filename - #$command = 'sudo -u \#'.$UID.' mv '.$original_filename.' '.$final_filename; + // Get access and modification times of the old file + $times = getFileTimes($original_filename); - //looks complicated, but preserve the original file creation time - #$command = 'sudo -u \#'.$UID.' cp --preserve=all '.$original_filename.' '.$final_filename.' && rm '.$original_filename; - $command = 'sudo -u \#'.$UID.' /opt/brother/scanner/brscan-skey/script/rename-preserve-datetime.sh '.$original_filename.' '.$final_filename ; - $output = shell_exec($command); + // Rename the file + if (rename($original_filename, $final_filename)) { + + // Restore access and modification time using 'touch' + @touch($final_filename, $times['modification_time'], $times['access_time']); + send_json_error(200, "Renamed file successfully"); + } else { + send_json_error(400, "Error renaming the file"); + } - json(array('status' => 'success')); }else{ send_json_error(400, "Invalid filename"); }