b0916191d4cce99f2bf70c4c7fbaad40a13cdbd5
[lhc/web/wiklou.git] / includes / upload / UploadFromChunks.php
1 <?php
2 /**
3 * Implements uploading from chunks
4 *
5 * @file
6 * @ingroup upload
7 * @author Michael Dale
8 */
9
10 class UploadFromChunks extends UploadFromFile {
11 protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath;
12
13 /**
14 * Setup local pointers to stash, repo and user ( similar to UploadFromStash )
15 *
16 * @param $user User
17 * @param $stash UploadStash
18 * @param $repo FileRepo
19 */
20 public function __construct( $user = false, $stash = false, $repo = false ) {
21 // user object. sometimes this won't exist, as when running from cron.
22 $this->user = $user;
23
24 if( $repo ) {
25 $this->repo = $repo;
26 } else {
27 $this->repo = RepoGroup::singleton()->getLocalRepo();
28 }
29
30 if( $stash ) {
31 $this->stash = $stash;
32 } else {
33 if( $user ) {
34 wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
35 } else {
36 wfDebug( __METHOD__ . " creating new UploadFromChunks instance with no user\n" );
37 }
38 $this->stash = new UploadStash( $this->repo, $this->user );
39 }
40
41 return true;
42 }
43 /**
44 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
45 *
46 * @return UploadStashFile stashed file
47 */
48 public function stashFile() {
49 // Stash file is the called on creating a new chunk session:
50 $this->mChunkIndex = 0;
51 $this->mOffset = 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();
57
58 // Output a copy of this first to chunk 0 location:
59 $status = $this->outputChunk( $this->mLocalFile->getPath() );
60
61 // Update db table to reflect initial "chunk" state
62 $this->updateChunkStatus();
63 return $this->mLocalFile;
64 }
65
66 /**
67 * Continue chunk uploading
68 */
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();
74
75 $metadata = $this->stash->getMetadata( $key );
76 $this->initializePathInfo( $name,
77 $this->getRealPath ( $metadata['us_path'] ),
78 $metadata['us_size'],
79 false
80 );
81 }
82
83 /**
84 * Append the final chunk and ready file for parent::performUpload()
85 * @return void
86 */
87 public function concatenateChunks() {
88 wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" .
89 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
90
91 // Concatenate all the chunks to mVirtualTempPath
92 $fileList = Array();
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 );
96 }
97
98 // Concatinate into the mVirtualTempPath location;
99 $status = $this->repo->concatenate( $fileList, $this->mVirtualTempPath, FileRepo::DELETE_SOURCE );
100 if( !$status->isOk() ){
101 return $status;
102 }
103 // Update the mTempPath variable ( for FileUpload or normal Stash to take over )
104 $this->mTempPath = $this->getRealPath( $this->mVirtualTempPath );
105 return $status;
106 }
107 /**
108 * Returns the virtual chunk location:
109 * @param unknown_type $index
110 */
111 function getVirtualChunkLocation( $index ){
112 return $this->repo->getVirtualUrl( 'temp' ) .
113 '/' .
114 $this->repo->getHashPath(
115 $this->getChunkFileKey( $index )
116 ) .
117 $this->getChunkFileKey( $index );
118 }
119 /**
120 * Add a chunk to the temporary directory
121 *
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 )
125 * @return Status
126 */
127 public function addChunk( $chunkPath, $chunkSize, $offset ) {
128 // Get the offset before we add the chunk to the file system
129 $preAppendOffset = $this->getOffset();
130
131 if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize()) {
132 $status = Status::newFatal( 'file-too-large' );
133 } else {
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();
144 }
145 } else {
146 $status = Status::newFatal( 'invalid-chunk-offset' );
147 }
148 }
149 return $status;
150 }
151
152 /**
153 * Update the chunk db table with the current status:
154 */
155 private function updateChunkStatus(){
156 wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
157 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
158
159 $dbw = $this->repo->getMasterDb();
160 $dbw->update(
161 'uploadstash',
162 array(
163 'us_status' => 'chunks',
164 'us_chunk_inx' => $this->getChunkIndex(),
165 'us_size' => $this->getOffset()
166 ),
167 array( 'us_key' => $this->mFileKey ),
168 __METHOD__
169 );
170 }
171 /**
172 * Get the chunk db state and populate update relevant local values
173 */
174 private function getChunkStatus(){
175 $dbr = $this->repo->getSlaveDb();
176 $row = $dbr->selectRow(
177 'uploadstash',
178 array(
179 'us_chunk_inx',
180 'us_size',
181 'us_path',
182 ),
183 array( 'us_key' => $this->mFileKey ),
184 __METHOD__
185 );
186 // Handle result:
187 if ( $row ) {
188 $this->mChunkIndex = $row->us_chunk_inx;
189 $this->mOffset = $row->us_size;
190 $this->mVirtualTempPath = $row->us_path;
191 }
192 }
193 /**
194 * Get the current Chunk index
195 * @return Integer index of the current chunk
196 */
197 private function getChunkIndex(){
198 if( $this->mChunkIndex !== null ){
199 return $this->mChunkIndex;
200 }
201 return 0;
202 }
203
204 /**
205 * Gets the current offset in fromt the stashedupload table
206 * @return Integer current byte offset of the chunk file set
207 */
208 private function getOffset(){
209 if ( $this->mOffset !== null ){
210 return $this->mOffset;
211 }
212 return 0;
213 }
214
215 /**
216 * Output the chunk to disk
217 *
218 * @param $chunk
219 * @param unknown_type $path
220 */
221 private function outputChunk( $chunkPath ){
222 // Key is fileKey + chunk index
223 $fileKey = $this->getChunkFileKey();
224
225 // Store the chunk per its indexed fileKey:
226 $hashPath = $this->repo->getHashPath( $fileKey );
227 $storeStatus = $this->repo->store( $chunkPath, 'temp', "$hashPath$fileKey" );
228
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' );
238 }
239 }
240 throw new UploadChunkFileException( "error storing file in '$path': " . implode( '; ', $error ) );
241 }
242 return $storeStatus;
243 }
244 private function getChunkFileKey( $index = null ){
245 if( $index === null ){
246 $index = $this->getChunkIndex();
247 }
248 return $this->mFileKey . '.' . $index ;
249 }
250 }
251
252 class UploadChunkZeroLengthFileException extends MWException {};
253 class UploadChunkFileException extends MWException {};