From e1027fbc4e02a82f280f8b0556b18edebd771206 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Fri, 21 Jul 2023 16:47:20 +0200 Subject: [PATCH] Rename conflicted not published folder/file on creating/uploading --- controllers/EditController.php | 28 +++++++++------------- docs/CHANGELOG.md | 1 + models/File.php | 13 ++++++++++ models/FileSystemItem.php | 7 ++++++ models/Folder.php | 44 ++++++++++++++++++++++++++++++---- 5 files changed, 71 insertions(+), 22 deletions(-) diff --git a/controllers/EditController.php b/controllers/EditController.php index d31b897..1579c20 100644 --- a/controllers/EditController.php +++ b/controllers/EditController.php @@ -8,16 +8,14 @@ namespace humhub\modules\cfiles\controllers; -use humhub\modules\cfiles\Module; -use Yii; -use yii\web\HttpException; +use humhub\modules\cfiles\models\File; use humhub\modules\cfiles\models\FileSystemItem; +use humhub\modules\cfiles\models\Folder; use humhub\modules\cfiles\models\forms\SelectionForm; -use humhub\modules\cfiles\permissions\ManageFiles; -use humhub\modules\cfiles\permissions\WriteAccess; +use humhub\modules\cfiles\Module; use humhub\modules\content\models\Content; -use humhub\modules\cfiles\models\File; -use humhub\modules\cfiles\models\Folder; +use Yii; +use yii\web\HttpException; /** * Description of BrowseController @@ -45,21 +43,17 @@ public function actionFolder($id = null) throw new HttpException(404); } + $post = Yii::$app->request->post(); + // create new folder if no folder was found or folder is not editable. if (!$folder || !($folder instanceof Folder) || !$folder->isEditableFolder()) { - $folderData = Yii::$app->request->post('Folder'); - $existingFolder = $folderData ? $this->getCurrentFolder()->findFolderByName($folderData['title']) : null; - if ($existingFolder && $existingFolder->content->getStateService()->isPublished()) { - $folder = $existingFolder; - $folder->content->getStateService()->set(Content::STATE_PUBLISHED); - } else { - $folder = $this->getCurrentFolder()->newFolder(); - $folder->content->container = $this->contentContainer; - } + $this->getCurrentFolder()->resolveConflictsBeforeCreate($post['Folder']['title'] ?? null); + $folder = $this->getCurrentFolder()->newFolder(); + $folder->content->container = $this->contentContainer; $folder->hidden = $this->module->getContentHiddenDefault($this->contentContainer); } - if ($folder->load(Yii::$app->request->post()) && $folder->save()) { + if ($folder->load($post) && $folder->save()) { $this->view->saved(); return $this->htmlRedirect($folder->createUrl('/cfiles/browse/index')); } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a24f260..2acf020 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,7 @@ Changelog - Enh #178: Use new content state service - Fix #180: Use icon `fa-unlock` for public files - Fix #184: Display only published content files in the folder "Files from the stream" +- Fix #186: Rename conflicted not published folder/file on creating/uploading 0.16.1 - May 1, 2023 -------------------- diff --git a/models/File.php b/models/File.php index c1c9458..613f97b 100644 --- a/models/File.php +++ b/models/File.php @@ -496,4 +496,17 @@ public function getDeleteVersionUrl(int $versionId): ?string ]); } + /** + * @inheritdoc + */ + public function renameConflicted(): bool + { + if ($this->isNewRecord) { + return false; + } + + $this->baseFile->file_name = 'conflict' . $this->baseFile->id . '-' . $this->baseFile->file_name; + + return $this->baseFile->save(); + } } diff --git a/models/FileSystemItem.php b/models/FileSystemItem.php index 3e76d8c..b33e3a9 100644 --- a/models/FileSystemItem.php +++ b/models/FileSystemItem.php @@ -80,6 +80,13 @@ abstract public function getVersionsUrl(int $versionId = 0): ?string; */ abstract public function getDeleteVersionUrl(int $versionId): ?string; + /** + * Rename a conflicted Item with same name + * + * @return bool + */ + abstract public function renameConflicted(): bool; + /** * @inheritdoc */ diff --git a/models/Folder.php b/models/Folder.php index 9478744..82ff9c8 100644 --- a/models/Folder.php +++ b/models/Folder.php @@ -755,10 +755,6 @@ public function addUploadedFile(UploadedFile $uploadedFile): File // Get file instance either an existing one or a new one $file = $this->getFileInstance($uploadedFile); - if ($file->content->getStateService()->isDeleted()) { - $file->content->getStateService()->publish(); - } - if ($file->setUploadedFile($uploadedFile)) { $file->save(); } @@ -773,7 +769,12 @@ public function addUploadedFile(UploadedFile $uploadedFile): File private function getFileInstance(UploadedFile $uploadedFile): File { if ($file = $this->findFileByName($uploadedFile->name)) { - return $file; + if ($file->content->getStateService()->isPublished()) { + // Allow to use only Published file with same name + return $file; + } + // otherwise old not published file must be renamed in order to avoid conflicts + $file->renameConflicted(); } return new File($this->content->container, $this->getNewItemVisibility(), [ @@ -1000,4 +1001,37 @@ public function getDeleteVersionUrl(int $versionId): ?string return null; } + /** + * @inheritdoc + */ + public function renameConflicted(): bool + { + if ($this->isNewRecord) { + return false; + } + + $this->title = 'conflict' . $this->id . '-' . $this->title; + + return $this->save(); + } + + /** + * Resolve conflicts before creating new folder + * + * @param string|null $title + */ + public function resolveConflictsBeforeCreate(?string $title = null) + { + if ($title === null || $title === '') { + return; + } + + $duplicatedFolder = $this->findFolderByName($title); + + if ($duplicatedFolder && !$duplicatedFolder->content->getStateService()->isPublished()) { + // Rename already existing Folder with same name but not Published(it maybe soft deleted), + // It is useful to avoid a conflict on creating a new Folder with same name + $duplicatedFolder->renameConflicted(); + } + } }