In FileBackendBase/FileBackend:
[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 // 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 );
102 $tmpPath = $tmpFile
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() ){
108 return $status;
109 }
110 // Update the mTempPath variable ( for FileUpload or normal Stash to take over )
111 $this->mTempPath = $tmpPath; // file system path
112 return $status;
113 }
114
115 /**
116 * Perform the upload, then remove the temp copy afterward
117 * @param $comment string
118 * @param $pageText string
119 * @param $watch bool
120 * @param $user User
121 * @return Status
122 */
123 public function performUpload( $comment, $pageText, $watch, $user ) {
124 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
125 return $rv;
126 }
127
128 /**
129 * Returns the virtual chunk location:
130 * @param unknown_type $index
131 */
132 function getVirtualChunkLocation( $index ){
133 return $this->repo->getVirtualUrl( 'temp' ) .
134 '/' .
135 $this->repo->getHashPath(
136 $this->getChunkFileKey( $index )
137 ) .
138 $this->getChunkFileKey( $index );
139 }
140 /**
141 * Add a chunk to the temporary directory
142 *
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 )
146 * @return Status
147 */
148 public function addChunk( $chunkPath, $chunkSize, $offset ) {
149 // Get the offset before we add the chunk to the file system
150 $preAppendOffset = $this->getOffset();
151
152 if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize()) {
153 $status = Status::newFatal( 'file-too-large' );
154 } else {
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();
165 }
166 } else {
167 $status = Status::newFatal( 'invalid-chunk-offset' );
168 }
169 }
170 return $status;
171 }
172
173 /**
174 * Update the chunk db table with the current status:
175 */
176 private function updateChunkStatus(){
177 wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
178 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
179
180 $dbw = $this->repo->getMasterDb();
181 $dbw->update(
182 'uploadstash',
183 array(
184 'us_status' => 'chunks',
185 'us_chunk_inx' => $this->getChunkIndex(),
186 'us_size' => $this->getOffset()
187 ),
188 array( 'us_key' => $this->mFileKey ),
189 __METHOD__
190 );
191 }
192 /**
193 * Get the chunk db state and populate update relevant local values
194 */
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(
200 'uploadstash',
201 array(
202 'us_chunk_inx',
203 'us_size',
204 'us_path',
205 ),
206 array( 'us_key' => $this->mFileKey ),
207 __METHOD__
208 );
209 // Handle result:
210 if ( $row ) {
211 $this->mChunkIndex = $row->us_chunk_inx;
212 $this->mOffset = $row->us_size;
213 $this->mVirtualTempPath = $row->us_path;
214 }
215 }
216 /**
217 * Get the current Chunk index
218 * @return Integer index of the current chunk
219 */
220 private function getChunkIndex(){
221 if( $this->mChunkIndex !== null ){
222 return $this->mChunkIndex;
223 }
224 return 0;
225 }
226
227 /**
228 * Gets the current offset in fromt the stashedupload table
229 * @return Integer current byte offset of the chunk file set
230 */
231 private function getOffset(){
232 if ( $this->mOffset !== null ){
233 return $this->mOffset;
234 }
235 return 0;
236 }
237
238 /**
239 * Output the chunk to disk
240 *
241 * @param $chunk
242 * @param unknown_type $path
243 */
244 private function outputChunk( $chunkPath ){
245 // Key is fileKey + chunk index
246 $fileKey = $this->getChunkFileKey();
247
248 // Store the chunk per its indexed fileKey:
249 $hashPath = $this->repo->getHashPath( $fileKey );
250 $storeStatus = $this->repo->store( $chunkPath, 'temp', "$hashPath$fileKey" );
251
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' );
261 }
262 }
263 throw new UploadChunkFileException( "error storing file in '$path': " . implode( '; ', $error ) );
264 }
265 return $storeStatus;
266 }
267 private function getChunkFileKey( $index = null ){
268 if( $index === null ){
269 $index = $this->getChunkIndex();
270 }
271 return $this->mFileKey . '.' . $index ;
272 }
273 }
274
275 class UploadChunkZeroLengthFileException extends MWException {};
276 class UploadChunkFileException extends MWException {};