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
21 * @ingroup Maintenance
23 require_once( __DIR__
. '/../../maintenance/Maintenance.php' );
26 * Assemble the segments of a chunked upload.
28 * @ingroup Maintenance
30 class AssembleUploadChunks
extends Maintenance
{
31 public function __construct() {
32 parent
::__construct();
33 $this->mDescription
= "Re-assemble the segments of a chunked upload into a single file";
34 $this->addOption( 'filename', "Desired file name", true, true );
35 $this->addOption( 'filekey', "Upload stash file key", true, true );
36 $this->addOption( 'userid', "Upload owner user ID", true, true );
37 $this->addOption( 'sessionid', "Upload owner session ID", true, true );
40 public function execute() {
42 wfDebug( "Started assembly for file {$this->getOption( 'filename' )}\n" );
43 wfSetupSession( $this->getOption( 'sessionid' ) );
45 $user = User
::newFromId( $this->getOption( 'userid' ) );
47 throw new MWException( "No user with ID " . $this->getOption( 'userid' ) . "." );
50 $upload = new UploadFromChunks( $user );
51 $upload->continueChunks(
52 $this->getOption( 'filename' ),
53 $this->getOption( 'filekey' ),
55 RequestContext
::getMain()->getRequest() // dummy request
58 // Combine all of the chunks into a local file and upload that to a new stash file
59 $status = $upload->concatenateChunks();
60 if ( !$status->isGood() ) {
61 UploadBase
::setSessionStatus(
62 $this->getOption( 'filekey' ),
63 array( 'result' => 'Failure', 'status' => $status )
65 session_write_close();
66 $this->error( $status->getWikiText() . "\n", 1 ); // die
69 // We have a new filekey for the fully concatenated file
70 $newFileKey = $upload->getLocalFile()->getFileKey();
72 // Remove the old stash file row and first chunk file
73 $upload->stash
->removeFileNoAuth( $this->getOption( 'filekey' ) );
75 // Build the image info array while we have the local reference handy
76 $apiMain = new ApiMain(); // dummy object (XXX)
77 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
79 // Cleanup any temporary local file
80 $upload->cleanupTempFile();
82 // Cache the info so the user doesn't have to wait forever to get the final info
83 UploadBase
::setSessionStatus(
84 $this->getOption( 'filekey' ),
86 'result' => 'Success',
87 'filekey' => $newFileKey,
88 'imageinfo' => $imageInfo,
89 'status' => Status
::newGood()
92 } catch ( MWException
$e ) {
93 UploadBase
::setSessionStatus(
94 $this->getOption( 'filekey' ),
96 'result' => 'Failure',
97 'status' => Status
::newFatal( 'api-error-stashfailed' )
101 session_write_close();
105 wfDebug( "Finished assembly for file {$this->getOption( 'filename' )}\n" );
109 $maintClass = "AssembleUploadChunks";
110 require_once( RUN_MAINTENANCE_IF_MAIN
);