Improve join_paths helper method (#1668)

This commit is contained in:
Boy132 2025-09-08 09:03:23 +02:00 committed by GitHub
parent 47557021fd
commit 32eb1abd4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -193,10 +193,10 @@ class ListFiles extends ListRecords
->required()
->live(),
Placeholder::make('new_location')
->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) {
$location = rtrim($data['location'], '/');
$location = $data['location'];
$files = [['to' => join_paths($location, $file->name), 'from' => $file->name]];
$this->getDaemonFileRepository()->renameFiles($this->path, $files);
@ -353,10 +353,10 @@ class ListFiles extends ListRecords
->required()
->live(),
Placeholder::make('new_location')
->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) {
$location = rtrim($data['location'], '/');
$location = $data['location'];
$files = $files->map(fn ($file) => ['to' => join_paths($location, $file['name']), 'from' => $file['name']])->toArray();
$this->getDaemonFileRepository()->renameFiles($this->path, $files);
@ -432,11 +432,12 @@ class ListFiles extends ListRecords
->modalSubmitActionLabel(trans('server/file.actions.new_file.create'))
->action(function ($data) {
$path = join_paths($this->path, $data['name']);
try {
$this->getDaemonFileRepository()->putContent($path, $data['editor'] ?? '');
Activity::event('server:file.write')
->property('file', join_paths($path, $data['name']))
->property('file', $path)
->log();
} catch (FileExistsException) {
AlertBanner::make('file_already_exists')

View File

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