Skip to content

Commit

Permalink
changed my mind about this. removed bash in do it in php
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarcia007 committed Sep 24, 2024
1 parent a13aa18 commit 7d9a553
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
18 changes: 0 additions & 18 deletions script/rename-preserve-datetime.sh

This file was deleted.

35 changes: 20 additions & 15 deletions www/private/views/api/file-rename.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include('config.php');
require_once('helper.php');

Expand All @@ -16,6 +12,15 @@
send_json_error(400, "No file specified");
}


// Function to get file access and modification times
function getFileTimes($original_filename) {
return [
'access_time' => 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'];
Expand All @@ -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'];

Expand All @@ -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");
}
Expand Down

0 comments on commit 7d9a553

Please sign in to comment.