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 // Concatinate into the mVirtualTempPath location;
99 $status = $this->repo
->concatenate( $fileList, $this->mVirtualTempPath
, FileRepo
::DELETE_SOURCE
);
100 if( !$status->isOk() ){
103 // Update the mTempPath variable ( for FileUpload or normal Stash to take over )
104 $this->mTempPath
= $this->getRealPath( $this->mVirtualTempPath
);
108 * Returns the virtual chunk location:
109 * @param unknown_type $index
111 function getVirtualChunkLocation( $index ){
112 return $this->repo
->getVirtualUrl( 'temp' ) .
114 $this->repo
->getHashPath(
115 $this->getChunkFileKey( $index )
117 $this->getChunkFileKey( $index );
120 * Add a chunk to the temporary directory
122 * @param $chunkPath path to temporary chunk file
123 * @param $chunkSize size of the current chunk
124 * @param $offset offset of current chunk ( mutch match database chunk offset )
127 public function addChunk( $chunkPath, $chunkSize, $offset ) {
128 // Get the offset before we add the chunk to the file system
129 $preAppendOffset = $this->getOffset();
131 if ( $preAppendOffset +
$chunkSize > $this->getMaxUploadSize()) {
132 $status = Status
::newFatal( 'file-too-large' );
134 // Make sure the client is uploading the correct chunk with a matching offset.
135 if ( $preAppendOffset == $offset ) {
136 // Update local chunk index for the current chunk
137 $this->mChunkIndex++
;
138 $status = $this->outputChunk( $chunkPath );
139 if( $status->isGood() ){
140 // Update local offset:
141 $this->mOffset
= $preAppendOffset +
$chunkSize;
142 // Update chunk table status db
143 $this->updateChunkStatus();
146 $status = Status
::newFatal( 'invalid-chunk-offset' );
153 * Update the chunk db table with the current status:
155 private function updateChunkStatus(){
156 wfDebug( __METHOD__
. " update chunk status for {$this->mFileKey} offset:" .
157 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
159 $dbw = $this->repo
->getMasterDb();
163 'us_status' => 'chunks',
164 'us_chunk_inx' => $this->getChunkIndex(),
165 'us_size' => $this->getOffset()
167 array( 'us_key' => $this->mFileKey
),
172 * Get the chunk db state and populate update relevant local values
174 private function getChunkStatus(){
175 $dbr = $this->repo
->getSlaveDb();
176 $row = $dbr->selectRow(
183 array( 'us_key' => $this->mFileKey
),
188 $this->mChunkIndex
= $row->us_chunk_inx
;
189 $this->mOffset
= $row->us_size
;
190 $this->mVirtualTempPath
= $row->us_path
;
194 * Get the current Chunk index
195 * @return Integer index of the current chunk
197 private function getChunkIndex(){
198 if( $this->mChunkIndex
!== null ){
199 return $this->mChunkIndex
;
205 * Gets the current offset in fromt the stashedupload table
206 * @return Integer current byte offset of the chunk file set
208 private function getOffset(){
209 if ( $this->mOffset
!== null ){
210 return $this->mOffset
;
216 * Output the chunk to disk
219 * @param unknown_type $path
221 private function outputChunk( $chunkPath ){
222 // Key is fileKey + chunk index
223 $fileKey = $this->getChunkFileKey();
225 // Store the chunk per its indexed fileKey:
226 $hashPath = $this->repo
->getHashPath( $fileKey );
227 $storeStatus = $this->repo
->store( $chunkPath, 'temp', "$hashPath$fileKey" );
229 // Check for error in stashing the chunk:
230 if ( ! $storeStatus->isOK() ) {
231 $error = $storeStatus->getErrorsArray();
232 $error = reset( $error );
233 if ( ! count( $error ) ) {
234 $error = $storeStatus->getWarningsArray();
235 $error = reset( $error );
236 if ( ! count( $error ) ) {
237 $error = array( 'unknown', 'no error recorded' );
240 throw new UploadChunkFileException( "error storing file in '$path': " . implode( '; ', $error ) );
244 private function getChunkFileKey( $index = null ){
245 if( $index === null ){
246 $index = $this->getChunkIndex();
248 return $this->mFileKey
. '.' . $index ;
252 class UploadChunkZeroLengthFileException
extends MWException
{};
253 class UploadChunkFileException
extends MWException
{};