Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable21] Get the parent directory before creating a file from a template #26406

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/private/DirectEditing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,7 +131,12 @@ public function create(string $path, string $editorId, string $creatorId, $templ
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) {
Expand Down
12 changes: 8 additions & 4 deletions lib/private/Files/Template/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Template\FileCreatedFromTemplateEvent;
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\Files\Template\ITemplateManager;
Expand Down Expand Up @@ -154,7 +153,11 @@ public function createFromTemplate(string $filePath, string $templateId = '', st
} 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());
Expand Down Expand Up @@ -295,9 +298,10 @@ public function initializeTemplateDirectory(string $path = null, string $userId
}

try {
$folder = $userFolder->newFolder($userTemplatePath);
} catch (NotPermittedException $e) {
$folder = $userFolder->get($userTemplatePath);
} catch (NotFoundException $e) {
$folder = $userFolder->get(dirname($userTemplatePath));
$folder = $folder->newFolder(basename($userTemplatePath));
}

$folderIsEmpty = count($folder->getDirectoryListing()) === 0;
Expand Down
22 changes: 16 additions & 6 deletions tests/lib/DirectEditing/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,16 @@ public function testCreateToken() {
$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');
Expand All @@ -174,11 +179,16 @@ public function testCreateTokenAccess() {
$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');
Expand Down