From bbec441b051a907cd906916e233a90d62dc5efc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 1 Oct 2020 08:20:08 +0200 Subject: [PATCH] Do not setup appdata in constructor to avoid errors causing the whole instance to stop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Service/DocumentService.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 73eb536887f..ccccfae5949 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -108,11 +108,6 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp $this->logger = $logger; $this->shareManager = $shareManager; $this->lockingProvider = $lockingProvider; - try { - $this->appData->getFolder('documents'); - } catch (NotFoundException $e) { - $this->appData->newFolder('documents'); - } $token = $request->getParam('token'); if ($this->userId === null && $token !== null) { @@ -149,8 +144,12 @@ public function createDocument(File $file): Document { } catch (NotFoundException $e) { } + if (!$this->ensureDocumentsFolder()) { + throw new NotFoundException('No app data folder present for text documents'); + } + try { - $documentBaseFile = $this->appData->getFolder('documents')->getFile((string)$file->getFileInfo()->getId()); + $documentBaseFile = $this->getBaseFile((string)$file->getFileInfo()->getId()); } catch (NotFoundException $e) { $documentBaseFile = $this->appData->getFolder('documents')->newFile((string)$file->getFileInfo()->getId()); } @@ -174,6 +173,9 @@ public function createDocument(File $file): Document { * @throws NotFoundException */ public function getBaseFile($document): ISimpleFile { + if (!$this->ensureDocumentsFolder()) { + throw new NotFoundException('No app data folder present for text documents'); + } return $this->appData->getFolder('documents')->getFile((string) $document); } @@ -343,7 +345,7 @@ public function resetDocument($documentId, $force = false): void { $this->documentMapper->delete($document); try { - $this->appData->getFolder('documents')->getFile((string)$documentId)->delete(); + $this->getBaseFile($documentId)->delete(); } catch (NotFoundException $e) { } catch (NotPermittedException $e) { } @@ -457,4 +459,18 @@ public function hasUnsavedChanges(Document $document) { return $document->getCurrentVersion() !== $document->getLastSavedVersion(); } + private function ensureDocumentsFolder(): bool { + try { + $this->appData->getFolder('documents'); + } catch (NotFoundException $e) { + $this->appData->newFolder('documents'); + } catch (\RuntimeException $e) { + // Do not fail hard + $this->logger->logException($e); + return false; + } + + return true; + } + }