61 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
67 wfDebug( __METHOD__ .
" creating new UploadFromChunks instance for " .
$user->
getId() );
68 $this->stash =
new UploadStash( $this->repo, $this->user );
79 return Status::newFatal( $e->
msg );
82 return parent::tryStashFile(
$user, $isPartial );
93 $this->mChunkIndex = 0;
97 $this->mStashFile = parent::doStashFile(
$user );
99 $this->mOffset = $this->mStashFile->getSize();
100 $this->mFileKey = $this->mStashFile->getFileKey();
103 $this->outputChunk( $this->mStashFile->getPath() );
106 $this->updateChunkStatus();
119 $this->mFileKey = $key;
120 $this->mUpload = $webRequestUpload;
122 $this->getChunkStatus();
124 $metadata = $this->stash->getMetadata( $key );
127 $metadata[
'us_size'],
137 $chunkIndex = $this->getChunkIndex();
138 wfDebug( __METHOD__ .
" concatenate {$this->mChunkIndex} chunks:" .
139 $this->
getOffset() .
' inx:' . $chunkIndex );
144 for ( $i = 0; $i <= $chunkIndex; $i++ ) {
145 $fileList[] = $this->getVirtualChunkLocation( $i );
151 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
152 ->newTempFSFile(
'chunkedupload_',
$ext );
156 $tmpPath = $tmpFile->bind( $this )->getPath();
160 $tStart = microtime(
true );
162 $tAmount = microtime(
true ) - $tStart;
163 if ( !$status->isOK() ) {
167 wfDebugLog(
'fileconcatenate',
"Combined $i chunks in $tAmount seconds." );
174 wfDebugLog(
'fileconcatenate',
"Verification failed for chunked upload" );
182 $tStart = microtime(
true );
187 $status->fatal( ...$error );
191 $this->mStashFile = parent::doStashFile( $this->user );
193 $status->fatal(
'uploadstash-exception', get_class( $e ), $e->getMessage() );
197 $tAmount = microtime(
true ) - $tStart;
199 $this->mStashFile->setLocalReference( $tmpFile );
200 wfDebugLog(
'fileconcatenate',
"Stashed combined file ($i chunks) in $tAmount seconds." );
210 private function getVirtualChunkLocation( $index ) {
211 return $this->repo->getVirtualUrl(
'temp' ) .
213 $this->repo->getHashPath(
214 $this->getChunkFileKey( $index )
216 $this->getChunkFileKey( $index );
227 public function addChunk( $chunkPath, $chunkSize, $offset ) {
232 $status = Status::newFatal(
'file-too-large' );
235 if ( $preAppendOffset == $offset ) {
237 $this->mChunkIndex++;
239 # For some reason mTempPath is set to first part
241 $this->mTempPath = $chunkPath;
242 $this->verifyChunk();
243 $this->mTempPath = $oldTemp;
245 return Status::newFatal( $e->
msg );
247 $status = $this->outputChunk( $chunkPath );
248 if ( $status->isGood() ) {
250 $this->mOffset = $preAppendOffset + $chunkSize;
252 $this->updateChunkStatus();
255 $status = Status::newFatal(
'invalid-chunk-offset' );
265 private function updateChunkStatus() {
266 wfDebug( __METHOD__ .
" update chunk status for {$this->mFileKey} offset:" .
267 $this->
getOffset() .
' inx:' . $this->getChunkIndex() );
269 $dbw = $this->repo->getPrimaryDB();
273 'us_status' =>
'chunks',
274 'us_chunk_inx' => $this->getChunkIndex(),
277 [
'us_key' => $this->mFileKey ],
285 private function getChunkStatus() {
288 $dbw = $this->repo->getPrimaryDB();
289 $row = $dbw->selectRow(
296 [
'us_key' => $this->mFileKey ],
301 $this->mChunkIndex = $row->us_chunk_inx;
302 $this->mOffset = $row->us_size;
303 $this->mVirtualTempPath = $row->us_path;
311 private function getChunkIndex() {
312 if ( $this->mChunkIndex !==
null ) {
324 if ( $this->mOffset !==
null ) {
338 private function outputChunk( $chunkPath ) {
340 $fileKey = $this->getChunkFileKey();
343 $hashPath = $this->repo->getHashPath( $fileKey );
344 $storeStatus = $this->repo->quickImport( $chunkPath,
345 $this->repo->getZonePath(
'temp' ) .
"/{$hashPath}{$fileKey}" );
348 if ( !$storeStatus->isOK() ) {
349 $error = $storeStatus->getErrorsArray();
350 $error = reset( $error );
351 if ( !count( $error ) ) {
352 $error = $storeStatus->getWarningsArray();
353 $error = reset( $error );
354 if ( !count( $error ) ) {
355 $error = [
'unknown',
'no error recorded' ];
359 implode(
'; ', $error ) );
365 private function getChunkFileKey( $index =
null ) {
366 if ( $index ===
null ) {
367 $index = $this->getChunkIndex();
370 return $this->mFileKey .
'.' . $index;
378 private function verifyChunk() {
382 $this->mTitle =
false;
384 $this->mDesiredDestName = $oldDesiredDestName;
385 $this->mTitle =
false;
386 if ( is_array(
$res ) ) {
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.
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 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:
getId( $wikiId=self::LOCAL)
Get the user's ID.
if(!is_readable( $file)) $ext