Skip to content

Commit

Permalink
pkp#10705 Fix file upload cancellation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Jan 15, 2025
1 parent 5b69986 commit ad8f11a
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions controllers/api/file/PKPManageFileApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function cancelFileUpload(array $args, Request $request): JSONMessage

$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;
$fileIdToCancel = $request->getUserVar('fileId') ? (int)$request->getUserVar('fileId') : null;

// Get revisions and check file IDs
$revisions = Repo::submissionFile()->getRevisions($submissionFile->getId());
Expand All @@ -111,46 +111,49 @@ public function cancelFileUpload(array $args, Request $request): JSONMessage
$revisionIds[] = $revision->fileId;
}

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

if (!isset($originalFile['fileId']) || !in_array($originalFile['fileId'], $revisionIds)) {
return new JSONMessage(false);
}
// Original file is only present in request when the file to be cancelled was being upload as a revision of a previous file
if (!empty($originalFile)) {
if (!isset($originalFile['fileId']) || !in_array($originalFile['fileId'], $revisionIds)) {
return new JSONMessage(false);
}

$originalFileId = (int) $originalFile['fileId'];
$originalFileId = (int) $originalFile['fileId'];

// Get the file name and uploader user ID
$originalUserId = $originalFile['uploaderUserId'] ? (int)$originalFile['uploaderUserId'] : null;
$originalFileName = $originalFile['name'] ? (array)$originalFile['name'] : null;
if (!$originalUserId || !$originalFileName) {
return new JSONMessage(false);
}
// Get the file name and uploader user ID
$originalUserId = $originalFile['uploaderUserId'] ? (int)$originalFile['uploaderUserId'] : null;
$originalFileName = $originalFile['name'] ? (array)$originalFile['name'] : null;
if (!$originalUserId || !$originalFileName) {
return new JSONMessage(false);
}

$originalUser = Repo::user()->get($originalUserId);
if (!$originalUser) {
return new JSONMessage(false);
}
$originalUser = Repo::user()->get($originalUserId);
if (!$originalUser) {
return new JSONMessage(false);
}

$originalUsername = $originalUser->getUsername();
$matchedLogEntry = $this->findMatchedLogEntry($submissionFile, $originalFileId, $originalUsername, $originalFileName);
if (!$matchedLogEntry) {
return new JSONMessage(false);
}
$originalUsername = $originalUser->getUsername();
$matchedLogEntry = $this->findMatchedLogEntry($submissionFile, $originalFileId, $originalUsername, $originalFileName);
if (!$matchedLogEntry) {
return new JSONMessage(false);
}

// Restore original submission file
Repo::submissionFile()->edit(
$submissionFile,
[
'fileId' => $matchedLogEntry->getData('fileId'),
'name' => $matchedLogEntry->getData('filename'),
'uploaderUserId' => Repo::user()->getByUsername($matchedLogEntry->getData('username'))->getId(),
]
);
// Restore original submission file
Repo::submissionFile()->edit(
$submissionFile,
[
'fileId' => $matchedLogEntry->getData('fileId'),
'name' => $matchedLogEntry->getData('filename'),
'uploaderUserId' => Repo::user()->getByUsername($matchedLogEntry->getData('username'))->getId(),
]
);
}

// Remove uploaded file
app()->get('file')->delete($revisedFileId);
app()->get('file')->delete($fileIdToCancel);

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

0 comments on commit ad8f11a

Please sign in to comment.