mirror of
https://github.com/brufdev/many-notes.git
synced 2026-01-24 20:09:50 -06:00
Fix name collision while importing vaults or files
This commit is contained in:
@@ -27,15 +27,28 @@ class ProcessImportedFile
|
||||
}
|
||||
|
||||
// Find new filename if it already exists
|
||||
$node = $vault->nodes()
|
||||
$nodeExists = $vault->nodes()
|
||||
->where('parent_id', $parent->id)
|
||||
->where('is_file', true)
|
||||
->where('name', 'like', "$name%")
|
||||
->where('name', 'like', "$name")
|
||||
->where('extension', 'md')
|
||||
->orderByDesc('name')
|
||||
->first();
|
||||
if (preg_match('/-(\d+)$/', $node->name, $matches) === 1) {
|
||||
$name .= '-' . ((int) $matches[1] + 1);
|
||||
->exists();
|
||||
if ($nodeExists) {
|
||||
$nodes = array_column(
|
||||
$vault->nodes()
|
||||
->select('name')
|
||||
->where('parent_id', $parent->id)
|
||||
->where('is_file', true)
|
||||
->where('name', 'like', "$name-%")
|
||||
->where('extension', 'md')
|
||||
->get()
|
||||
->toArray(),
|
||||
'name',
|
||||
);
|
||||
natcasesort($nodes);
|
||||
$name .= count($nodes) && preg_match('/-(\d+)$/', end($nodes), $matches) === 1 ?
|
||||
'-' . ((int) $matches[1] + 1) :
|
||||
'-1';
|
||||
}
|
||||
|
||||
$node = $vault->nodes()->createQuietly([
|
||||
|
||||
@@ -18,13 +18,23 @@ class ProcessImportedVault
|
||||
$vaultName = pathinfo($fileName, PATHINFO_FILENAME);
|
||||
|
||||
// Find new vault name if it already exists
|
||||
$vault = auth()->user()
|
||||
$vaultExists = auth()
|
||||
->user()
|
||||
->vaults()
|
||||
->where('name', 'like', "$vaultName%")
|
||||
->orderByDesc('name')
|
||||
->first();
|
||||
if ($vault) {
|
||||
$vaultName .= preg_match('/-(\d+)$/', $vault->name, $matches) === 1 ?
|
||||
->where('name', 'like', "$vaultName")
|
||||
->exists();
|
||||
if ($vaultExists) {
|
||||
$vaults = array_column(
|
||||
auth()->user()
|
||||
->vaults()
|
||||
->select('name')
|
||||
->where('name', 'like', "$vaultName-%")
|
||||
->get()
|
||||
->toArray(),
|
||||
'name',
|
||||
);
|
||||
natcasesort($vaults);
|
||||
$vaultName .= count($vaults) && preg_match('/-(\d+)$/', end($vaults), $matches) === 1 ?
|
||||
'-' . ((int) $matches[1] + 1) :
|
||||
'-1';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user