Fix for chunked uploading support in API.
authorErik Moeller <erik@wikimedia.org>
Sun, 8 Apr 2012 11:27:32 +0000 (04:27 -0700)
committerErik Moeller <erik@wikimedia.org>
Mon, 9 Apr 2012 03:27:02 +0000 (20:27 -0700)
Chunked uploading is currently only implemented by Upload Wizard,
but is supported in MediaWiki core. It's enabled in Upload Wizard
by setting $wgUploadWizardConfig['enableChunked'] to true.

When enabled, large files will be split into smaller chunks, by
default of 1 MB. This is done through a series of API POST
requests. The file is identified by means of a 'filekey' to
allow for continuation of uploads from previous offsets.

Previously broken behavior: Files were concatenated correctly,
but instead of the whole file, one of the chunks was uploaded
to the wiki. This was due to the API using the filekey of the
chunk, as opposed to the filekey of the whole file.

In addition, this change also cleans out the stash information
for both filekeys after the upload is complete.

[Patch set 2: Whitespace fix]
[Patch set 3: Move filekey result past status check]

Change-Id: Idac94e953676787f9516051e47c006525f198fd4

includes/api/ApiUpload.php

index 67165b9..5040c70 100644 (file)
@@ -187,15 +187,28 @@ class ApiUpload extends ApiBase {
                                $this->dieUsage( $status->getWikiText(), 'stashfailed' );
                                return ;
                        }
-                       $result['filekey'] = $this->mParams['filekey'];
+
                        // Check we added the last chunk: 
                        if( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
                                $status = $this->mUpload->concatenateChunks();
+
                                if ( !$status->isGood() ) {
                                        $this->dieUsage( $status->getWikiText(), 'stashfailed' );
                                        return ;
                                }
+
+                               // We have a new filekey for the fully concatenated file.
+                               $result['filekey'] =  $this->mUpload->getLocalFile()->getFileKey();
+
+                               // Remove chunk from stash. (Checks against user ownership of chunks.)
+                               $this->mUpload->stash->removeFile( $this->mParams['filekey'] );
+
                                $result['result'] = 'Success';
+
+                       } else {
+
+                               // Continue passing through the filekey for adding further chunks.
+                               $result['filekey'] = $this->mParams['filekey'];
                        }
                }
                $result['offset'] = $this->mParams['offset'] + $chunkSize;