3 * Assemble the segments of a chunked upload.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Wikimedia\ScopedCallback
;
26 * Assemble the segments of a chunked upload.
30 class AssembleUploadChunksJob
extends Job
{
31 public function __construct( Title
$title, array $params ) {
32 parent
::__construct( 'AssembleUploadChunks', $title, $params );
33 $this->removeDuplicates
= true;
36 public function run() {
37 $scope = RequestContext
::importScopedSession( $this->params
['session'] );
38 $this->addTeardownCallback( function () use ( &$scope ) {
39 ScopedCallback
::consume( $scope ); // T126450
42 $context = RequestContext
::getMain();
43 $user = $context->getUser();
45 if ( !$user->isLoggedIn() ) {
46 $this->setLastError( "Could not load the author user from session." );
51 UploadBase
::setSessionStatus(
53 $this->params
['filekey'],
54 [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status
::newGood() ]
57 $upload = new UploadFromChunks( $user );
58 $upload->continueChunks(
59 $this->params
['filename'],
60 $this->params
['filekey'],
61 new WebRequestUpload( $context->getRequest(), 'null' )
64 // Combine all of the chunks into a local file and upload that to a new stash file
65 $status = $upload->concatenateChunks();
66 if ( !$status->isGood() ) {
67 UploadBase
::setSessionStatus(
69 $this->params
['filekey'],
70 [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
72 $this->setLastError( $status->getWikiText( false, false, 'en' ) );
77 // We can only get warnings like 'duplicate' after concatenating the chunks
78 $status = Status
::newGood();
80 'warnings' => UploadBase
::makeWarningsSerializable( $upload->checkWarnings() )
83 // We have a new filekey for the fully concatenated file
84 $newFileKey = $upload->getStashFile()->getFileKey();
86 // Remove the old stash file row and first chunk file
87 $upload->stash
->removeFileNoAuth( $this->params
['filekey'] );
89 // Build the image info array while we have the local reference handy
90 $apiMain = new ApiMain(); // dummy object (XXX)
91 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
93 // Cleanup any temporary local file
94 $upload->cleanupTempFile();
96 // Cache the info so the user doesn't have to wait forever to get the final info
97 UploadBase
::setSessionStatus(
99 $this->params
['filekey'],
101 'result' => 'Success',
102 'stage' => 'assembling',
103 'filekey' => $newFileKey,
104 'imageinfo' => $imageInfo,
108 } catch ( Exception
$e ) {
109 UploadBase
::setSessionStatus(
111 $this->params
['filekey'],
113 'result' => 'Failure',
114 'stage' => 'assembling',
115 'status' => Status
::newFatal( 'api-error-stashfailed' )
118 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
119 // To be extra robust.
120 MWExceptionHandler
::rollbackMasterChangesAndLog( $e );
128 public function getDeduplicationInfo() {
129 $info = parent
::getDeduplicationInfo();
130 if ( is_array( $info['params'] ) ) {
131 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
137 public function allowRetries() {