]> source.dussan.org Git - nextcloud-server.git/commitdiff
Get the parent directory before creating a file from a template
authorJulius Härtl <jus@bitgrid.net>
Wed, 31 Mar 2021 13:44:47 +0000 (15:44 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Thu, 1 Apr 2021 13:31:55 +0000 (13:31 +0000)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/DirectEditing/Manager.php
lib/private/Files/Template/TemplateManager.php
tests/lib/DirectEditing/ManagerTest.php

index 0e7e988eef2957aa638e98bcc338f601f4ee868a..1ea09e74d5943ebb79f4b383ccbacc358d7f970d 100644 (file)
@@ -27,6 +27,7 @@
 namespace OC\DirectEditing;
 
 use Doctrine\DBAL\FetchMode;
+use OC\Files\Node\Folder;
 use OCP\AppFramework\Http\NotFoundResponse;
 use OCP\AppFramework\Http\Response;
 use OCP\AppFramework\Http\TemplateResponse;
@@ -130,7 +131,12 @@ class Manager implements IManager {
                if ($userFolder->nodeExists($path)) {
                        throw new \RuntimeException('File already exists');
                } 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);
                        $creators = $editor->getCreators();
                        foreach ($creators as $creator) {
index 44e1b10fa357bd8497f84e3af0c11f72c15c93cd..a81851b2757c087dd42277c4b87aeed15be6ebe6 100644 (file)
@@ -154,7 +154,11 @@ class TemplateManager implements ITemplateManager {
                } catch (NotFoundException $e) {
                }
                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 !== '') {
                                $template = $userFolder->get($templateId);
                                $template->copy($targetFile->getPath());
index 73bb4a836d80e62475ecac040d2f802b2e4f31cf..b00de02bcf520fa4b256c0ff38e60b25ef7d8cb3 100644 (file)
@@ -154,11 +154,16 @@ class ManagerTest extends TestCase {
                $this->random->expects($this->once())
                        ->method('generate')
                        ->willReturn($expectedToken);
+               $folder = $this->createMock(Folder::class);
                $this->userFolder
                        ->method('nodeExists')
-                       ->with('/File.txt')
-                       ->willReturn(false);
-               $this->userFolder->expects($this->once())
+                       ->withConsecutive(['/File.txt'], ['/'])
+                       ->willReturnOnConsecutiveCalls(false, true);
+               $this->userFolder
+                       ->method('get')
+                       ->with('/')
+                       ->willReturn($folder);
+               $folder->expects($this->once())
                        ->method('newFile')
                        ->willReturn($file);
                $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
@@ -174,11 +179,16 @@ class ManagerTest extends TestCase {
                $this->random->expects($this->once())
                        ->method('generate')
                        ->willReturn($expectedToken);
+               $folder = $this->createMock(Folder::class);
                $this->userFolder
                        ->method('nodeExists')
-                       ->with('/File.txt')
-                       ->willReturn(false);
-               $this->userFolder->expects($this->once())
+                       ->withConsecutive(['/File.txt'], ['/'])
+                       ->willReturnOnConsecutiveCalls(false, true);
+               $this->userFolder
+                       ->method('get')
+                       ->with('/')
+                       ->willReturn($folder);
+               $folder->expects($this->once())
                        ->method('newFile')
                        ->willReturn($file);
                $this->manager->create('/File.txt', 'testeditor', 'createEmpty');