Merge pull request #26396 from nextcloud/bugfix/noid/ro-root-template
Get the parent directory before creating a file from a template
This commit is contained in:
commit
507facda8b
|
@ -27,6 +27,7 @@
|
||||||
namespace OC\DirectEditing;
|
namespace OC\DirectEditing;
|
||||||
|
|
||||||
use Doctrine\DBAL\FetchMode;
|
use Doctrine\DBAL\FetchMode;
|
||||||
|
use OC\Files\Node\Folder;
|
||||||
use OCP\AppFramework\Http\NotFoundResponse;
|
use OCP\AppFramework\Http\NotFoundResponse;
|
||||||
use OCP\AppFramework\Http\Response;
|
use OCP\AppFramework\Http\Response;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
|
@ -130,7 +131,12 @@ class Manager implements IManager {
|
||||||
if ($userFolder->nodeExists($path)) {
|
if ($userFolder->nodeExists($path)) {
|
||||||
throw new \RuntimeException('File already exists');
|
throw new \RuntimeException('File already exists');
|
||||||
} else {
|
} else {
|
||||||
$file = $userFolder->newFile($path);
|
if (!$userFolder->nodeExists(dirname($path))) {
|
||||||
|
throw new \RuntimeException('Invalid path');
|
||||||
|
}
|
||||||
|
/** @var Folder $folder */
|
||||||
|
$folder = $userFolder->get(dirname($path));
|
||||||
|
$file = $folder->newFile(basename($path));
|
||||||
$editor = $this->getEditor($editorId);
|
$editor = $this->getEditor($editorId);
|
||||||
$creators = $editor->getCreators();
|
$creators = $editor->getCreators();
|
||||||
foreach ($creators as $creator) {
|
foreach ($creators as $creator) {
|
||||||
|
|
|
@ -36,7 +36,6 @@ use OCP\Files\GenericFileException;
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\IRootFolder;
|
||||||
use OCP\Files\Node;
|
use OCP\Files\Node;
|
||||||
use OCP\Files\NotFoundException;
|
use OCP\Files\NotFoundException;
|
||||||
use OCP\Files\NotPermittedException;
|
|
||||||
use OCP\Files\Template\FileCreatedFromTemplateEvent;
|
use OCP\Files\Template\FileCreatedFromTemplateEvent;
|
||||||
use OCP\Files\Template\ICustomTemplateProvider;
|
use OCP\Files\Template\ICustomTemplateProvider;
|
||||||
use OCP\Files\Template\ITemplateManager;
|
use OCP\Files\Template\ITemplateManager;
|
||||||
|
@ -155,7 +154,11 @@ class TemplateManager implements ITemplateManager {
|
||||||
} catch (NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$targetFile = $userFolder->newFile($filePath);
|
if (!$userFolder->nodeExists(dirname($filePath))) {
|
||||||
|
throw new GenericFileException($this->l10n->t('Invalid path'));
|
||||||
|
}
|
||||||
|
$folder = $userFolder->get(dirname($filePath));
|
||||||
|
$targetFile = $folder->newFile(basename($filePath));
|
||||||
if ($templateType === 'user' && $templateId !== '') {
|
if ($templateType === 'user' && $templateId !== '') {
|
||||||
$template = $userFolder->get($templateId);
|
$template = $userFolder->get($templateId);
|
||||||
$template->copy($targetFile->getPath());
|
$template->copy($targetFile->getPath());
|
||||||
|
@ -296,9 +299,10 @@ class TemplateManager implements ITemplateManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$folder = $userFolder->newFolder($userTemplatePath);
|
|
||||||
} catch (NotPermittedException $e) {
|
|
||||||
$folder = $userFolder->get($userTemplatePath);
|
$folder = $userFolder->get($userTemplatePath);
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
$folder = $userFolder->get(dirname($userTemplatePath));
|
||||||
|
$folder = $folder->newFolder(basename($userTemplatePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$folderIsEmpty = count($folder->getDirectoryListing()) === 0;
|
$folderIsEmpty = count($folder->getDirectoryListing()) === 0;
|
||||||
|
|
|
@ -154,11 +154,16 @@ class ManagerTest extends TestCase {
|
||||||
$this->random->expects($this->once())
|
$this->random->expects($this->once())
|
||||||
->method('generate')
|
->method('generate')
|
||||||
->willReturn($expectedToken);
|
->willReturn($expectedToken);
|
||||||
|
$folder = $this->createMock(Folder::class);
|
||||||
$this->userFolder
|
$this->userFolder
|
||||||
->method('nodeExists')
|
->method('nodeExists')
|
||||||
->with('/File.txt')
|
->withConsecutive(['/File.txt'], ['/'])
|
||||||
->willReturn(false);
|
->willReturnOnConsecutiveCalls(false, true);
|
||||||
$this->userFolder->expects($this->once())
|
$this->userFolder
|
||||||
|
->method('get')
|
||||||
|
->with('/')
|
||||||
|
->willReturn($folder);
|
||||||
|
$folder->expects($this->once())
|
||||||
->method('newFile')
|
->method('newFile')
|
||||||
->willReturn($file);
|
->willReturn($file);
|
||||||
$token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
$token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
||||||
|
@ -174,11 +179,16 @@ class ManagerTest extends TestCase {
|
||||||
$this->random->expects($this->once())
|
$this->random->expects($this->once())
|
||||||
->method('generate')
|
->method('generate')
|
||||||
->willReturn($expectedToken);
|
->willReturn($expectedToken);
|
||||||
|
$folder = $this->createMock(Folder::class);
|
||||||
$this->userFolder
|
$this->userFolder
|
||||||
->method('nodeExists')
|
->method('nodeExists')
|
||||||
->with('/File.txt')
|
->withConsecutive(['/File.txt'], ['/'])
|
||||||
->willReturn(false);
|
->willReturnOnConsecutiveCalls(false, true);
|
||||||
$this->userFolder->expects($this->once())
|
$this->userFolder
|
||||||
|
->method('get')
|
||||||
|
->with('/')
|
||||||
|
->willReturn($folder);
|
||||||
|
$folder->expects($this->once())
|
||||||
->method('newFile')
|
->method('newFile')
|
||||||
->willReturn($file);
|
->willReturn($file);
|
||||||
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
||||||
|
|
Loading…
Reference in New Issue