65 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
71 wfDebug( __METHOD__ .
" creating new UploadFromChunks instance for " .
$user->
getId() );
72 $this->stash =
new UploadStash( $this->repo, $this->user );
83 return Status::newFatal( $e->
msg );
86 return parent::tryStashFile(
$user, $isPartial );
97 $this->mChunkIndex = 0;
101 $this->mStashFile = parent::doStashFile(
$user );
103 $this->mOffset = $this->mStashFile->getSize();
104 $this->mFileKey = $this->mStashFile->getFileKey();
107 $this->outputChunk( $this->mStashFile->getPath() );
110 $this->updateChunkStatus();
123 $this->mFileKey = $key;
124 $this->mUpload = $webRequestUpload;
126 $this->getChunkStatus();
128 $metadata = $this->stash->getMetadata( $key );
131 $metadata[
'us_size'],
141 $chunkIndex = $this->getChunkIndex();
142 wfDebug( __METHOD__ .
" concatenate {$this->mChunkIndex} chunks:" .
143 $this->
getOffset() .
' inx:' . $chunkIndex );
148 for ( $i = 0; $i <= $chunkIndex; $i++ ) {
149 $fileList[] = $this->getVirtualChunkLocation( $i );
155 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
156 ->newTempFSFile(
'chunkedupload_',
$ext );
160 $tmpPath = $tmpFile->bind( $this )->getPath();
164 $tStart = microtime(
true );
166 $tAmount = microtime(
true ) - $tStart;
167 if ( !$status->
isOK() ) {
170 $this->logFileBackendStatus(
172 '[{type}] Error on concatenate {chunks} stashed files ({details})',
173 [
'chunks' => $chunkIndex ]
178 wfDebugLog(
'fileconcatenate',
"Combined $i chunks in $tAmount seconds." );
185 wfDebugLog(
'fileconcatenate',
"Verification failed for chunked upload" );
193 $tStart = microtime(
true );
198 $status->
fatal( ...$error );
202 $this->mStashFile = parent::doStashFile( $this->user );
204 $status->
fatal(
'uploadstash-exception', get_class( $e ), $e->getMessage() );
208 $tAmount = microtime(
true ) - $tStart;
210 $this->mStashFile->setLocalReference( $tmpFile );
211 wfDebugLog(
'fileconcatenate',
"Stashed combined file ($i chunks) in $tAmount seconds." );
221 private function getVirtualChunkLocation( $index ) {
222 return $this->repo->getVirtualUrl(
'temp' ) .
224 $this->repo->getHashPath(
225 $this->getChunkFileKey( $index )
227 $this->getChunkFileKey( $index );
238 public function addChunk( $chunkPath, $chunkSize, $offset ) {
243 $status = Status::newFatal(
'file-too-large' );
246 if ( $preAppendOffset == $offset ) {
248 $this->mChunkIndex++;
250 # For some reason mTempPath is set to first part
252 $this->mTempPath = $chunkPath;
253 $this->verifyChunk();
254 $this->mTempPath = $oldTemp;
256 return Status::newFatal( $e->
msg );
258 $status = $this->outputChunk( $chunkPath );
259 if ( $status->
isGood() ) {
261 $this->mOffset = $preAppendOffset + $chunkSize;
263 $this->updateChunkStatus();
266 $status = Status::newFatal(
'invalid-chunk-offset' );
276 private function updateChunkStatus() {
277 wfDebug( __METHOD__ .
" update chunk status for {$this->mFileKey} offset:" .
278 $this->
getOffset() .
' inx:' . $this->getChunkIndex() );
280 $dbw = $this->repo->getPrimaryDB();
281 $dbw->newUpdateQueryBuilder()
282 ->update(
'uploadstash' )
284 'us_status' =>
'chunks',
285 'us_chunk_inx' => $this->getChunkIndex(),
288 ->where( [
'us_key' => $this->mFileKey ] )
289 ->caller( __METHOD__ )->execute();
295 private function getChunkStatus() {
298 $dbw = $this->repo->getPrimaryDB();
299 $row = $dbw->newSelectQueryBuilder()
300 ->select( [
'us_chunk_inx',
'us_size',
'us_path' ] )
301 ->from(
'uploadstash' )
302 ->where( [
'us_key' => $this->mFileKey ] )
303 ->caller( __METHOD__ )->fetchRow();
306 $this->mChunkIndex = $row->us_chunk_inx;
307 $this->mOffset = $row->us_size;
308 $this->mVirtualTempPath = $row->us_path;
316 private function getChunkIndex() {
317 if ( $this->mChunkIndex !==
null ) {
329 if ( $this->mOffset !==
null ) {
343 private function outputChunk( $chunkPath ) {
345 $fileKey = $this->getChunkFileKey();
348 $hashPath = $this->repo->getHashPath( $fileKey );
349 $storeStatus = $this->repo->quickImport( $chunkPath,
350 $this->repo->getZonePath(
'temp' ) .
"/{$hashPath}{$fileKey}" );
353 if ( !$storeStatus->isOK() ) {
354 $error = $this->logFileBackendStatus(
356 '[{type}] Error storing chunk in "{chunkKey}" for {fileKey} ({details})',
357 [
'chunkPath' => $chunkPath,
'fileKey' => $fileKey ]
360 implode(
'; ', $error ), [
'chunkPath' => $chunkPath ] );
366 private function getChunkFileKey( $index =
null ) {
367 return $this->mFileKey .
'.' . ( $index ?? $this->getChunkIndex() );
375 private function verifyChunk() {
379 $this->mTitle =
false;
381 $this->mDesiredDestName = $oldDesiredDestName;
382 $this->mTitle =
false;
383 if ( is_array( $res ) ) {
397 private function logFileBackendStatus(
Status $status,
string $logMessage, array $context = [] ): array {
399 $errorToThrow =
null;
400 $warningToThrow =
null;
402 foreach ( $status->
getErrors() as $errorItem ) {
405 $logMessageType = str_replace(
'{type}', $errorItem[
'message'], $logMessage );
409 $context[
'details'] = implode(
'; ', $errorItem[
'params'] );
411 if ( $errorItem[
'type'] ===
'error' ) {
413 $errorToThrow ??= array_merge( [ $errorItem[
'message'] ], $errorItem[
'params'] );
414 $logger->error( $logMessage, $context );
417 $warningToThrow ??= array_merge( [ $errorItem[
'message'] ], $errorItem[
'params'] );
418 $logger->warning( $logMessage, $context );
421 return $errorToThrow ?? $warningToThrow ?? [
'unknown',
'no error recorded' ];
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
msg( $key, $fallback,... $params)
Get a message from i18n.
getErrors()
Get the list of errors.
isOK()
Returns whether the operation completed.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
UploadStashFile null $mStashFile
runUploadStashFileHook(User $user)
verifyPartialFile()
A verification routine suitable for partial files.
getVerificationErrorCode( $error)
string null $mDesiredDestName
setTempFile( $tempPath, $fileSize=null)
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
static getMaxUploadSize( $forType=null)
Get MediaWiki's maximum uploaded file size for a given type of upload, based on $wgMaxUploadSize.
string null $mTempPath
Local file system path to the file to upload (or a local copy)
Implements uploading from chunks.
addChunk( $chunkPath, $chunkSize, $offset)
Add a chunk to the temporary directory.
doStashFile(User $user=null)
Calls the parent doStashFile and updates the uploadsession table to handle "chunks".
continueChunks( $name, $key, $webRequestUpload)
Continue chunk uploading.
tryStashFile(User $user, $isPartial=false)
Like stashFile(), but respects extensions' wishes to prevent the stashing.verifyUpload() must be call...
__construct(User $user, $stash=false, $repo=false)
@noinspection PhpMissingParentConstructorInspection
getOffset()
Get the offset at which the next uploaded chunk will be appended to.
concatenateChunks()
Append the final chunk and ready file for parent::performUpload()
Implements regular file uploads.
UploadStash is intended to accomplish a few things:
if(!is_readable( $file)) $ext