diff options
Diffstat (limited to 'includes/upload/UploadFromChunks.php')
-rw-r--r-- | includes/upload/UploadFromChunks.php | 110 |
1 files changed, 69 insertions, 41 deletions
diff --git a/includes/upload/UploadFromChunks.php b/includes/upload/UploadFromChunks.php index 2e0b9444..14993023 100644 --- a/includes/upload/UploadFromChunks.php +++ b/includes/upload/UploadFromChunks.php @@ -28,14 +28,19 @@ * @author Michael Dale */ class UploadFromChunks extends UploadFromFile { - protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath; + protected $mOffset; + protected $mChunkIndex; + protected $mFileKey; + protected $mVirtualTempPath; + /** @var LocalRepo */ + private $repo; /** * Setup local pointers to stash, repo and user (similar to UploadFromStash) * - * @param $user User - * @param $stash UploadStash - * @param $repo FileRepo + * @param User|null $user Default: null + * @param UploadStash|bool $stash Default: false + * @param FileRepo|bool $repo Default: false */ public function __construct( $user = null, $stash = false, $repo = false ) { // user object. sometimes this won't exist, as when running from cron. @@ -57,14 +62,13 @@ class UploadFromChunks extends UploadFromFile { } $this->stash = new UploadStash( $this->repo, $this->user ); } - - return true; } /** * Calls the parent stashFile and updates the uploadsession table to handle "chunks" * - * @return UploadStashFile stashed file + * @param User|null $user + * @return UploadStashFile Stashed file */ public function stashFile( User $user = null ) { // Stash file is the called on creating a new chunk session: @@ -83,11 +87,16 @@ class UploadFromChunks extends UploadFromFile { // Update db table to reflect initial "chunk" state $this->updateChunkStatus(); + return $this->mLocalFile; } /** * Continue chunk uploading + * + * @param string $name + * @param string $key + * @param WebRequestUpload $webRequestUpload */ public function continueChunks( $name, $key, $webRequestUpload ) { $this->mFileKey = $key; @@ -108,13 +117,14 @@ class UploadFromChunks extends UploadFromFile { * @return FileRepoStatus */ public function concatenateChunks() { + $chunkIndex = $this->getChunkIndex(); wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" . - $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" ); + $this->getOffset() . ' inx:' . $chunkIndex . "\n" ); // Concatenate all the chunks to mVirtualTempPath - $fileList = Array(); + $fileList = array(); // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1" - for ( $i = 0; $i <= $this->getChunkIndex(); $i++ ) { + for ( $i = 0; $i <= $chunkIndex; $i++ ) { $fileList[] = $this->getVirtualChunkLocation( $i ); } @@ -122,9 +132,12 @@ class UploadFromChunks extends UploadFromFile { $ext = FileBackend::extensionFromPath( $this->mVirtualTempPath ); // Get a 0-byte temp file to perform the concatenation at $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext ); - $tmpPath = $tmpFile - ? $tmpFile->bind( $this )->getPath() // keep alive with $this - : false; // fail in concatenate() + $tmpPath = false; // fail in concatenate() + if ( $tmpFile ) { + // keep alive with $this + $tmpPath = $tmpFile->bind( $this )->getPath(); + } + // Concatenate the chunks at the temp file $tStart = microtime( true ); $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE ); @@ -132,14 +145,17 @@ class UploadFromChunks extends UploadFromFile { if ( !$status->isOk() ) { return $status; } - wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds.\n" ); + wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds." ); - $this->mTempPath = $tmpPath; // file system path - $this->mFileSize = filesize( $this->mTempPath ); //Since this was set for the last chunk previously + // File system path + $this->mTempPath = $tmpPath; + // Since this was set for the last chunk previously + $this->mFileSize = filesize( $this->mTempPath ); $ret = $this->verifyUpload(); if ( $ret['status'] !== UploadBase::OK ) { wfDebugLog( 'fileconcatenate', "Verification failed for chunked upload" ); $status->fatal( $this->getVerificationErrorCode( $ret['status'] ) ); + return $status; } @@ -149,44 +165,45 @@ class UploadFromChunks extends UploadFromFile { $this->mLocalFile = parent::stashFile( $this->user ); $tAmount = microtime( true ) - $tStart; $this->mLocalFile->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo()) - wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds.\n" ); + wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds." ); return $status; } /** * Perform the upload, then remove the temp copy afterward - * @param $comment string - * @param $pageText string - * @param $watch bool - * @param $user User + * @param string $comment + * @param string $pageText + * @param bool $watch + * @param User $user * @return Status */ public function performUpload( $comment, $pageText, $watch, $user ) { $rv = parent::performUpload( $comment, $pageText, $watch, $user ); + return $rv; } /** * Returns the virtual chunk location: - * @param $index + * @param int $index * @return string */ function getVirtualChunkLocation( $index ) { return $this->repo->getVirtualUrl( 'temp' ) . - '/' . - $this->repo->getHashPath( - $this->getChunkFileKey( $index ) - ) . - $this->getChunkFileKey( $index ); + '/' . + $this->repo->getHashPath( + $this->getChunkFileKey( $index ) + ) . + $this->getChunkFileKey( $index ); } /** * Add a chunk to the temporary directory * - * @param string $chunkPath path to temporary chunk file - * @param int $chunkSize size of the current chunk - * @param int $offset offset of current chunk ( mutch match database chunk offset ) + * @param string $chunkPath Path to temporary chunk file + * @param int $chunkSize Size of the current chunk + * @param int $offset Offset of current chunk ( mutch match database chunk offset ) * @return Status */ public function addChunk( $chunkPath, $chunkSize, $offset ) { @@ -220,6 +237,7 @@ class UploadFromChunks extends UploadFromFile { $status = Status::newFatal( 'invalid-chunk-offset' ); } } + return $status; } @@ -228,7 +246,7 @@ class UploadFromChunks extends UploadFromFile { */ private function updateChunkStatus() { wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" . - $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" ); + $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" ); $dbw = $this->repo->getMasterDb(); // Use a quick transaction since we will upload the full temp file into shared @@ -274,30 +292,32 @@ class UploadFromChunks extends UploadFromFile { /** * Get the current Chunk index - * @return Integer index of the current chunk + * @return int Index of the current chunk */ private function getChunkIndex() { if ( $this->mChunkIndex !== null ) { return $this->mChunkIndex; } + return 0; } /** * Gets the current offset in fromt the stashedupload table - * @return Integer current byte offset of the chunk file set + * @return int Current byte offset of the chunk file set */ private function getOffset() { if ( $this->mOffset !== null ) { return $this->mOffset; } + return 0; } /** * Output the chunk to disk * - * @param $chunkPath string + * @param string $chunkPath * @throws UploadChunkFileException * @return FileRepoStatus */ @@ -311,18 +331,20 @@ class UploadFromChunks extends UploadFromFile { $this->repo->getZonePath( 'temp' ) . "/{$hashPath}{$fileKey}" ); // Check for error in stashing the chunk: - if ( ! $storeStatus->isOK() ) { + if ( !$storeStatus->isOK() ) { $error = $storeStatus->getErrorsArray(); $error = reset( $error ); - if ( ! count( $error ) ) { + if ( !count( $error ) ) { $error = $storeStatus->getWarningsArray(); $error = reset( $error ); - if ( ! count( $error ) ) { + if ( !count( $error ) ) { $error = array( 'unknown', 'no error recorded' ); } } - throw new UploadChunkFileException( "error storing file in '$chunkPath': " . implode( '; ', $error ) ); + throw new UploadChunkFileException( "Error storing file in '$chunkPath': " . + implode( '; ', $error ) ); } + return $storeStatus; } @@ -330,6 +352,7 @@ class UploadFromChunks extends UploadFromFile { if ( $index === null ) { $index = $this->getChunkIndex(); } + return $this->mFileKey . '.' . $index; } @@ -352,6 +375,11 @@ class UploadFromChunks extends UploadFromFile { } } -class UploadChunkZeroLengthFileException extends MWException {}; -class UploadChunkFileException extends MWException {}; -class UploadChunkVerificationException extends MWException {}; +class UploadChunkZeroLengthFileException extends MWException { +} + +class UploadChunkFileException extends MWException { +} + +class UploadChunkVerificationException extends MWException { +} |