| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Api\ApiUpload; |
| 22 |
use MediaWiki\Context\RequestContext; |
| 23 |
use MediaWiki\Logger\LoggerFactory; |
| 24 |
use MediaWiki\Request\WebRequestUpload; |
| 25 |
use MediaWiki\Status\Status; |
| 26 |
use Wikimedia\ScopedCallback; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class AssembleUploadChunksJob extends Job implements GenericParameterJob { |
| 35 |
public function __construct( array $params ) { |
| 36 |
parent::__construct( 'AssembleUploadChunks', $params ); |
| 37 |
$this->removeDuplicates = true; |
| 38 |
} |
| 39 |
|
| 40 |
public function run() { |
| 41 |
$scope = RequestContext::importScopedSession( $this->params['session'] ); |
| 42 |
$this->addTeardownCallback( static function () use ( &$scope ) { |
| 43 |
ScopedCallback::consume( $scope ); |
| 44 |
} ); |
| 45 |
|
| 46 |
$logger = LoggerFactory::getInstance( 'upload' ); |
| 47 |
$context = RequestContext::getMain(); |
| 48 |
$user = $context->getUser(); |
| 49 |
try { |
| 50 |
if ( !$user->isRegistered() ) { |
| 51 |
$this->setLastError( "Could not load the author user from session." ); |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
$startingStatus = UploadBase::getSessionStatus( $user, $this->params['filekey'] ); |
| 58 |
if ( |
| 59 |
!$startingStatus || |
| 60 |
( $startingStatus['result'] ?? '' ) !== 'Poll' || |
| 61 |
( $startingStatus['stage'] ?? '' ) !== 'queued' |
| 62 |
) { |
| 63 |
$logger->warning( "Tried to assemble upload that is in stage {stage}/{result}", |
| 64 |
[ |
| 65 |
'stage' => $startingStatus['stage'] ?? '-', |
| 66 |
'result' => $startingStatus['result'] ?? '-', |
| 67 |
'status' => (string)( $startingStatus['status'] ?? '-' ), |
| 68 |
'filekey' => $this->params['filekey'], |
| 69 |
'filename' => $this->params['filename'], |
| 70 |
'user' => $user->getName(), |
| 71 |
] |
| 72 |
); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if ( |
| 77 |
$startingStatus && |
| 78 |
$startingStatus['stage'] === 'assembling' && |
| 79 |
$startingStatus['result'] !== 'Failure' |
| 80 |
) { |
| 81 |
$this->setLastError( __METHOD__ . " already in progress" ); |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
UploadBase::setSessionStatus( |
| 86 |
$user, |
| 87 |
$this->params['filekey'], |
| 88 |
[ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ] |
| 89 |
); |
| 90 |
|
| 91 |
$upload = new UploadFromChunks( $user ); |
| 92 |
$upload->continueChunks( |
| 93 |
$this->params['filename'], |
| 94 |
$this->params['filekey'], |
| 95 |
new WebRequestUpload( $context->getRequest(), 'null' ) |
| 96 |
); |
| 97 |
if ( |
| 98 |
isset( $this->params['filesize'] ) && |
| 99 |
$this->params['filesize'] !== (int)$upload->getOffset() |
| 100 |
) { |
| 101 |
|
| 102 |
|
| 103 |
throw new UnexpectedValueException( |
| 104 |
"UploadStash file size does not match job's. Potential mis-nested transaction?" |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
$status = $upload->concatenateChunks(); |
| 109 |
if ( !$status->isGood() ) { |
| 110 |
UploadBase::setSessionStatus( |
| 111 |
$user, |
| 112 |
$this->params['filekey'], |
| 113 |
[ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ] |
| 114 |
); |
| 115 |
$logger->info( "Chunked upload assembly job failed for {filekey} because {status}", |
| 116 |
[ |
| 117 |
'filekey' => $this->params['filekey'], |
| 118 |
'filename' => $this->params['filename'], |
| 119 |
'user' => $user->getName(), |
| 120 |
'status' => (string)$status |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
$status = Status::newGood(); |
| 131 |
$status->value = [ |
| 132 |
'warnings' => UploadBase::makeWarningsSerializable( |
| 133 |
$upload->checkWarnings( $user ) |
| 134 |
) |
| 135 |
]; |
| 136 |
|
| 137 |
|
| 138 |
$newFileKey = $upload->getStashFile()->getFileKey(); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$upload->stash->removeFileNoAuth( $this->params['filekey'] ); |
| 144 |
|
| 145 |
|
| 146 |
$apiUpload = ApiUpload::getDummyInstance(); |
| 147 |
$imageInfo = $apiUpload->getUploadImageInfo( $upload ); |
| 148 |
|
| 149 |
|
| 150 |
$upload->cleanupTempFile(); |
| 151 |
|
| 152 |
|
| 153 |
UploadBase::setSessionStatus( |
| 154 |
$user, |
| 155 |
$this->params['filekey'], |
| 156 |
[ |
| 157 |
'result' => 'Success', |
| 158 |
'stage' => 'assembling', |
| 159 |
'filekey' => $newFileKey, |
| 160 |
'imageinfo' => $imageInfo, |
| 161 |
'status' => $status |
| 162 |
] |
| 163 |
); |
| 164 |
$logger->info( "{filekey} successfully assembled into {newkey}", |
| 165 |
[ |
| 166 |
'filekey' => $this->params['filekey'], |
| 167 |
'newkey' => $newFileKey, |
| 168 |
'filename' => $this->params['filename'], |
| 169 |
'user' => $user->getName(), |
| 170 |
'status' => (string)$status |
| 171 |
] |
| 172 |
); |
| 173 |
} catch ( Exception $e ) { |
| 174 |
UploadBase::setSessionStatus( |
| 175 |
$user, |
| 176 |
$this->params['filekey'], |
| 177 |
[ |
| 178 |
'result' => 'Failure', |
| 179 |
'stage' => 'assembling', |
| 180 |
'status' => Status::newFatal( 'api-error-stashfailed' ) |
| 181 |
] |
| 182 |
); |
| 183 |
$this->setLastError( get_class( $e ) . ": " . $e->getMessage() ); |
| 184 |
|
| 185 |
MWExceptionHandler::rollbackPrimaryChangesAndLog( $e ); |
| 186 |
|
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
public function getDeduplicationInfo() { |
| 194 |
$info = parent::getDeduplicationInfo(); |
| 195 |
if ( is_array( $info['params'] ) ) { |
| 196 |
$info['params'] = [ 'filekey' => $info['params']['filekey'] ]; |
| 197 |
} |
| 198 |
|
| 199 |
return $info; |
| 200 |
} |
| 201 |
|
| 202 |
public function allowRetries() { |
| 203 |
return false; |
| 204 |
} |
| 205 |
} |