3 * Backend for uploading files from chunks.
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
25 * Implements uploading from chunks
28 * @author Michael Dale
30 class UploadFromChunks
extends UploadFromFile
{
32 protected $mChunkIndex;
34 protected $mVirtualTempPath;
39 * Setup local pointers to stash, repo and user (similar to UploadFromStash)
41 * @param User|null $user Default: null
42 * @param UploadStash|bool $stash Default: false
43 * @param FileRepo|bool $repo Default: false
45 public function __construct( $user = null, $stash = false, $repo = false ) {
46 // user object. sometimes this won't exist, as when running from cron.
52 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
56 $this->stash
= $stash;
59 wfDebug( __METHOD__
. " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
61 wfDebug( __METHOD__
. " creating new UploadFromChunks instance with no user\n" );
63 $this->stash
= new UploadStash( $this->repo
, $this->user
);
68 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
70 * @param User|null $user
71 * @return UploadStashFile Stashed file
73 public function stashFile( User
$user = null ) {
74 // Stash file is the called on creating a new chunk session:
75 $this->mChunkIndex
= 0;
79 // Create a local stash target
80 $this->mLocalFile
= parent
::stashFile( $user );
81 // Update the initial file offset (based on file size)
82 $this->mOffset
= $this->mLocalFile
->getSize();
83 $this->mFileKey
= $this->mLocalFile
->getFileKey();
85 // Output a copy of this first to chunk 0 location:
86 $this->outputChunk( $this->mLocalFile
->getPath() );
88 // Update db table to reflect initial "chunk" state
89 $this->updateChunkStatus();
91 return $this->mLocalFile
;
95 * Continue chunk uploading
99 * @param WebRequestUpload $webRequestUpload
101 public function continueChunks( $name, $key, $webRequestUpload ) {
102 $this->mFileKey
= $key;
103 $this->mUpload
= $webRequestUpload;
104 // Get the chunk status form the db:
105 $this->getChunkStatus();
107 $metadata = $this->stash
->getMetadata( $key );
108 $this->initializePathInfo( $name,
109 $this->getRealPath( $metadata['us_path'] ),
110 $metadata['us_size'],
116 * Append the final chunk and ready file for parent::performUpload()
117 * @return FileRepoStatus
119 public function concatenateChunks() {
120 $chunkIndex = $this->getChunkIndex();
121 wfDebug( __METHOD__
. " concatenate {$this->mChunkIndex} chunks:" .
122 $this->getOffset() . ' inx:' . $chunkIndex . "\n" );
124 // Concatenate all the chunks to mVirtualTempPath
126 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
127 for ( $i = 0; $i <= $chunkIndex; $i++
) {
128 $fileList[] = $this->getVirtualChunkLocation( $i );
131 // Get the file extension from the last chunk
132 $ext = FileBackend
::extensionFromPath( $this->mVirtualTempPath
);
133 // Get a 0-byte temp file to perform the concatenation at
134 $tmpFile = TempFSFile
::factory( 'chunkedupload_', $ext );
135 $tmpPath = false; // fail in concatenate()
137 // keep alive with $this
138 $tmpPath = $tmpFile->bind( $this )->getPath();
141 // Concatenate the chunks at the temp file
142 $tStart = microtime( true );
143 $status = $this->repo
->concatenate( $fileList, $tmpPath, FileRepo
::DELETE_SOURCE
);
144 $tAmount = microtime( true ) - $tStart;
145 if ( !$status->isOk() ) {
148 wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds." );
150 // File system path of the actual full temp file
151 $this->setTempFile( $tmpPath );
153 $ret = $this->verifyUpload();
154 if ( $ret['status'] !== UploadBase
::OK
) {
155 wfDebugLog( 'fileconcatenate', "Verification failed for chunked upload" );
156 $status->fatal( $this->getVerificationErrorCode( $ret['status'] ) );
161 // Update the mTempPath and mLocalFile
162 // (for FileUpload or normal Stash to take over)
163 $tStart = microtime( true );
164 $this->mLocalFile
= parent
::stashFile( $this->user
);
165 $tAmount = microtime( true ) - $tStart;
166 $this->mLocalFile
->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo())
167 wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds." );
173 * Returns the virtual chunk location:
177 function getVirtualChunkLocation( $index ) {
178 return $this->repo
->getVirtualUrl( 'temp' ) .
180 $this->repo
->getHashPath(
181 $this->getChunkFileKey( $index )
183 $this->getChunkFileKey( $index );
187 * Add a chunk to the temporary directory
189 * @param string $chunkPath Path to temporary chunk file
190 * @param int $chunkSize Size of the current chunk
191 * @param int $offset Offset of current chunk ( mutch match database chunk offset )
194 public function addChunk( $chunkPath, $chunkSize, $offset ) {
195 // Get the offset before we add the chunk to the file system
196 $preAppendOffset = $this->getOffset();
198 if ( $preAppendOffset +
$chunkSize > $this->getMaxUploadSize() ) {
199 $status = Status
::newFatal( 'file-too-large' );
201 // Make sure the client is uploading the correct chunk with a matching offset.
202 if ( $preAppendOffset == $offset ) {
203 // Update local chunk index for the current chunk
204 $this->mChunkIndex++
;
206 # For some reason mTempPath is set to first part
207 $oldTemp = $this->mTempPath
;
208 $this->mTempPath
= $chunkPath;
209 $this->verifyChunk();
210 $this->mTempPath
= $oldTemp;
211 } catch ( UploadChunkVerificationException
$e ) {
212 return Status
::newFatal( $e->getMessage() );
214 $status = $this->outputChunk( $chunkPath );
215 if ( $status->isGood() ) {
216 // Update local offset:
217 $this->mOffset
= $preAppendOffset +
$chunkSize;
218 // Update chunk table status db
219 $this->updateChunkStatus();
222 $status = Status
::newFatal( 'invalid-chunk-offset' );
230 * Update the chunk db table with the current status:
232 private function updateChunkStatus() {
233 wfDebug( __METHOD__
. " update chunk status for {$this->mFileKey} offset:" .
234 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
236 $dbw = $this->repo
->getMasterDb();
237 // Use a quick transaction since we will upload the full temp file into shared
238 // storage, which takes time for large files. We don't want to hold locks then.
242 'us_status' => 'chunks',
243 'us_chunk_inx' => $this->getChunkIndex(),
244 'us_size' => $this->getOffset()
246 [ 'us_key' => $this->mFileKey
],
249 $dbw->commit( __METHOD__
, 'flush' );
253 * Get the chunk db state and populate update relevant local values
255 private function getChunkStatus() {
256 // get Master db to avoid race conditions.
257 // Otherwise, if chunk upload time < replag there will be spurious errors
258 $dbw = $this->repo
->getMasterDb();
259 $row = $dbw->selectRow(
266 [ 'us_key' => $this->mFileKey
],
271 $this->mChunkIndex
= $row->us_chunk_inx
;
272 $this->mOffset
= $row->us_size
;
273 $this->mVirtualTempPath
= $row->us_path
;
278 * Get the current Chunk index
279 * @return int Index of the current chunk
281 private function getChunkIndex() {
282 if ( $this->mChunkIndex
!== null ) {
283 return $this->mChunkIndex
;
290 * Get the offset at which the next uploaded chunk will be appended to
291 * @return int Current byte offset of the chunk file set
293 public function getOffset() {
294 if ( $this->mOffset
!== null ) {
295 return $this->mOffset
;
302 * Output the chunk to disk
304 * @param string $chunkPath
305 * @throws UploadChunkFileException
306 * @return FileRepoStatus
308 private function outputChunk( $chunkPath ) {
309 // Key is fileKey + chunk index
310 $fileKey = $this->getChunkFileKey();
312 // Store the chunk per its indexed fileKey:
313 $hashPath = $this->repo
->getHashPath( $fileKey );
314 $storeStatus = $this->repo
->quickImport( $chunkPath,
315 $this->repo
->getZonePath( 'temp' ) . "/{$hashPath}{$fileKey}" );
317 // Check for error in stashing the chunk:
318 if ( !$storeStatus->isOK() ) {
319 $error = $storeStatus->getErrorsArray();
320 $error = reset( $error );
321 if ( !count( $error ) ) {
322 $error = $storeStatus->getWarningsArray();
323 $error = reset( $error );
324 if ( !count( $error ) ) {
325 $error = [ 'unknown', 'no error recorded' ];
328 throw new UploadChunkFileException( "Error storing file in '$chunkPath': " .
329 implode( '; ', $error ) );
335 private function getChunkFileKey( $index = null ) {
336 if ( $index === null ) {
337 $index = $this->getChunkIndex();
340 return $this->mFileKey
. '.' . $index;
344 * Verify that the chunk isn't really an evil html file
346 * @throws UploadChunkVerificationException
348 private function verifyChunk() {
349 // Rest mDesiredDestName here so we verify the name as if it were mFileKey
350 $oldDesiredDestName = $this->mDesiredDestName
;
351 $this->mDesiredDestName
= $this->mFileKey
;
352 $this->mTitle
= false;
353 $res = $this->verifyPartialFile();
354 $this->mDesiredDestName
= $oldDesiredDestName;
355 $this->mTitle
= false;
356 if ( is_array( $res ) ) {
357 throw new UploadChunkVerificationException( $res[0] );
362 class UploadChunkZeroLengthFileException
extends MWException
{
365 class UploadChunkFileException
extends MWException
{
368 class UploadChunkVerificationException
extends MWException
{