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

Rename conflicted not published folder/file on creating/uploading #186

Merged
merged 1 commit into from
Jul 21, 2023
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
28 changes: 11 additions & 17 deletions controllers/EditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'));
}
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------
Expand Down
13 changes: 13 additions & 0 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
7 changes: 7 additions & 0 deletions models/FileSystemItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
44 changes: 39 additions & 5 deletions models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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(), [
Expand Down Expand Up @@ -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();
}
}
}