Refactor code

This commit is contained in:
brufdev
2025-02-11 21:15:50 +00:00
parent 6c179b995e
commit 87a2ef8b3b
4 changed files with 28 additions and 34 deletions

View File

@@ -22,18 +22,15 @@ final readonly class CreateVault
if ($vaultExists) {
/** @var list<string> $vaults */
$vaults = array_column(
$user->vaults()
->select('name')
->where('name', 'like', $attributes['name'] . '-%')
->get()
->toArray(),
'name',
);
$vaults = $user->vaults()
->select('name')
->where('name', 'like', $attributes['name'] . '-%')
->pluck('name')
->toArray();
natcasesort($vaults);
$attributes['name'] .= count($vaults) && preg_match('/-(\d+)$/', end($vaults), $matches) === 1 ?
'-' . ((int) $matches[1] + 1) :
'-1';
$attributes['name'] .= count($vaults) && preg_match('/-(\d+)$/', end($vaults), $matches) === 1
? '-' . ((int) $matches[1] + 1)
: '-1';
}
// Save vault to database

View File

@@ -35,17 +35,14 @@ final readonly class CreateVaultNode
if ($nodeExists) {
/** @var list<string> $nodes */
$nodes = array_column(
$vault->nodes()
->select('name')
->where('parent_id', $attributes['parent_id'])
->where('is_file', $attributes['is_file'])
->where('name', 'like', $attributes['name'] . '-%')
->where('extension', $attributes['extension'])
->get()
->toArray(),
'name',
);
$nodes = $vault->nodes()
->select('name')
->where('parent_id', $attributes['parent_id'])
->where('is_file', $attributes['is_file'])
->where('name', 'like', $attributes['name'] . '-%')
->where('extension', $attributes['extension'])
->pluck('name')
->toArray();
natcasesort($nodes);
$attributes['name'] .= count($nodes) && preg_match('/-(\d+)$/', end($nodes), $matches) === 1
? '-' . ((int) $matches[1] + 1)

View File

@@ -55,23 +55,21 @@ final readonly class ExportVault
);
}
if ($node->is_file) {
if ($node->extension === 'md') {
$zip->addFromString($nodePath, (string) $node->content);
} else {
$relativePath = new GetPathFromVaultNode()->handle($node);
$zip->addFile(
Storage::disk('local')->path($relativePath),
$nodePath,
);
}
} else {
if (!$node->is_file) {
$zip->addEmptyDir($nodePath);
if ($node->children()->count()) {
$this->exportNodes($zip, $node->children()->get(), $nodePath);
}
} elseif ($node->extension === 'md') {
$zip->addFromString($nodePath, (string) $node->content);
} else {
$relativePath = new GetPathFromVaultNode()->handle($node);
$zip->addFile(
Storage::disk('local')->path($relativePath),
$nodePath,
);
}
}
}

View File

@@ -22,10 +22,12 @@ final readonly class ProcessImportedFile
$attributes['name'] = $pathInfo['filename'];
$attributes['extension'] = $pathInfo['extension'] ?? '';
$attributes['content'] = null;
if (in_array($attributes['extension'], Note::extensions())) {
$attributes['extension'] = 'md';
$attributes['content'] = (string) file_get_contents($filePath);
}
$node = new CreateVaultNode()->handle($vault, $attributes);
if ($node->extension === 'md') {