mDescription = "Re-assemble the segments of a chunked upload into a single file"; $this->addOption( 'filename', "Desired file name", true, true ); $this->addOption( 'filekey', "Upload stash file key", true, true ); $this->addOption( 'userid', "Upload owner user ID", true, true ); $this->addOption( 'sessionid', "Upload owner session ID", true, true ); } public function execute() { $e = null; wfDebug( "Started assembly for file {$this->getOption( 'filename' )}\n" ); wfSetupSession( $this->getOption( 'sessionid' ) ); try { $user = User::newFromId( $this->getOption( 'userid' ) ); if ( !$user ) { throw new MWException( "No user with ID " . $this->getOption( 'userid' ) . "." ); } UploadBase::setSessionStatus( $this->getOption( 'filekey' ), array( 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ) ); $upload = new UploadFromChunks( $user ); $upload->continueChunks( $this->getOption( 'filename' ), $this->getOption( 'filekey' ), // @TODO: set User? RequestContext::getMain()->getRequest() // dummy request ); // Combine all of the chunks into a local file and upload that to a new stash file $status = $upload->concatenateChunks(); if ( !$status->isGood() ) { UploadBase::setSessionStatus( $this->getOption( 'filekey' ), array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ) ); session_write_close(); $this->error( $status->getWikiText() . "\n", 1 ); // die } // We have a new filekey for the fully concatenated file $newFileKey = $upload->getLocalFile()->getFileKey(); // Remove the old stash file row and first chunk file $upload->stash->removeFileNoAuth( $this->getOption( 'filekey' ) ); // Build the image info array while we have the local reference handy $apiMain = new ApiMain(); // dummy object (XXX) $imageInfo = $upload->getImageInfo( $apiMain->getResult() ); // Cleanup any temporary local file $upload->cleanupTempFile(); // Cache the info so the user doesn't have to wait forever to get the final info UploadBase::setSessionStatus( $this->getOption( 'filekey' ), array( 'result' => 'Success', 'stage' => 'assembling', 'filekey' => $newFileKey, 'imageinfo' => $imageInfo, 'status' => Status::newGood() ) ); } catch ( MWException $e ) { UploadBase::setSessionStatus( $this->getOption( 'filekey' ), array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => Status::newFatal( 'api-error-stashfailed' ) ) ); } session_write_close(); if ( $e ) { throw $e; } wfDebug( "Finished assembly for file {$this->getOption( 'filename' )}\n" ); } } $maintClass = "AssembleUploadChunks"; require_once( RUN_MAINTENANCE_IF_MAIN );