Skip to content

Commit

Permalink
#8933 Restore original file after cancelling file upload wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Apr 24, 2023
1 parent bd61fc9 commit fc94ba2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 13 deletions.
49 changes: 48 additions & 1 deletion controllers/api/file/PKPManageFileApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace PKP\controllers\api\file;

use APP\core\Application;
use APP\core\Request;
use APP\core\Services;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\notification\NotificationManager;
Expand All @@ -40,7 +42,7 @@ public function __construct()
parent::__construct();
$this->addRoleAssignment(
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_ASSISTANT, Role::ROLE_ID_REVIEWER, Role::ROLE_ID_AUTHOR],
['deleteFile', 'editMetadata', 'editMetadataTab', 'saveMetadata']
['deleteFile', 'editMetadata', 'editMetadataTab', 'saveMetadata', 'cancelFileUpload']
);
}

Expand Down Expand Up @@ -88,6 +90,51 @@ public function deleteFile($args, $request)
return \PKP\db\DAO::getDataChangedEvent();
}

/**
* Restore original file when cancelling the upload wizard
*/
public function cancelFileUpload(array $args, Request $request): JSONMessage
{
if (!$request->checkCSRF()) {
return new JSONMessage(false);
}

$submissionFile = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION_FILE);
$originalFile = $request->getUserVar('originalFile') ? (array) $request->getUserVar('originalFile') : null;
$revisedFileId = $request->getUserVar('fileId') ? (int) $request->getUserVar('fileId') : null;

// Get revisions and check file IDs
$revisions = Repo::submissionFile()->getRevisions($submissionFile->getId());
$revisionIds = [];
foreach ($revisions as $revision) {
$revisionIds[] = $revision->fileId;
}

if (!$revisedFileId && !in_array($revisedFileId, $revisionIds)) {
return new JSONMessage(false);
}

if (!isset($originalFile['fileId']) && !in_array($originalFile['fileId'], $revisionIds)) {
return new JSONMessage(false);
}

// Restore original submission file
Repo::submissionFile()->edit(
$submissionFile,
[
'fileId' => $originalFile['fileId'],
'name' => $originalFile['name'],
'uploaderUserId' => $originalFile['uploaderUserId'],
]
);

// Remove uploaded file
Services::get('file')->delete($revisedFileId);

$this->setupTemplate($request);
return \PKP\db\DAO::getDataChangedEvent();
}

/**
* Edit submission file metadata modal.
*
Expand Down
24 changes: 19 additions & 5 deletions controllers/wizard/fileUpload/FileUploadWizardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
use PKP\security\authorization\SubmissionFileAccessPolicy;
use PKP\security\authorization\WorkflowStageAccessPolicy;
use PKP\security\Role;

use PKP\security\Validation;
use PKP\submission\GenreDAO;
use PKP\submissionFile\SubmissionFile;

Expand Down Expand Up @@ -418,13 +416,19 @@ public function uploadFile($args, $request)
return new JSONMessage(false, $uploadForm->fetch($request));
}

$submissionFileId = $uploadForm->getRevisedFileId();
// Store the data of the submission file before it's replaced by the revised file
if ($submissionFileId) {
$originalFile = Repo::submissionFile()->get($submissionFileId);
}

$uploadedFile = $uploadForm->execute(); /** @var SubmissionFile $uploadedFile */
if (!is_a($uploadedFile, 'SubmissionFile')) {
return new JSONMessage(false, __('common.uploadFailed'));
}

// Retrieve file info to be used in a JSON response.
$uploadedFileInfo = $this->_getUploadedFileInfo($uploadedFile);
$uploadedFileInfo = $this->_getUploadedFileInfo($uploadedFile, $originalFile ?? null);
$reviewRound = $this->getReviewRound();

