From 0fc14a4310e1f386064be669a09341cc2facd87f Mon Sep 17 00:00:00 2001 From: Taslan Graham Date: Wed, 15 Jan 2025 14:47:09 -0500 Subject: [PATCH] pkp/pkp-lib#10705 Fix file upload cancellation issue --- .../api/file/PKPManageFileApiHandler.php | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/controllers/api/file/PKPManageFileApiHandler.php b/controllers/api/file/PKPManageFileApiHandler.php index 5e72c85248a..a113a1ceee1 100644 --- a/controllers/api/file/PKPManageFileApiHandler.php +++ b/controllers/api/file/PKPManageFileApiHandler.php @@ -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()); @@ -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(); @@ -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();