Merge "copy paste comment should probably have end instead of start"
[lhc/web/wiklou.git] / includes / api / ApiUpload.php
index 9d30729..34741b5 100644 (file)
@@ -56,15 +56,19 @@ class ApiUpload extends ApiBase {
                $this->mParams['chunk'] = $request->getFileName( 'chunk' );
 
                // Copy the session key to the file key, for backward compatibility.
-               if( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
+               if ( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
                        $this->mParams['filekey'] = $this->mParams['sessionkey'];
                }
 
                // Select an upload module
-               if ( !$this->selectUploadModule() ) {
-                       return; // not a true upload, but a status request or similar
-               } elseif ( !isset( $this->mUpload ) ) {
-                       $this->dieUsage( 'No upload module set', 'nomodule' );
+               try {
+                       if ( !$this->selectUploadModule() ) {
+                               return; // not a true upload, but a status request or similar
+                       } elseif ( !isset( $this->mUpload ) ) {
+                               $this->dieUsage( 'No upload module set', 'nomodule' );
+                       }
+               } catch ( UploadStashException $e ) { // XXX: don't spam exception log
+                       $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
                }
 
                // First check permission to upload
@@ -82,15 +86,16 @@ class ApiUpload extends ApiBase {
                // Check if the uploaded file is sane
                if ( $this->mParams['chunk'] ) {
                        $maxSize = $this->mUpload->getMaxUploadSize();
-                       if( $this->mParams['filesize'] > $maxSize ) {
+                       if ( $this->mParams['filesize'] > $maxSize ) {
                                $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
                        }
                        if ( !$this->mUpload->getTitle() ) {
                                $this->dieUsage( 'Invalid file title supplied', 'internal-error' );
                        }
-               } elseif ( $this->mParams['async'] ) {
+               } elseif ( $this->mParams['async'] && $this->mParams['filekey'] ) {
                        // defer verification to background process
                } else {
+                       wfDebug( __METHOD__ . 'about to verify' );
                        $this->verifyUpload();
                }
 
@@ -105,9 +110,13 @@ class ApiUpload extends ApiBase {
                }
 
                // Get the result based on the current upload context:
-               $result = $this->getContextResult();
-               if ( $result['result'] === 'Success' ) {
-                       $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
+               try {
+                       $result = $this->getContextResult();
+                       if ( $result['result'] === 'Success' ) {
+                               $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
+                       }
+               } catch ( UploadStashException $e ) { // XXX: don't spam exception log
+                       $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
                }
 
                $this->getResult()->addValue( null, $this->getModuleName(), $result );
@@ -195,7 +204,12 @@ class ApiUpload extends ApiBase {
                $chunkPath = $request->getFileTempname( 'chunk' );
                $chunkSize = $request->getUpload( 'chunk' )->getSize();
                if ( $this->mParams['offset'] == 0 ) {
-                       $filekey = $this->performStash();
+                       try {
+                               $filekey = $this->performStash();
+                       } catch ( MWException $e ) {
+                               // FIXME: Error handling here is wrong/different from rest of this
+                               $this->dieUsage( $e->getMessage(), 'stashfailed' );
+                       }
                } else {
                        $filekey = $this->mParams['filekey'];
                        /** @var $status Status */
@@ -210,27 +224,27 @@ class ApiUpload extends ApiBase {
                // Check we added the last chunk:
                if ( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
                        if ( $this->mParams['async'] ) {
-                               $progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
+                               $progress = UploadBase::getSessionStatus( $filekey );
                                if ( $progress && $progress['result'] === 'Poll' ) {
                                        $this->dieUsage( "Chunk assembly already in progress.", 'stashfailed' );
                                }
                                UploadBase::setSessionStatus(
-                                       $this->mParams['filekey'],
+                                       $filekey,
                                        array( 'result' => 'Poll',
                                                'stage' => 'queued', 'status' => Status::newGood() )
                                );
                                $ok = JobQueueGroup::singleton()->push( new AssembleUploadChunksJob(
-                                       Title::makeTitle( NS_FILE, $this->mParams['filekey'] ),
+                                       Title::makeTitle( NS_FILE, $filekey ),
                                        array(
                                                'filename' => $this->mParams['filename'],
-                                               'filekey' => $this->mParams['filekey'],
+                                               'filekey' => $filekey,
                                                'session' => $this->getContext()->exportSession()
                                        )
                                ) );
                                if ( $ok ) {
                                        $result['result'] = 'Poll';
                                } else {
-                                       UploadBase::setSessionStatus( $this->mParams['filekey'], false );
+                                       UploadBase::setSessionStatus( $filekey, false );
                                        $this->dieUsage(
                                                "Failed to start AssembleUploadChunks.php", 'stashfailed' );
                                }
@@ -354,7 +368,7 @@ class ApiUpload extends ApiBase {
                if ( $this->mParams['chunk'] ) {
                        // Chunk upload
                        $this->mUpload = new UploadFromChunks();
-                       if( isset( $this->mParams['filekey'] ) ) {
+                       if ( isset( $this->mParams['filekey'] ) ) {
                                // handle new chunk
                                $this->mUpload->continueChunks(
                                        $this->mParams['filename'],
@@ -456,7 +470,7 @@ class ApiUpload extends ApiBase {
                global $wgFileExtensions;
 
                // @todo Move them to ApiBase's message map
-               switch( $verification['status'] ) {
+               switch ( $verification['status'] ) {
                        // Recoverable errors
                        case UploadBase::MIN_LENGTH_PARTNAME:
                                $this->dieRecoverableError( 'filename-tooshort', 'filename' );
@@ -801,6 +815,7 @@ class ApiUpload extends ApiBase {
                                array( 'code' => 'publishfailed', 'info' => 'Publishing of stashed file failed' ),
                                array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
                                array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
+                               array( 'code' => 'stasherror', 'info' => 'An upload stash error occurred' ),
                                array( 'fileexists-forbidden' ),
                                array( 'fileexists-shared-forbidden' ),
                        )