Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulien Veyssier <eneiluj@posteo.net>2021-12-28 14:31:21 +0300
committerJulien Veyssier <eneiluj@posteo.net>2022-01-03 12:27:38 +0300
commit9b1c396d9b5257b841c4870e974ed08c7a6679d5 (patch)
tree11d9e0f7a468da814d3ebfeb29b537431023c870 /lib
parentdc710f7e99ebf7695504dfa3f66000ecdcb93560 (diff)
factorize getAttachmentDirectoryForFile and getOrCreateAttachmentDirectoryForFile
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/Service/ImageService.php61
1 files changed, 20 insertions, 41 deletions
diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php
index 8ba791a05..55f4582a0 100644
--- a/lib/Service/ImageService.php
+++ b/lib/Service/ImageService.php
@@ -117,11 +117,12 @@ class ImageService {
* @param File $textFile
* @return File|\OCP\Files\Node|ISimpleFile|null
* @throws NotFoundException
+ * @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
- * @throws \OCP\Files\NotPermittedException
+ * @throws \OC\User\NoUserException
*/
private function getImagePreview(string $imageFileName, File $textFile) {
- $attachmentFolder = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
if ($attachmentFolder !== null) {
try {
$imageFile = $attachmentFolder->get($imageFileName);
@@ -148,15 +149,16 @@ class ImageService {
* @param string $userId
* @return array
* @throws NotFoundException
+ * @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
- * @throws \OCP\Files\NotPermittedException
+ * @throws \OC\User\NoUserException
*/
public function uploadImage(int $documentId, string $newFileName, string $newFileContent, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
}
- $saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
if ($saveDir !== null) {
$fileName = (string) time() . '-' . $newFileName;
$savedFile = $saveDir->newFile($fileName, $newFileContent);
@@ -188,7 +190,7 @@ class ImageService {
throw new NotPermittedException('No write permissions');
}
$textFile = $this->getTextFilePublic($documentId, $shareToken);
- $saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
if ($saveDir !== null) {
$fileName = (string) time() . '-' . $newFileName;
$savedFile = $saveDir->newFile($fileName, $newFileContent);
@@ -212,9 +214,9 @@ class ImageService {
* @param string $userId
* @return array
* @throws NotFoundException
+ * @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
- * @throws \OCP\Files\NotPermittedException
- * @throws \OCP\Lock\LockedException
+ * @throws \OC\User\NoUserException
*/
public function insertImageFile(int $documentId, string $path, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
@@ -222,7 +224,7 @@ class ImageService {
throw new NotPermittedException('No write permissions');
}
$imageFile = $this->getFileFromPath($path, $userId);
- $saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
if ($saveDir !== null) {
return $this->copyImageFile($imageFile, $saveDir, $textFile);
} else {
@@ -266,15 +268,17 @@ class ImageService {
* @param string $userId
* @return array
* @throws NotFoundException
+ * @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
- * @throws \OCP\Files\NotPermittedException
+ * @throws \OCP\Lock\LockedException
+ * @throws \OC\User\NoUserException
*/
public function insertImageLink(int $documentId, string $link, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
}
- $saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
if ($saveDir !== null) {
return $this->downloadLink($saveDir, $link, $textFile);
} else {
@@ -298,7 +302,7 @@ class ImageService {
throw new NotPermittedException('No write permissions');
}
$textFile = $this->getTextFilePublic($documentId, $shareToken);
- $saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
+ $saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
if ($saveDir !== null) {
return $this->downloadLink($saveDir, $link, $textFile);
} else {
@@ -384,41 +388,14 @@ class ImageService {
* Get or create file-specific attachment folder
*
* @param File $textFile
- * @return Folder|null
- * @throws NotFoundException
- * @throws \OCP\Files\InvalidPathException
- * @throws \OCP\Files\NotPermittedException
- */
- private function getOrCreateAttachmentDirectoryForFile(File $textFile): ?Folder {
- $owner = $textFile->getOwner();
- $ownerId = $owner->getUID();
- $ownerUserFolder = $this->rootFolder->getUserFolder($ownerId);
- $ownerTextFile = $ownerUserFolder->getById($textFile->getId());
- if (count($ownerTextFile) > 0) {
- $ownerTextFile = $ownerTextFile[0];
- $ownerParentFolder = $ownerTextFile->getParent();
- $attachmentFolderName = '.attachments.' . $textFile->getId();
- if ($ownerParentFolder->nodeExists($attachmentFolderName)) {
- $attachmentFolder = $ownerParentFolder->get($attachmentFolderName);
- if ($attachmentFolder instanceof Folder) {
- return $attachmentFolder;
- }
- } else {
- return $ownerParentFolder->newFolder($attachmentFolderName);
- }
- }
- return null;
- }
-
- /**
- * @param File $textFile
+ * @param bool $create
* @return Folder|null
* @throws NotFoundException
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
* @throws \OC\User\NoUserException
*/
- private function getAttachmentDirectoryForFile(File $textFile): ?Folder {
+ private function getAttachmentDirectoryForFile(File $textFile, bool $create = false): ?Folder {
$owner = $textFile->getOwner();
$ownerId = $owner->getUID();
$ownerUserFolder = $this->rootFolder->getUserFolder($ownerId);
@@ -432,6 +409,8 @@ class ImageService {
if ($attachmentFolder instanceof Folder) {
return $attachmentFolder;
}
+ } elseif ($create) {
+ return $ownerParentFolder->newFolder($attachmentFolderName);
}
}
return null;
@@ -667,7 +646,7 @@ class ImageService {
$sourceAttachmentDir = $this->getAttachmentDirectoryForFile($source);
if ($sourceAttachmentDir !== null) {
// create a new attachment dir next to the new file
- $targetAttachmentDir = $this->getOrCreateAttachmentDirectoryForFile($target);
+ $targetAttachmentDir = $this->getAttachmentDirectoryForFile($target, true);
// copy the attachment files
foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) {
if ($sourceAttachment instanceof File) {