3 * Implements uploading from chunks
10 class UploadFromChunks
extends UploadFromFile
{
11 protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath;
14 * Setup local pointers to stash, repo and user ( similar to UploadFromStash )
17 * @param $stash UploadStash
18 * @param $repo FileRepo
20 public function __construct( $user = false, $stash = false, $repo = false ) {
21 // user object. sometimes this won't exist, as when running from cron.
27 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
31 $this->stash
= $stash;
34 wfDebug( __METHOD__
. " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
36 wfDebug( __METHOD__
. " creating new UploadFromChunks instance with no user\n" );
38 $this->stash
= new UploadStash( $this->repo
, $this->user
);
44 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
46 * @return UploadStashFile stashed file
48 public function stashFile() {
49 // Stash file is the called on creating a new chunk session:
50 $this->mChunkIndex
= 0;
52 // Create a local stash target
53 $this->mLocalFile
= parent
::stashFile();
54 // Update the initial file offset ( based on file size )
55 $this->mOffset
= $this->mLocalFile
->getSize();
56 $this->mFileKey
= $this->mLocalFile
->getFileKey();
58 // Output a copy of this first to chunk 0 location:
59 $status = $this->outputChunk( $this->mLocalFile
->getPath() );
61 // Update db table to reflect initial "chunk" state
62 $this->updateChunkStatus();
63 return $this->mLocalFile
;
67 * Continue chunk uploading
69 public function continueChunks( $name, $key, $webRequestUpload ) {
70 $this->mFileKey
= $key;
71 $this->mUpload
= $webRequestUpload;
72 // Get the chunk status form the db:
73 $this->getChunkStatus();
75 $metadata = $this->stash
->getMetadata( $key );
76 $this->initializePathInfo( $name,
77 $this->getRealPath( $metadata['us_path'] ),
84 * Append the final chunk and ready file for parent::performUpload()
87 public function concatenateChunks() {
88 wfDebug( __METHOD__
. " concatenate {$this->mChunkIndex} chunks:" .
89 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
91 // Concatenate all the chunks to mVirtualTempPath
93 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
94 for( $i = 0; $i <= $this->getChunkIndex(); $i++
){
95 $fileList[] = $this->getVirtualChunkLocation( $i );
98 // Get the file extension from the last chunk
99 $ext = FileBackend
::extensionFromPath( $this->mVirtualTempPath
);
100 // Get a 0-byte temp file to perform the concatenation at
101 $tmpFile = TempFSFile
::factory( 'chunkedupload_', $ext );
103 ?
$tmpFile->getPath()
104 : false; // fail in concatenate()
105 // Concatenate the chunks at the temp file
106 $status = $this->repo
->concatenate( $fileList, $tmpPath, FileRepo
::DELETE_SOURCE
);
107 if( !$status->isOk() ){
110 // Update the mTempPath variable ( for FileUpload or normal Stash to take over )
111 $this->mTempPath
= $tmpPath; // file system path
116 * Perform the upload, then remove the temp copy afterward
117 * @param $comment string
118 * @param $pageText string
123 public function performUpload( $comment, $pageText, $watch, $user ) {
124 $rv = parent
::performUpload( $comment, $pageText, $watch, $user );
129 * Returns the virtual chunk location:
130 * @param unknown_type $index
132 function getVirtualChunkLocation( $index ){
133 return $this->repo
->getVirtualUrl( 'temp' ) .
135 $this->repo
->getHashPath(
136 $this->getChunkFileKey( $index )
138 $this->getChunkFileKey( $index );
141 * Add a chunk to the temporary directory
143 * @param $chunkPath path to temporary chunk file
144 * @param $chunkSize size of the current chunk
145 * @param $offset offset of current chunk ( mutch match database chunk offset )
148 public function addChunk( $chunkPath, $chunkSize, $offset ) {
149 // Get the offset before we add the chunk to the file system
150 $preAppendOffset = $this->getOffset();
152 if ( $preAppendOffset +
$chunkSize > $this->getMaxUploadSize()) {
153 $status = Status
::newFatal( 'file-too-large' );
155 // Make sure the client is uploading the correct chunk with a matching offset.
156 if ( $preAppendOffset == $offset ) {
157 // Update local chunk index for the current chunk
158 $this->mChunkIndex++
;
159 $status = $this->outputChunk( $chunkPath );
160 if( $status->isGood() ){
161 // Update local offset:
162 $this->mOffset
= $preAppendOffset +
$chunkSize;
163 // Update chunk table status db
164 $this->updateChunkStatus();
167 $status = Status
::newFatal( 'invalid-chunk-offset' );
174 * Update the chunk db table with the current status:
176 private function updateChunkStatus(){
177 wfDebug( __METHOD__
. " update chunk status for {$this->mFileKey} offset:" .
178 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
180 $dbw = $this->repo
->getMasterDb();
184 'us_status' => 'chunks',
185 'us_chunk_inx' => $this->getChunkIndex(),
186 'us_size' => $this->getOffset()
188 array( 'us_key' => $this->mFileKey
),
193 * Get the chunk db state and populate update relevant local values
195 private function getChunkStatus(){
196 // get Master db to avoid race conditions.
197 // Otherwise, if chunk upload time < replag there will be spurious errors
198 $dbw = $this->repo
->getMasterDb();
199 $row = $dbw->selectRow(
206 array( 'us_key' => $this->mFileKey
),
211 $this->mChunkIndex
= $row->us_chunk_inx
;
212 $this->mOffset
= $row->us_size
;
213 $this->mVirtualTempPath
= $row->us_path
;
217 * Get the current Chunk index
218 * @return Integer index of the current chunk
220 private function getChunkIndex(){
221 if( $this->mChunkIndex
!== null ){
222 return $this->mChunkIndex
;
228 * Gets the current offset in fromt the stashedupload table
229 * @return Integer current byte offset of the chunk file set
231 private function getOffset(){
232 if ( $this->mOffset
!== null ){
233 return $this->mOffset
;
239 * Output the chunk to disk
242 * @param unknown_type $path
244 private function outputChunk( $chunkPath ){
245 // Key is fileKey + chunk index
246 $fileKey = $this->getChunkFileKey();
248 // Store the chunk per its indexed fileKey:
249 $hashPath = $this->repo
->getHashPath( $fileKey );
250 $storeStatus = $this->repo
->store( $chunkPath, 'temp', "$hashPath$fileKey" );
252 // Check for error in stashing the chunk:
253 if ( ! $storeStatus->isOK() ) {
254 $error = $storeStatus->getErrorsArray();
255 $error = reset( $error );
256 if ( ! count( $error ) ) {
257 $error = $storeStatus->getWarningsArray();
258 $error = reset( $error );
259 if ( ! count( $error ) ) {
260 $error = array( 'unknown', 'no error recorded' );
263 throw new UploadChunkFileException( "error storing file in '$path': " . implode( '; ', $error ) );
267 private function getChunkFileKey( $index = null ){
268 if( $index === null ){
269 $index = $this->getChunkIndex();
271 return $this->mFileKey
. '.' . $index ;
275 class UploadChunkZeroLengthFileException
extends MWException
{};
276 class UploadChunkFileException
extends MWException
{};