improve join_paths helper method

This commit is contained in:
Boy132 2025-09-05 13:45:02 +02:00
parent 925ab26fb4
commit 8c6490bc2c
2 changed files with 8 additions and 5 deletions

View File

@ -196,7 +196,7 @@ class ListFiles extends ListRecords
->content(fn (Get $get, File $file) => resolve_path('./' . join_paths($this->path, $get('location') ?? '/', $file->name))), ->content(fn (Get $get, File $file) => resolve_path('./' . join_paths($this->path, $get('location') ?? '/', $file->name))),
]) ])
->action(function ($data, File $file) { ->action(function ($data, File $file) {
$location = rtrim($data['location'], '/'); $location = $data['location'];
$files = [['to' => join_paths($location, $file->name), 'from' => $file->name]]; $files = [['to' => join_paths($location, $file->name), 'from' => $file->name]];
$this->getDaemonFileRepository()->renameFiles($this->path, $files); $this->getDaemonFileRepository()->renameFiles($this->path, $files);
@ -356,7 +356,7 @@ class ListFiles extends ListRecords
->content(fn (Get $get) => resolve_path('./' . join_paths($this->path, $get('location') ?? ''))), ->content(fn (Get $get) => resolve_path('./' . join_paths($this->path, $get('location') ?? ''))),
]) ])
->action(function (Collection $files, $data) { ->action(function (Collection $files, $data) {
$location = rtrim($data['location'], '/'); $location = $data['location'];
$files = $files->map(fn ($file) => ['to' => join_paths($location, $file['name']), 'from' => $file['name']])->toArray(); $files = $files->map(fn ($file) => ['to' => join_paths($location, $file['name']), 'from' => $file['name']])->toArray();
$this->getDaemonFileRepository()->renameFiles($this->path, $files); $this->getDaemonFileRepository()->renameFiles($this->path, $files);

View File

@ -52,11 +52,14 @@ if (!function_exists('convert_bytes_to_readable')) {
if (!function_exists('join_paths')) { if (!function_exists('join_paths')) {
function join_paths(string $base, string ...$paths): string function join_paths(string $base, string ...$paths): string
{ {
if ($base === '/') { $paths = array_filter($paths, fn (string $path) => strlen($path) > 0);
return str_replace('//', '', implode('/', $paths)); $paths = array_map(fn (string $path) => trim($path, '/'), $paths);
if (empty($base) || $base === '/') {
return implode('/', $paths);
} }
return str_replace('//', '', $base . '/' . implode('/', $paths)); return $base . '/' . implode('/', $paths);
} }
} }