Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Jan 29, 2025
1 parent 0c3c227 commit d03de20
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions view/listFiles.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
require_once $global['systemRootPath'] . 'objects/Login.php';
header('Content-Type: application/json');

if(empty($global['allowed'])){
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

if (empty($global['allowed'])) {
$global['allowed'] = array();
}

Expand All @@ -12,40 +16,66 @@
$global['allowed'] = array_unique($global['allowed']);

$files = array();
if(Login::canBulkEncode()){

if (Login::canBulkEncode()) {
if (!empty($_POST['path'])) {
$path = $_POST['path'];
if (substr($path, -1) !== DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
$path = rtrim($_POST['path'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

// Log the received path
error_log("Bulk Encode: Received path - " . $path);

if (!file_exists($path)) {
error_log("Bulk Encode Error: Path does not exist - " . $path);
echo json_encode(["error" => "Path does not exist"]);
exit;
}


if (!is_readable($path)) {
error_log("Bulk Encode Error: Path is not readable - " . $path);
echo json_encode(["error" => "Path is not readable"]);
exit;
}

$video_array = array();
if (file_exists($path)) {
foreach ($global['allowed'] as $value) {
$video_array = array_merge($video_array, glob($path . "*." . $value));
foreach ($global['allowed'] as $ext) {
$filesFound = glob($path . "*." . $ext);
if ($filesFound === false) {
error_log("Bulk Encode Error: glob() failed for extension .$ext in path $path");
} else {
error_log("Bulk Encode: Found " . count($filesFound) . " files with extension .$ext");
}
$video_array = array_merge($video_array, $filesFound);
}

// Deduplication: Use an associative array to track already added files

if (empty($video_array)) {
error_log("Bulk Encode Warning: No files found in the directory.");
}

$addedFiles = [];

$id = 0;
foreach ($video_array as $key => $value) {
// If file is already added, skip
if(isset($addedFiles[strtolower($value)])) {
foreach ($video_array as $value) {
if (isset($addedFiles[strtolower($value)])) {
continue;
}

// Mark the file as added

$addedFiles[strtolower($value)] = true;

$path_parts = pathinfo($value);

$obj = new stdClass();
$obj->id = $id++;
$obj->path = _utf8_encode($value);
$obj->name = _utf8_encode($path_parts['basename']);
$obj->path = utf8_encode($value);
$obj->name = utf8_encode($path_parts['basename']);
$files[] = $obj;
}
} else {
error_log("Bulk Encode Error: No path provided.");
echo json_encode(["error" => "No path provided"]);
exit;
}
} else {
error_log("Bulk Encode Error: User does not have permission to bulk encode.");
echo json_encode(["error" => "Permission denied"]);
exit;
}

echo json_encode($files);

0 comments on commit d03de20

Please sign in to comment.