62 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
68 wfDebug( __METHOD__ .
" creating new UploadFromChunks instance for " .
$user->
getId() );
69 $this->stash =
new UploadStash( $this->repo, $this->user );
83 return parent::tryStashFile(
$user, $isPartial );
94 $this->mChunkIndex = 0;
98 $this->mStashFile = parent::doStashFile(
$user );
100 $this->mOffset = $this->mStashFile->getSize();
101 $this->mFileKey = $this->mStashFile->getFileKey();
104 $this->outputChunk( $this->mStashFile->getPath() );
107 $this->updateChunkStatus();
120 $this->mFileKey = $key;
121 $this->mUpload = $webRequestUpload;
123 $this->getChunkStatus();
125 $metadata = $this->stash->getMetadata( $key );
128 $metadata[
'us_size'],
138 $chunkIndex = $this->getChunkIndex();
139 wfDebug( __METHOD__ .
" concatenate {$this->mChunkIndex} chunks:" .
140 $this->
getOffset() .
' inx:' . $chunkIndex );
145 for ( $i = 0; $i <= $chunkIndex; $i++ ) {
146 $fileList[] = $this->getVirtualChunkLocation( $i );
152 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
153 ->newTempFSFile(
'chunkedupload_',
$ext );
157 $tmpPath = $tmpFile->bind( $this )->getPath();
161 $tStart = microtime(
true );
163 $tAmount = microtime(
true ) - $tStart;
164 if ( !$status->isOK() ) {
168 wfDebugLog(
'fileconcatenate',
"Combined $i chunks in $tAmount seconds." );
175 wfDebugLog(
'fileconcatenate',
"Verification failed for chunked upload" );
183 $tStart = microtime(
true );
188 $status->fatal( ...$error );
192 $this->mStashFile = parent::doStashFile( $this->user );
194 $status->fatal(
'uploadstash-exception', get_class( $e ), $e->getMessage() );
198 $tAmount = microtime(
true ) - $tStart;
200 $this->mStashFile->setLocalReference( $tmpFile );
201 wfDebugLog(
'fileconcatenate',
"Stashed combined file ($i chunks) in $tAmount seconds." );
211 private function getVirtualChunkLocation( $index ) {
212 return $this->repo->getVirtualUrl(
'temp' ) .
214 $this->repo->getHashPath(
215 $this->getChunkFileKey( $index )
217 $this->getChunkFileKey( $index );
228 public function addChunk( $chunkPath, $chunkSize, $offset ) {
236 if ( $preAppendOffset == $offset ) {
238 $this->mChunkIndex++;
240 # For some reason mTempPath is set to first part
242 $this->mTempPath = $chunkPath;
243 $this->verifyChunk();
244 $this->mTempPath = $oldTemp;
248 $status = $this->outputChunk( $chunkPath );
249 if ( $status->isGood() ) {
251 $this->mOffset = $preAppendOffset + $chunkSize;
253 $this->updateChunkStatus();
266 private function updateChunkStatus() {
267 wfDebug( __METHOD__ .
" update chunk status for {$this->mFileKey} offset:" .
268 $this->
getOffset() .
' inx:' . $this->getChunkIndex() );
270 $dbw = $this->repo->getPrimaryDB();
274 'us_status' =>
'chunks',
275 'us_chunk_inx' => $this->getChunkIndex(),
278 [
'us_key' => $this->mFileKey ],
286 private function getChunkStatus() {
289 $dbw = $this->repo->getPrimaryDB();
290 $row = $dbw->selectRow(
297 [
'us_key' => $this->mFileKey ],
302 $this->mChunkIndex = $row->us_chunk_inx;
303 $this->mOffset = $row->us_size;
304 $this->mVirtualTempPath = $row->us_path;
312 private function getChunkIndex() {
313 if ( $this->mChunkIndex !==
null ) {
325 if ( $this->mOffset !==
null ) {
339 private function outputChunk( $chunkPath ) {
341 $fileKey = $this->getChunkFileKey();
344 $hashPath = $this->repo->getHashPath( $fileKey );
345 $storeStatus = $this->repo->quickImport( $chunkPath,
346 $this->repo->getZonePath(
'temp' ) .
"/{$hashPath}{$fileKey}" );
349 if ( !$storeStatus->isOK() ) {
350 $error = $storeStatus->getErrorsArray();
351 $error = reset( $error );
352 if ( !count( $error ) ) {
353 $error = $storeStatus->getWarningsArray();
354 $error = reset( $error );
355 if ( !count( $error ) ) {
356 $error = [
'unknown',
'no error recorded' ];
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 ) ) {
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.
msg( $key, $fallback,... $params)
Get a message from i18n.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
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:
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
getId( $wikiId=self::LOCAL)
Get the user's ID.
if(!is_readable( $file)) $ext