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 cabb879 commit 0fc14a4
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions controllers/api/file/PKPManageFileApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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 @@ -114,46 +114,50 @@ 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);
}
if (!empty($originalFile)) {

$originalFileId = (int) $originalFile['fileId'];
if (!isset($originalFile['fileId']) || !in_array($originalFile['fileId'], $revisionIds)) {
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);
}
$originalFileId = (int) $originalFile['fileId'];

$originalUser = Repo::user()->get($originalUserId);
if (!$originalUser) {
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);
}

$originalUsername = $originalUser->getUsername();
$matchedLogEntry = $this->findMatchedLogEntry($submissionFile, $originalFileId, $originalUsername, $originalFileName);
if (!$matchedLogEntry) {
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);
}

// 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
Services::get('file')->delete($revisedFileId);
Services::get('file')->delete($fileIdToCancel);

$this->setupTemplate($request);
return \PKP\db\DAO::getDataChangedEvent();
Expand Down Expand Up @@ -276,8 +280,7 @@ protected function findMatchedLogEntry(
int $originalFileId,
string $originalUsername,
array $originalFileName
): ?EventLogEntry
{
): ?EventLogEntry {
$logEntries = Repo::eventLog()->getCollector()
->filterByAssoc(PKPApplication::ASSOC_TYPE_SUBMISSION_FILE, [$submissionFile->getId()])
->getMany();
Expand Down

0 comments on commit 0fc14a4

Please sign in to comment.