3 * Implements uploading from chunks
8 class UploadFromChunks
extends UploadFromFile
{
9 protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath;
12 * Setup local pointers to stash, repo and user ( similar to UploadFromStash )
15 * @param $stash UploadStash
16 * @param $repo FileRepo
18 public function __construct( $user = false, $stash = false, $repo = false ) {
19 // user object. sometimes this won't exist, as when running from cron.
25 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
29 $this->stash
= $stash;
32 wfDebug( __METHOD__
. " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
34 wfDebug( __METHOD__
. " creating new UploadFromChunks instance with no user\n" );
36 $this->stash
= new UploadStash( $this->repo
, $this->user
);
42 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
44 * @return UploadStashFile stashed file
46 public function stashFile() {
47 // Stash file is the called on creating a new chunk session:
48 $this->mChunkIndex
= 0;
50 // Create a local stash target
51 $this->mLocalFile
= parent
::stashFile();
52 // Update the initial file offset ( based on file size )
53 $this->mOffset
= $this->mLocalFile
->getSize();
54 $this->mFileKey
= $this->mLocalFile
->getFileKey();
56 // Output a copy of this first to chunk 0 location:
57 $status = $this->outputChunk( $this->mLocalFile
->getPath() );
59 // Update db table to reflect initial "chunk" state
60 $this->updateChunkStatus();
61 return $this->mLocalFile
;
65 * Continue chunk uploading
67 public function continueChunks( $name, $key, $webRequestUpload ) {
68 $this->mFileKey
= $key;
69 $this->mUpload
= $webRequestUpload;
70 // Get the chunk status form the db:
71 $this->getChunkStatus();
73 $metadata = $this->stash
->getMetadata( $key );
74 $this->initializePathInfo( $name,
75 $this->getRealPath( $metadata['us_path'] ),
82 * Append the final chunk and ready file for parent::performUpload()
83 * @return FileRepoStatus
85 public function concatenateChunks() {
86 wfDebug( __METHOD__
. " concatenate {$this->mChunkIndex} chunks:" .
87 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
89 // Concatenate all the chunks to mVirtualTempPath
91 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
92 for( $i = 0; $i <= $this->getChunkIndex(); $i++
){
93 $fileList[] = $this->getVirtualChunkLocation( $i );
96 // Get the file extension from the last chunk
97 $ext = FileBackend
::extensionFromPath( $this->mVirtualTempPath
);
98 // Get a 0-byte temp file to perform the concatenation at
99 $tmpFile = TempFSFile
::factory( 'chunkedupload_', $ext );
101 ?
$tmpFile->getPath()
102 : false; // fail in concatenate()
103 // Concatenate the chunks at the temp file
104 $status = $this->repo
->concatenate( $fileList, $tmpPath, FileRepo
::DELETE_SOURCE
);
105 if( !$status->isOk() ){
108 // Update the mTempPath and mLocalFile
109 // ( for FileUpload or normal Stash to take over )
110 $this->mTempPath
= $tmpPath; // file system path
111 $this->mLocalFile
= parent
::stashFile();
117 * Perform the upload, then remove the temp copy afterward
118 * @param $comment string
119 * @param $pageText string
124 public function performUpload( $comment, $pageText, $watch, $user ) {
125 $rv = parent
::performUpload( $comment, $pageText, $watch, $user );
130 * Returns the virtual chunk location:
134 function getVirtualChunkLocation( $index ){
135 return $this->repo
->getVirtualUrl( 'temp' ) .
137 $this->repo
->getHashPath(
138 $this->getChunkFileKey( $index )
140 $this->getChunkFileKey( $index );
144 * Add a chunk to the temporary directory
146 * @param $chunkPath string path to temporary chunk file
147 * @param $chunkSize int size of the current chunk
148 * @param $offset int offset of current chunk ( mutch match database chunk offset )
151 public function addChunk( $chunkPath, $chunkSize, $offset ) {
152 // Get the offset before we add the chunk to the file system
153 $preAppendOffset = $this->getOffset();
155 if ( $preAppendOffset +
$chunkSize > $this->getMaxUploadSize()) {
156 $status = Status
::newFatal( 'file-too-large' );
158 // Make sure the client is uploading the correct chunk with a matching offset.
159 if ( $preAppendOffset == $offset ) {
160 // Update local chunk index for the current chunk
161 $this->mChunkIndex++
;
162 $status = $this->outputChunk( $chunkPath );
163 if( $status->isGood() ){
164 // Update local offset:
165 $this->mOffset
= $preAppendOffset +
$chunkSize;
166 // Update chunk table status db
167 $this->updateChunkStatus();
170 $status = Status
::newFatal( 'invalid-chunk-offset' );
177 * Update the chunk db table with the current status:
179 private function updateChunkStatus(){
180 wfDebug( __METHOD__
. " update chunk status for {$this->mFileKey} offset:" .
181 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
183 $dbw = $this->repo
->getMasterDb();
187 'us_status' => 'chunks',
188 'us_chunk_inx' => $this->getChunkIndex(),
189 'us_size' => $this->getOffset()
191 array( 'us_key' => $this->mFileKey
),
196 * Get the chunk db state and populate update relevant local values
198 private function getChunkStatus(){
199 // get Master db to avoid race conditions.
200 // Otherwise, if chunk upload time < replag there will be spurious errors
201 $dbw = $this->repo
->getMasterDb();
202 $row = $dbw->selectRow(
209 array( 'us_key' => $this->mFileKey
),
214 $this->mChunkIndex
= $row->us_chunk_inx
;
215 $this->mOffset
= $row->us_size
;
216 $this->mVirtualTempPath
= $row->us_path
;
220 * Get the current Chunk index
221 * @return Integer index of the current chunk
223 private function getChunkIndex(){
224 if( $this->mChunkIndex
!== null ){
225 return $this->mChunkIndex
;
231 * Gets the current offset in fromt the stashedupload table
232 * @return Integer current byte offset of the chunk file set
234 private function getOffset(){
235 if ( $this->mOffset
!== null ){
236 return $this->mOffset
;
242 * Output the chunk to disk
244 * @param $chunkPath string
245 * @return FileRepoStatus
247 private function outputChunk( $chunkPath ){
248 // Key is fileKey + chunk index
249 $fileKey = $this->getChunkFileKey();
251 // Store the chunk per its indexed fileKey:
252 $hashPath = $this->repo
->getHashPath( $fileKey );
253 $storeStatus = $this->repo
->store( $chunkPath, 'temp', "$hashPath$fileKey" );
255 // Check for error in stashing the chunk:
256 if ( ! $storeStatus->isOK() ) {
257 $error = $storeStatus->getErrorsArray();
258 $error = reset( $error );
259 if ( ! count( $error ) ) {
260 $error = $storeStatus->getWarningsArray();
261 $error = reset( $error );
262 if ( ! count( $error ) ) {
263 $error = array( 'unknown', 'no error recorded' );
266 throw new UploadChunkFileException( "error storing file in '$chunkPath': " . implode( '; ', $error ) );
270 private function getChunkFileKey( $index = null ){
271 if( $index === null ){
272 $index = $this->getChunkIndex();
274 return $this->mFileKey
. '.' . $index ;
278 class UploadChunkZeroLengthFileException
extends MWException
{};
279 class UploadChunkFileException
extends MWException
{};