mirror of
https://github.com/brufdev/many-notes.git
synced 2026-01-24 20:09:50 -06:00
Refactor code
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user