2011-11-30 14:56:40 +00:00
|
|
|
<?php
|
2019-08-16 10:00:15 +00:00
|
|
|
|
2024-06-16 18:23:55 +00:00
|
|
|
use MediaWiki\Deferred\AutoCommitUpdate;
|
|
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2023-05-11 20:15:17 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2019-08-16 10:00:15 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\WebRequestUpload;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-02-16 23:15:49 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2024-05-19 13:40:05 +00:00
|
|
|
use Wikimedia\FileBackend\FileBackend;
|
2019-08-16 10:00:15 +00:00
|
|
|
|
2012-06-05 22:58:54 +00:00
|
|
|
/**
|
|
|
|
|
* Backend for uploading files from chunks.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Implements uploading from chunks
|
|
|
|
|
*
|
2012-02-08 17:03:43 +00:00
|
|
|
* @ingroup Upload
|
2011-11-30 14:56:40 +00:00
|
|
|
* @author Michael Dale
|
|
|
|
|
*/
|
|
|
|
|
class UploadFromChunks extends UploadFromFile {
|
2019-07-06 06:00:53 +00:00
|
|
|
/** @var LocalRepo */
|
|
|
|
|
private $repo;
|
|
|
|
|
/** @var UploadStash */
|
|
|
|
|
public $stash;
|
|
|
|
|
/** @var User */
|
|
|
|
|
public $user;
|
|
|
|
|
|
2024-09-01 18:20:46 +00:00
|
|
|
/** @var int|null */
|
2013-10-04 13:43:09 +00:00
|
|
|
protected $mOffset;
|
2024-09-01 18:20:46 +00:00
|
|
|
/** @var int|null */
|
2013-10-04 13:43:09 +00:00
|
|
|
protected $mChunkIndex;
|
2024-09-01 18:20:46 +00:00
|
|
|
/** @var string */
|
2013-10-04 13:43:09 +00:00
|
|
|
protected $mFileKey;
|
2024-09-01 18:20:46 +00:00
|
|
|
/** @var string|null */
|
2013-10-04 13:43:09 +00:00
|
|
|
protected $mVirtualTempPath;
|
2019-07-06 06:00:53 +00:00
|
|
|
|
2024-02-16 23:15:49 +00:00
|
|
|
private LoggerInterface $logger;
|
|
|
|
|
|
2019-07-06 06:00:53 +00:00
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */
|
2012-08-09 15:20:09 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
2013-04-11 05:29:05 +00:00
|
|
|
* Setup local pointers to stash, repo and user (similar to UploadFromStash)
|
2012-08-09 15:20:09 +00:00
|
|
|
*
|
2016-08-17 13:31:22 +00:00
|
|
|
* @param User $user
|
2022-07-31 00:02:18 +00:00
|
|
|
* @param UploadStash|false $stash Default: false
|
|
|
|
|
* @param FileRepo|false $repo Default: false
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2016-08-17 13:31:22 +00:00
|
|
|
public function __construct( User $user, $stash = false, $repo = false ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->user = $user;
|
|
|
|
|
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( $repo ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->repo = $repo;
|
|
|
|
|
} else {
|
2020-03-08 21:38:47 +00:00
|
|
|
$this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( $stash ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->stash = $stash;
|
|
|
|
|
} else {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() );
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->stash = new UploadStash( $this->repo, $this->user );
|
|
|
|
|
}
|
2024-02-16 23:15:49 +00:00
|
|
|
|
|
|
|
|
$this->logger = LoggerFactory::getInstance( 'upload' );
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-11-19 08:07:50 +00:00
|
|
|
|
2016-12-15 13:34:13 +00:00
|
|
|
/**
|
2017-08-11 14:49:52 +00:00
|
|
|
* @inheritDoc
|
2016-12-15 13:34:13 +00:00
|
|
|
*/
|
|
|
|
|
public function tryStashFile( User $user, $isPartial = false ) {
|
|
|
|
|
try {
|
|
|
|
|
$this->verifyChunk();
|
|
|
|
|
} catch ( UploadChunkVerificationException $e ) {
|
|
|
|
|
return Status::newFatal( $e->msg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::tryStashFile( $user, $isPartial );
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
Introduce UploadStashFile hook, improve API handling of stash errors
UploadBase:
* Introduce a new method, tryStashFile(), as a replacement for the
now-soft-deprecated stashFile(). The method runs the new hook and
returns a Status object, with an error (if the hook returned an
error) or a value (if it didn't).
* Introduce a new hook, UploadStashFile, allowing extensions to
prevent a file from being stashed. Note that code in extensions
which has not been updated for MediaWiki 1.28 may still call
stashFile() directly, and therefore not call this hook. For
important checks (not just for UI), extension authors should use
UploadVerifyFile or UploadVerifyUpload hooks.
* Extract common code of tryStashFile() and stashFile() to
a new protected method doStashFile().
SpecialUpload:
* Use tryStashFile() when stashing a file after a warning or
"recoverable error" was encountered.
ApiUpload:
* Refactor stashing code so that error handling only happens in one
place, not four different ones. Use Status objects rather than
exception throwing/catching for control flow.
* Simplify the error messages slightly (error codes are unchanged).
Produce better ones by always using handleStashException().
'stashfailed' is now always at root (not nested inside 'warnings'),
behaving the same as 'filekey' does on success.
* Use tryStashFile() when stashing. Handle errors so as to allow
custom API results passed via ApiMessage to be preserved.
Some API result changes for different requests are shown below.
api.php?action=upload&format=json&filename=good.png&file=...&stash=1
Before:
{
"error": {
"code": "stashfilestorage",
"info": "Could not store upload in the stash: Stashing temporary file failed: UploadStashFileException Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\".",
"*": "See http://localhost:3080/w/api.php for API usage"
}
}
After:
{
"error": {
"code": "stashfilestorage",
"info": "Could not store upload in the stash: Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\".",
"*": "See http://localhost:3080/w/api.php for API usage"
}
}
api.php?action=upload&format=json&filename=[bad].png&file=...
Before:
{
"upload": {
"result": "Warning",
"warnings": {
"badfilename": "-bad-.png",
"stashfailed": "Stashing temporary file failed: UploadStashFileException Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\"."
}
}
}
After:
{
"upload": {
"result": "Warning",
"stashfailed": "Could not store upload in the stash: Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\"."
"warnings": {
"badfilename": "-bad-.png",
}
}
}
Bug: T140521
Change-Id: I2f574b355cd33b2e9fa7ff8e1793503b257cce65
2016-08-03 00:13:01 +00:00
|
|
|
* Calls the parent doStashFile and updates the uploadsession table to handle "chunks"
|
2011-11-30 14:56:40 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param User|null $user
|
2014-07-24 17:43:44 +00:00
|
|
|
* @return UploadStashFile Stashed file
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
protected function doStashFile( ?User $user = null ) {
|
2012-09-09 04:40:17 +00:00
|
|
|
// Stash file is the called on creating a new chunk session:
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->mChunkIndex = 0;
|
|
|
|
|
$this->mOffset = 0;
|
2013-05-16 22:09:44 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
// Create a local stash target
|
2016-08-17 18:43:55 +00:00
|
|
|
$this->mStashFile = parent::doStashFile( $user );
|
2013-04-11 05:29:05 +00:00
|
|
|
// Update the initial file offset (based on file size)
|
2016-08-17 18:43:55 +00:00
|
|
|
$this->mOffset = $this->mStashFile->getSize();
|
|
|
|
|
$this->mFileKey = $this->mStashFile->getFileKey();
|
2011-11-30 14:56:40 +00:00
|
|
|
|
|
|
|
|
// Output a copy of this first to chunk 0 location:
|
2016-08-17 18:43:55 +00:00
|
|
|
$this->outputChunk( $this->mStashFile->getPath() );
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2012-09-09 04:40:17 +00:00
|
|
|
// Update db table to reflect initial "chunk" state
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->updateChunkStatus();
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2016-08-17 18:43:55 +00:00
|
|
|
return $this->mStashFile;
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-08-09 15:20:09 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Continue chunk uploading
|
2014-04-19 15:19:17 +00:00
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param WebRequestUpload $webRequestUpload
|
2012-09-09 04:40:17 +00:00
|
|
|
*/
|
2011-11-30 14:56:40 +00:00
|
|
|
public function continueChunks( $name, $key, $webRequestUpload ) {
|
|
|
|
|
$this->mFileKey = $key;
|
|
|
|
|
$this->mUpload = $webRequestUpload;
|
2012-09-09 04:40:17 +00:00
|
|
|
// Get the chunk status form the db:
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->getChunkStatus();
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
$metadata = $this->stash->getMetadata( $key );
|
|
|
|
|
$this->initializePathInfo( $name,
|
2011-12-23 18:59:39 +00:00
|
|
|
$this->getRealPath( $metadata['us_path'] ),
|
2011-11-30 14:56:40 +00:00
|
|
|
$metadata['us_size'],
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Append the final chunk and ready file for parent::performUpload()
|
2016-12-21 06:21:34 +00:00
|
|
|
* @return Status
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
|
|
|
|
public function concatenateChunks() {
|
2024-02-21 23:43:05 +00:00
|
|
|
$oldFileKey = $this->mFileKey;
|
2013-10-04 13:43:09 +00:00
|
|
|
$chunkIndex = $this->getChunkIndex();
|
2024-02-16 23:15:49 +00:00
|
|
|
$this->logger->debug(
|
|
|
|
|
__METHOD__ . ' concatenate {totalChunks} chunks: {offset} inx: {curIndex}',
|
|
|
|
|
[
|
|
|
|
|
'offset' => $this->getOffset(),
|
|
|
|
|
'totalChunks' => $this->mChunkIndex,
|
|
|
|
|
'curIndex' => $chunkIndex,
|
2024-02-21 23:43:05 +00:00
|
|
|
'filekey' => $oldFileKey
|
2024-02-16 23:15:49 +00:00
|
|
|
]
|
|
|
|
|
);
|
2011-12-23 18:59:39 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
// Concatenate all the chunks to mVirtualTempPath
|
2016-02-17 09:09:32 +00:00
|
|
|
$fileList = [];
|
2011-11-30 14:56:40 +00:00
|
|
|
// The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
|
2013-10-04 13:43:09 +00:00
|
|
|
for ( $i = 0; $i <= $chunkIndex; $i++ ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
$fileList[] = $this->getVirtualChunkLocation( $i );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-23 18:59:39 +00:00
|
|
|
// Get the file extension from the last chunk
|
|
|
|
|
$ext = FileBackend::extensionFromPath( $this->mVirtualTempPath );
|
|
|
|
|
// Get a 0-byte temp file to perform the concatenation at
|
2019-08-16 10:00:15 +00:00
|
|
|
$tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
|
2019-08-28 19:08:58 +00:00
|
|
|
->newTempFSFile( 'chunkedupload_', $ext );
|
2013-10-04 13:43:09 +00:00
|
|
|
$tmpPath = false; // fail in concatenate()
|
2013-11-19 18:03:54 +00:00
|
|
|
if ( $tmpFile ) {
|
2013-10-04 13:43:09 +00:00
|
|
|
// keep alive with $this
|
|
|
|
|
$tmpPath = $tmpFile->bind( $this )->getPath();
|
2024-02-16 23:15:49 +00:00
|
|
|
} else {
|
2024-02-21 23:43:05 +00:00
|
|
|
$this->logger->warning( "Error getting tmp file", [ 'filekey' => $oldFileKey ] );
|
2013-10-04 13:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-23 18:59:39 +00:00
|
|
|
// Concatenate the chunks at the temp file
|
2012-11-18 10:32:50 +00:00
|
|
|
$tStart = microtime( true );
|
2024-02-23 11:45:20 +00:00
|
|
|
$status = $this->repo->concatenate( $fileList, $tmpPath );
|
2012-11-18 10:32:50 +00:00
|
|
|
$tAmount = microtime( true ) - $tStart;
|
2016-03-19 00:08:06 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2023-05-11 20:15:17 +00:00
|
|
|
// This is a backend error and not user-related, so log is safe
|
|
|
|
|
// Upload verification further on is not safe to log server side
|
|
|
|
|
$this->logFileBackendStatus(
|
|
|
|
|
$status,
|
|
|
|
|
'[{type}] Error on concatenate {chunks} stashed files ({details})',
|
2024-02-21 23:43:05 +00:00
|
|
|
[ 'chunks' => $chunkIndex, 'filekey' => $oldFileKey ]
|
2023-05-11 20:15:17 +00:00
|
|
|
);
|
2012-09-09 04:40:17 +00:00
|
|
|
return $status;
|
2024-02-23 11:45:20 +00:00
|
|
|
} else {
|
|
|
|
|
// Delete old chunks in deferred job. Put in deferred job because deleting
|
|
|
|
|
// lots of chunks can take a long time, sometimes to the point of causing
|
|
|
|
|
// a timeout, and we do not want that to tank the operation. Note that chunks
|
|
|
|
|
// are also automatically deleted after a set time by cleanupUploadStash.php
|
|
|
|
|
// Additionally, using AutoCommitUpdate ensures that we do not delete files
|
|
|
|
|
// if the main transaction is rolled back for some reason.
|
|
|
|
|
DeferredUpdates::addUpdate( new AutoCommitUpdate(
|
|
|
|
|
$this->repo->getPrimaryDB(),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
function () use( $fileList, $oldFileKey ) {
|
|
|
|
|
$status = $this->repo->quickPurgeBatch( $fileList );
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->logger->warning(
|
|
|
|
|
"Could not delete chunks of {filekey} - {status}",
|
|
|
|
|
[
|
|
|
|
|
'status' => (string)$status,
|
|
|
|
|
'filekey' => $oldFileKey,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
) );
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2016-03-04 20:05:12 +00:00
|
|
|
|
2014-02-04 21:16:13 +00:00
|
|
|
wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds." );
|
2013-05-16 22:09:44 +00:00
|
|
|
|
2016-03-02 23:42:36 +00:00
|
|
|
// File system path of the actual full temp file
|
|
|
|
|
$this->setTempFile( $tmpPath );
|
|
|
|
|
|
2013-05-16 22:09:44 +00:00
|
|
|
$ret = $this->verifyUpload();
|
|
|
|
|
if ( $ret['status'] !== UploadBase::OK ) {
|
2024-02-21 23:43:05 +00:00
|
|
|
$this->logger->info(
|
|
|
|
|
"Verification failed for chunked upload {filekey}",
|
|
|
|
|
[
|
|
|
|
|
'user' => $this->user->getName(),
|
|
|
|
|
'filekey' => $oldFileKey
|
|
|
|
|
]
|
|
|
|
|
);
|
2013-05-16 22:09:44 +00:00
|
|
|
$status->fatal( $this->getVerificationErrorCode( $ret['status'] ) );
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2013-05-16 22:09:44 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-17 18:43:55 +00:00
|
|
|
// Update the mTempPath and mStashFile
|
2013-04-11 05:29:05 +00:00
|
|
|
// (for FileUpload or normal Stash to take over)
|
2012-11-18 10:32:50 +00:00
|
|
|
$tStart = microtime( true );
|
2016-08-17 13:31:22 +00:00
|
|
|
// This is a re-implementation of UploadBase::tryStashFile(), we can't call it because we
|
|
|
|
|
// override doStashFile() with completely different functionality in this class...
|
|
|
|
|
$error = $this->runUploadStashFileHook( $this->user );
|
|
|
|
|
if ( $error ) {
|
2018-06-08 02:58:35 +00:00
|
|
|
$status->fatal( ...$error );
|
2024-02-16 23:15:49 +00:00
|
|
|
$this->logger->info( "Aborting stash upload due to hook - {status}",
|
|
|
|
|
[
|
|
|
|
|
'status' => (string)$status,
|
|
|
|
|
'user' => $this->user->getName(),
|
|
|
|
|
'filekey' => $this->mFileKey
|
|
|
|
|
]
|
|
|
|
|
);
|
2016-08-17 13:31:22 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2016-08-17 18:43:55 +00:00
|
|
|
$this->mStashFile = parent::doStashFile( $this->user );
|
2016-08-17 13:31:22 +00:00
|
|
|
} catch ( UploadStashException $e ) {
|
2024-02-16 23:15:49 +00:00
|
|
|
$this->logger->warning( "Could not stash file for {user} because {error} {msg}",
|
|
|
|
|
[
|
|
|
|
|
'user' => $this->user->getName(),
|
|
|
|
|
'error' => get_class( $e ),
|
|
|
|
|
'msg' => $e->getMessage(),
|
|
|
|
|
'filekey' => $this->mFileKey
|
|
|
|
|
]
|
|
|
|
|
);
|
2016-08-17 13:31:22 +00:00
|
|
|
$status->fatal( 'uploadstash-exception', get_class( $e ), $e->getMessage() );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 10:32:50 +00:00
|
|
|
$tAmount = microtime( true ) - $tStart;
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable tmpFile is set when tmpPath is set here
|
2016-08-17 18:43:55 +00:00
|
|
|
$this->mStashFile->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo())
|
2024-02-21 23:43:05 +00:00
|
|
|
$this->logger->info( "Stashed combined ({chunks} chunks) of {oldkey} under new name {filekey}",
|
|
|
|
|
[
|
|
|
|
|
'chunks' => $i,
|
|
|
|
|
'stashTime' => $tAmount,
|
|
|
|
|
'oldpath' => $this->mVirtualTempPath,
|
|
|
|
|
'filekey' => $this->mStashFile->getFileKey(),
|
|
|
|
|
'oldkey' => $oldFileKey,
|
|
|
|
|
'newpath' => $this->mStashFile->getPath(),
|
|
|
|
|
'user' => $this->user->getName()
|
|
|
|
|
]
|
|
|
|
|
);
|
2014-02-04 21:16:13 +00:00
|
|
|
wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds." );
|
2011-12-27 04:49:08 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
2012-09-09 04:40:17 +00:00
|
|
|
* Returns the virtual chunk location:
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param int $index
|
2012-02-09 21:36:14 +00:00
|
|
|
* @return string
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2020-05-10 08:32:47 +00:00
|
|
|
private function getVirtualChunkLocation( $index ) {
|
2012-09-09 04:40:17 +00:00
|
|
|
return $this->repo->getVirtualUrl( 'temp' ) .
|
2014-05-09 14:53:19 +00:00
|
|
|
'/' .
|
|
|
|
|
$this->repo->getHashPath(
|
|
|
|
|
$this->getChunkFileKey( $index )
|
|
|
|
|
) .
|
|
|
|
|
$this->getChunkFileKey( $index );
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-02-09 19:29:36 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Add a chunk to the temporary directory
|
|
|
|
|
*
|
2014-07-24 17:43:44 +00:00
|
|
|
* @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 )
|
2011-11-30 14:56:40 +00:00
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function addChunk( $chunkPath, $chunkSize, $offset ) {
|
|
|
|
|
// Get the offset before we add the chunk to the file system
|
|
|
|
|
$preAppendOffset = $this->getOffset();
|
2012-09-09 04:40:17 +00:00
|
|
|
|
2013-03-24 10:01:51 +00:00
|
|
|
if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize() ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
$status = Status::newFatal( 'file-too-large' );
|
|
|
|
|
} else {
|
|
|
|
|
// Make sure the client is uploading the correct chunk with a matching offset.
|
|
|
|
|
if ( $preAppendOffset == $offset ) {
|
2012-09-09 04:40:17 +00:00
|
|
|
// Update local chunk index for the current chunk
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->mChunkIndex++;
|
2013-05-16 22:09:44 +00:00
|
|
|
try {
|
|
|
|
|
# For some reason mTempPath is set to first part
|
|
|
|
|
$oldTemp = $this->mTempPath;
|
|
|
|
|
$this->mTempPath = $chunkPath;
|
|
|
|
|
$this->verifyChunk();
|
|
|
|
|
$this->mTempPath = $oldTemp;
|
|
|
|
|
} catch ( UploadChunkVerificationException $e ) {
|
2024-02-16 23:15:49 +00:00
|
|
|
$this->logger->info( "Error verifying upload chunk {msg}",
|
|
|
|
|
[
|
|
|
|
|
'user' => $this->user->getName(),
|
|
|
|
|
'msg' => $e->getMessage(),
|
|
|
|
|
'chunkIndex' => $this->mChunkIndex,
|
|
|
|
|
'filekey' => $this->mFileKey
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2016-12-13 14:11:43 +00:00
|
|
|
return Status::newFatal( $e->msg );
|
2013-05-16 22:09:44 +00:00
|
|
|
}
|
2011-11-30 14:56:40 +00:00
|
|
|
$status = $this->outputChunk( $chunkPath );
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( $status->isGood() ) {
|
2012-09-09 04:40:17 +00:00
|
|
|
// Update local offset:
|
2011-11-30 14:56:40 +00:00
|
|
|
$this->mOffset = $preAppendOffset + $chunkSize;
|
2012-09-09 04:40:17 +00:00
|
|
|
// Update chunk table status db
|
|
|
|
|
$this->updateChunkStatus();
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$status = Status::newFatal( 'invalid-chunk-offset' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
2012-09-09 04:40:17 +00:00
|
|
|
* Update the chunk db table with the current status:
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
private function updateChunkStatus() {
|
2024-02-16 23:15:49 +00:00
|
|
|
$this->logger->info( "update chunk status for {filekey} offset: {offset} inx: {inx}",
|
|
|
|
|
[
|
|
|
|
|
'offset' => $this->getOffset(),
|
|
|
|
|
'inx' => $this->getChunkIndex(),
|
|
|
|
|
'filekey' => $this->mFileKey,
|
|
|
|
|
'user' => $this->user->getName()
|
|
|
|
|
]
|
|
|
|
|
);
|
2012-08-09 15:20:09 +00:00
|
|
|
|
2021-04-19 01:32:42 +00:00
|
|
|
$dbw = $this->repo->getPrimaryDB();
|
2023-06-07 22:07:31 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
|
->update( 'uploadstash' )
|
|
|
|
|
->set( [
|
2011-11-30 14:56:40 +00:00
|
|
|
'us_status' => 'chunks',
|
|
|
|
|
'us_chunk_inx' => $this->getChunkIndex(),
|
|
|
|
|
'us_size' => $this->getOffset()
|
2023-06-07 22:07:31 +00:00
|
|
|
] )
|
|
|
|
|
->where( [ 'us_key' => $this->mFileKey ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get the chunk db state and populate update relevant local values
|
|
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
private function getChunkStatus() {
|
2021-05-14 20:04:02 +00:00
|
|
|
// get primary db to avoid race conditions.
|
2011-12-20 23:02:42 +00:00
|
|
|
// Otherwise, if chunk upload time < replag there will be spurious errors
|
2021-04-19 01:32:42 +00:00
|
|
|
$dbw = $this->repo->getPrimaryDB();
|
2023-09-19 11:06:53 +00:00
|
|
|
$row = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'us_chunk_inx', 'us_size', 'us_path' ] )
|
|
|
|
|
->from( 'uploadstash' )
|
|
|
|
|
->where( [ 'us_key' => $this->mFileKey ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchRow();
|
2011-11-30 14:56:40 +00:00
|
|
|
// Handle result:
|
|
|
|
|
if ( $row ) {
|
|
|
|
|
$this->mChunkIndex = $row->us_chunk_inx;
|
|
|
|
|
$this->mOffset = $row->us_size;
|
|
|
|
|
$this->mVirtualTempPath = $row->us_path;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
2012-09-09 04:40:17 +00:00
|
|
|
* Get the current Chunk index
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return int Index of the current chunk
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
private function getChunkIndex() {
|
2024-08-08 14:01:09 +00:00
|
|
|
return $this->mChunkIndex ?? 0;
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
2015-02-12 15:59:09 +00:00
|
|
|
* Get the offset at which the next uploaded chunk will be appended to
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return int Current byte offset of the chunk file set
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2015-02-12 15:59:09 +00:00
|
|
|
public function getOffset() {
|
2024-08-08 14:01:09 +00:00
|
|
|
return $this->mOffset ?? 0;
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2012-08-07 23:47:25 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
/**
|
|
|
|
|
* Output the chunk to disk
|
2012-08-09 15:20:09 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $chunkPath
|
2012-08-09 15:20:09 +00:00
|
|
|
* @throws UploadChunkFileException
|
2016-12-21 06:21:34 +00:00
|
|
|
* @return Status
|
2011-11-30 14:56:40 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
private function outputChunk( $chunkPath ) {
|
2011-11-30 14:56:40 +00:00
|
|
|
// Key is fileKey + chunk index
|
|
|
|
|
$fileKey = $this->getChunkFileKey();
|
2012-09-09 04:40:17 +00:00
|
|
|
|
|
|
|
|
// Store the chunk per its indexed fileKey:
|
2012-01-01 23:28:18 +00:00
|
|
|
$hashPath = $this->repo->getHashPath( $fileKey );
|
2012-09-09 04:40:17 +00:00
|
|
|
$storeStatus = $this->repo->quickImport( $chunkPath,
|
|
|
|
|
$this->repo->getZonePath( 'temp' ) . "/{$hashPath}{$fileKey}" );
|
|
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
// Check for error in stashing the chunk:
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !$storeStatus->isOK() ) {
|
2023-05-11 20:15:17 +00:00
|
|
|
$error = $this->logFileBackendStatus(
|
|
|
|
|
$storeStatus,
|
2024-01-30 21:01:34 +00:00
|
|
|
'[{type}] Error storing chunk in "{chunkPath}" for {fileKey} ({details})',
|
2023-05-11 20:15:17 +00:00
|
|
|
[ 'chunkPath' => $chunkPath, 'fileKey' => $fileKey ]
|
|
|
|
|
);
|
2023-03-10 05:31:09 +00:00
|
|
|
throw new UploadChunkFileException( "Error storing file in '{chunkPath}': " .
|
|
|
|
|
implode( '; ', $error ), [ 'chunkPath' => $chunkPath ] );
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-11-30 14:56:40 +00:00
|
|
|
return $storeStatus;
|
|
|
|
|
}
|
2012-08-09 15:20:09 +00:00
|
|
|
|
2012-12-20 15:09:25 +00:00
|
|
|
private function getChunkFileKey( $index = null ) {
|
2023-02-14 08:44:27 +00:00
|
|
|
return $this->mFileKey . '.' . ( $index ?? $this->getChunkIndex() );
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|
2013-05-16 22:09:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify that the chunk isn't really an evil html file
|
|
|
|
|
*
|
|
|
|
|
* @throws UploadChunkVerificationException
|
|
|
|
|
*/
|
|
|
|
|
private function verifyChunk() {
|
|
|
|
|
// Rest mDesiredDestName here so we verify the name as if it were mFileKey
|
|
|
|
|
$oldDesiredDestName = $this->mDesiredDestName;
|
|
|
|
|
$this->mDesiredDestName = $this->mFileKey;
|
|
|
|
|
$this->mTitle = false;
|
|
|
|
|
$res = $this->verifyPartialFile();
|
|
|
|
|
$this->mDesiredDestName = $oldDesiredDestName;
|
|
|
|
|
$this->mTitle = false;
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( is_array( $res ) ) {
|
2016-12-13 14:11:43 +00:00
|
|
|
throw new UploadChunkVerificationException( $res );
|
2013-05-16 22:09:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 20:15:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log a status object from FileBackend functions (via FileRepo functions) to the upload log channel.
|
|
|
|
|
* Return a array with the first error to build up a exception message
|
|
|
|
|
*
|
|
|
|
|
* @param Status $status
|
|
|
|
|
* @param string $logMessage
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function logFileBackendStatus( Status $status, string $logMessage, array $context = [] ): array {
|
2024-02-16 23:15:49 +00:00
|
|
|
$logger = $this->logger;
|
2023-05-11 20:15:17 +00:00
|
|
|
$errorToThrow = null;
|
|
|
|
|
$warningToThrow = null;
|
|
|
|
|
|
|
|
|
|
foreach ( $status->getErrors() as $errorItem ) {
|
|
|
|
|
// The message key stands for distinct error situation from the file backend,
|
|
|
|
|
// each error situation should be shown up in aggregated stats as own point, replace in message
|
|
|
|
|
$logMessageType = str_replace( '{type}', $errorItem['message'], $logMessage );
|
|
|
|
|
|
|
|
|
|
// The message arguments often contains the name of the failing datacenter or file names
|
|
|
|
|
// and should not show up in aggregated stats, add to context
|
|
|
|
|
$context['details'] = implode( '; ', $errorItem['params'] );
|
2024-02-16 23:15:49 +00:00
|
|
|
$context['user'] = $this->user->getName();
|
2023-05-11 20:15:17 +00:00
|
|
|
|
|
|
|
|
if ( $errorItem['type'] === 'error' ) {
|
|
|
|
|
// Use the first error of the list for the exception text
|
2024-01-25 14:57:50 +00:00
|
|
|
$errorToThrow ??= [ $errorItem['message'], ...$errorItem['params'] ];
|
2024-01-30 21:01:34 +00:00
|
|
|
$logger->error( $logMessageType, $context );
|
2023-05-11 20:15:17 +00:00
|
|
|
} else {
|
|
|
|
|
// When no error is found, fall back to the first warning
|
2024-01-25 14:57:50 +00:00
|
|
|
$warningToThrow ??= [ $errorItem['message'], ...$errorItem['params'] ];
|
2024-01-30 21:01:34 +00:00
|
|
|
$logger->warning( $logMessageType, $context );
|
2023-05-11 20:15:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $errorToThrow ?? $warningToThrow ?? [ 'unknown', 'no error recorded' ];
|
|
|
|
|
}
|
2011-11-30 14:56:40 +00:00
|
|
|
}
|