// Advance to the next step (i.e. meta-data editing).
Expand Down Expand Up @@ -552,15 +556,25 @@ public function _onlyNumbersDiffer($a, $b)
*
* @return array
*/
public function _getUploadedFileInfo($uploadedFile)
public function _getUploadedFileInfo(SubmissionFile $uploadedFile, ?SubmissionFile $originalFile = null)
{
return [
$uploadedFile = [
'uploadedFile' => [
'id' => $uploadedFile->getId(),
'fileId' => $uploadedFile->getData('fileId'),
'name' => $uploadedFile->getLocalizedData('name'),
'genreId' => $uploadedFile->getGenreId(),
]
];

if ($originalFile) {
$uploadedFile['uploadedFile']['originalFile'] = [
'fileId' => $originalFile->getData('fileId'),
'name' => $originalFile->getData('name'),
'uploaderUserId' => $originalFile->getData('uploaderUserId'),
];
}

return $uploadedFile;
}
}
34 changes: 28 additions & 6 deletions js/controllers/wizard/fileUpload/FileUploadWizardHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
this.deleteUrl_ = options.deleteUrl;
this.metadataUrl_ = options.metadataUrl;
this.finishUrl_ = options.finishUrl;
this.cancelUrl_ = options.cancelUrl;

// Bind events of the nested upload forms.
this.bind('fileUploaded', this.handleFileUploaded);
Expand All @@ -65,7 +66,7 @@


/**
* The URL to be called when a cancel event occurs.
* The URL to be called when a delete event occurs.
* @private
* @type {string}
*/
Expand All @@ -90,6 +91,14 @@
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.finishUrl_ = '';

/**
* he URL to be called when a cancel event occurs.
* @private
* @type {string}
*/
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.cancelUrl_ = '';


/**
* Information about the uploaded file (once there is one).
Expand All @@ -99,7 +108,13 @@
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.uploadedFile_ = null;


/**
* Information about the file being revised.
* @private
* @type {{fileId: number, name: string, uploaderUserId: number}}
*/
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.originalFile_ = null;
//
// Public methods
//
Expand Down Expand Up @@ -243,7 +258,8 @@
this.uploadedFile_.csrfToken = this.csrfToken_;
// Authorization policy expects to find the submissionFileId para
this.uploadedFile_.submissionFileId = this.uploadedFile_.id;
$.post(this.deleteUrl_, this.uploadedFile_,
this.uploadedFile_.originalFile = this.originalFile_;
$.post(this.cancelUrl_, this.uploadedFile_,
$.pkp.classes.Helper.curry(this.wizardCancelSuccess, this,
wizardElement, event), 'json');

Expand Down Expand Up @@ -298,7 +314,13 @@
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.handleFileUploaded = function(callingForm, event, uploadedFile) {

// Save the uploaded file information.
// Keep the original file data to restore if the wizard is canceled
if (this.originalFile_ === null) {
this.originalFile_ = uploadedFile.originalFile;
}
delete uploadedFile.originalFile;

// Save the uploaded file information
this.uploadedFile_ = uploadedFile;
};

Expand Down Expand Up @@ -348,8 +370,8 @@
$.pkp.controllers.wizard.fileUpload.FileUploadWizardHandler.
prototype.startWizard = function() {

// Reset the uploaded file.
this.uploadedFile_ = null;
// Reset the uploaded and original file.
this.uploadedFile_ = this.originalFile_= null;

this.parent('startWizard');
};
Expand Down
3 changes: 2 additions & 1 deletion templates/controllers/wizard/fileUpload/fileUploadWizard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
finishButtonText: {translate|json_encode key="common.complete"},
deleteUrl: {url|json_encode component="api.file.ManageFileApiHandler" op="deleteFile" submissionId=$submissionId stageId=$stageId fileStage=$fileStage suppressNotification=true escape=false},
metadataUrl: {url|json_encode op="editMetadata" submissionId=$submissionId stageId=$stageId reviewRoundId=$reviewRoundId fileStage=$fileStage assocType=$assocType assocId=$assocId queryId=$queryId escape=false},
finishUrl: {url|json_encode op="finishFileSubmission" submissionId=$submissionId stageId=$stageId reviewRoundId=$reviewRoundId fileStage=$fileStage assocType=$assocType assocId=$assocId queryId=$queryId escape=false}
finishUrl: {url|json_encode op="finishFileSubmission" submissionId=$submissionId stageId=$stageId reviewRoundId=$reviewRoundId fileStage=$fileStage assocType=$assocType assocId=$assocId queryId=$queryId escape=false},
cancelUrl: {url|json_encode component="api.file.ManageFileApiHandler" op="cancelFileUpload" submissionId=$submissionId stageId=$stageId fileStage=$fileStage escape=false}
{rdelim}
);
{rdelim});
Expand Down

0 comments on commit fc94ba2

Please sign in to comment.