CHeck if file already exists during file creation
Signed-off-by: Julius Härtl <jus@bitgrid.net> Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
edd957140a
commit
ab4b9a6df5
|
@ -123,15 +123,21 @@ class Manager implements IManager {
|
||||||
|
|
||||||
public function create(string $path, string $editorId, string $creatorId, $templateId = null): string {
|
public function create(string $path, string $editorId, string $creatorId, $templateId = null): string {
|
||||||
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
||||||
$file = $userFolder->newFile($path);
|
try {
|
||||||
$editor = $this->getEditor($editorId);
|
$file = $userFolder->get($path);
|
||||||
$creators = $editor->getCreators();
|
throw new \RuntimeException('File already exists');
|
||||||
foreach ($creators as $creator) {
|
} catch (\OCP\Files\NotFoundException $e) {
|
||||||
if ($creator->getId() === $creatorId) {
|
$file = $userFolder->newFile($path);
|
||||||
$creator->create($file, $creatorId, $templateId);
|
$editor = $this->getEditor($editorId);
|
||||||
return $this->createToken($editorId, $file, $path);
|
$creators = $editor->getCreators();
|
||||||
|
foreach ($creators as $creator) {
|
||||||
|
if ($creator->getId() === $creatorId) {
|
||||||
|
$creator->create($file, $creatorId, $templateId);
|
||||||
|
return $this->createToken($editorId, $file, $path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException('No creator found');
|
throw new \RuntimeException('No creator found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ use OCP\DirectEditing\IEditor;
|
||||||
use OCP\DirectEditing\IToken;
|
use OCP\DirectEditing\IToken;
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\IRootFolder;
|
||||||
|
use OCP\Files\NotFoundException;
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
use OCP\IL10N;
|
use OCP\IL10N;
|
||||||
use OCP\IUserSession;
|
use OCP\IUserSession;
|
||||||
|
@ -151,6 +152,10 @@ class ManagerTest extends TestCase {
|
||||||
$this->random->expects($this->once())
|
$this->random->expects($this->once())
|
||||||
->method('generate')
|
->method('generate')
|
||||||
->willReturn($expectedToken);
|
->willReturn($expectedToken);
|
||||||
|
$this->userFolder
|
||||||
|
->method('get')
|
||||||
|
->with('/File.txt')
|
||||||
|
->willThrowException(new NotFoundException());
|
||||||
$this->userFolder->expects($this->once())
|
$this->userFolder->expects($this->once())
|
||||||
->method('newFile')
|
->method('newFile')
|
||||||
->willReturn($file);
|
->willReturn($file);
|
||||||
|
@ -167,6 +172,10 @@ class ManagerTest extends TestCase {
|
||||||
$this->random->expects($this->once())
|
$this->random->expects($this->once())
|
||||||
->method('generate')
|
->method('generate')
|
||||||
->willReturn($expectedToken);
|
->willReturn($expectedToken);
|
||||||
|
$this->userFolder
|
||||||
|
->method('get')
|
||||||
|
->with('/File.txt')
|
||||||
|
->willThrowException(new NotFoundException());
|
||||||
$this->userFolder->expects($this->once())
|
$this->userFolder->expects($this->once())
|
||||||
->method('newFile')
|
->method('newFile')
|
||||||
->willReturn($file);
|
->willReturn($file);
|
||||||
|
@ -177,4 +186,10 @@ class ManagerTest extends TestCase {
|
||||||
$this->assertInstanceOf(NotFoundResponse::class, $secondResult);
|
$this->assertInstanceOf(NotFoundResponse::class, $secondResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCreateFileAlreadyExists() {
|
||||||
|
$this->expectException(\RuntimeException::class);
|
||||||
|
|
||||||
|
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue