Conditions | 56 |
Paths | > 20000 |
Total Lines | 266 |
Code Lines | 149 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
134 | public function put($data) { |
||
135 | try { |
||
136 | $exists = $this->fileView->file_exists($this->path); |
||
137 | if ($this->info && $exists && !$this->info->isUpdateable()) { |
||
138 | throw new Forbidden(); |
||
139 | } |
||
140 | } catch (StorageNotAvailableException $e) { |
||
141 | throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()])); |
||
142 | } |
||
143 | |||
144 | // verify path of the target |
||
145 | $this->verifyPath(); |
||
146 | |||
147 | // chunked handling |
||
148 | if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
||
149 | try { |
||
150 | return $this->createFileChunked($data); |
||
151 | } catch (\Exception $e) { |
||
152 | $this->convertToSabreException($e); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** @var Storage $partStorage */ |
||
157 | [$partStorage] = $this->fileView->resolvePath($this->path); |
||
158 | $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
||
159 | |||
160 | $view = \OC\Files\Filesystem::getView(); |
||
161 | |||
162 | if ($needsPartFile) { |
||
163 | // mark file as partial while uploading (ignored by the scanner) |
||
164 | $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
||
165 | |||
166 | if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) { |
||
167 | $needsPartFile = false; |
||
168 | } |
||
169 | } |
||
170 | if (!$needsPartFile) { |
||
171 | // upload file directly as the final path |
||
172 | $partFilePath = $this->path; |
||
173 | |||
174 | if ($view && !$this->emitPreHooks($exists)) { |
||
175 | throw new Exception($this->l10n->t('Could not write to final file, canceled by hook')); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
||
180 | /** @var \OC\Files\Storage\Storage $partStorage */ |
||
181 | [$partStorage, $internalPartPath] = $this->fileView->resolvePath($partFilePath); |
||
|
|||
182 | /** @var \OC\Files\Storage\Storage $storage */ |
||
183 | [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
||
184 | try { |
||
185 | if (!$needsPartFile) { |
||
186 | try { |
||
187 | $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
188 | } catch (LockedException $e) { |
||
189 | // during very large uploads, the shared lock we got at the start might have been expired |
||
190 | // meaning that the above lock can fail not just only because somebody else got a shared lock |
||
191 | // or because there is no existing shared lock to make exclusive |
||
192 | // |
||
193 | // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
||
194 | // lock this will still fail, if our original shared lock expired the new lock will be successful and |
||
195 | // the entire operation will be safe |
||
196 | |||
197 | try { |
||
198 | $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
199 | } catch (LockedException $ex) { |
||
200 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | if (!is_resource($data)) { |
||
206 | $tmpData = fopen('php://temp', 'r+'); |
||
207 | if ($data !== null) { |
||
208 | fwrite($tmpData, $data); |
||
209 | rewind($tmpData); |
||
210 | } |
||
211 | $data = $tmpData; |
||
212 | } |
||
213 | |||
214 | $data = HashWrapper::wrap($data, 'md5', function ($hash) { |
||
215 | $this->header('X-Hash-MD5: ' . $hash); |
||
216 | }); |
||
217 | $data = HashWrapper::wrap($data, 'sha1', function ($hash) { |
||
218 | $this->header('X-Hash-SHA1: ' . $hash); |
||
219 | }); |
||
220 | $data = HashWrapper::wrap($data, 'sha256', function ($hash) { |
||
221 | $this->header('X-Hash-SHA256: ' . $hash); |
||
222 | }); |
||
223 | |||
224 | if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { |
||
225 | $isEOF = false; |
||
226 | $wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) { |
||
227 | $isEOF = feof($stream); |
||
228 | }); |
||
229 | |||
230 | $result = true; |
||
231 | $count = -1; |
||
232 | try { |
||
233 | $count = $partStorage->writeStream($internalPartPath, $wrappedData); |
||
234 | } catch (GenericFileException $e) { |
||
235 | $result = false; |
||
236 | } catch (BadGateway $e) { |
||
237 | throw $e; |
||
238 | } |
||
239 | |||
240 | |||
241 | if ($result === false) { |
||
242 | $result = $isEOF; |
||
243 | if (is_resource($wrappedData)) { |
||
244 | $result = feof($wrappedData); |
||
245 | } |
||
246 | } |
||
247 | } else { |
||
248 | $target = $partStorage->fopen($internalPartPath, 'wb'); |
||
249 | if ($target === false) { |
||
250 | \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
||
251 | // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
||
252 | throw new Exception($this->l10n->t('Could not write file contents')); |
||
253 | } |
||
254 | [$count, $result] = \OC_Helper::streamCopy($data, $target); |
||
255 | fclose($target); |
||
256 | } |
||
257 | |||
258 | if ($result === false) { |
||
259 | $expected = -1; |
||
260 | if (isset($_SERVER['CONTENT_LENGTH'])) { |
||
261 | $expected = $_SERVER['CONTENT_LENGTH']; |
||
262 | } |
||
263 | if ($expected !== "0") { |
||
264 | throw new Exception( |
||
265 | $this->l10n->t( |
||
266 | 'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)', |
||
267 | [ |
||
268 | $this->l10n->n('%n byte', '%n bytes', $count), |
||
269 | $this->l10n->n('%n byte', '%n bytes', $expected), |
||
270 | ], |
||
271 | ) |
||
272 | ); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | // if content length is sent by client: |
||
277 | // double check if the file was fully received |
||
278 | // compare expected and actual size |
||
279 | if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
||
280 | $expected = (int)$_SERVER['CONTENT_LENGTH']; |
||
281 | if ($count !== $expected) { |
||
282 | throw new BadRequest( |
||
283 | $this->l10n->t( |
||
284 | 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
||
285 | [ |
||
286 | $this->l10n->n('%n byte', '%n bytes', $expected), |
||
287 | $this->l10n->n('%n byte', '%n bytes', $count), |
||
288 | ], |
||
289 | ) |
||
290 | ); |
||
291 | } |
||
292 | } |
||
293 | } catch (\Exception $e) { |
||
294 | $context = []; |
||
295 | |||
296 | if ($e instanceof LockedException) { |
||
297 | $context['level'] = ILogger::DEBUG; |
||
298 | } |
||
299 | |||
300 | \OC::$server->getLogger()->logException($e, $context); |
||
301 | if ($needsPartFile) { |
||
302 | $partStorage->unlink($internalPartPath); |
||
303 | } |
||
304 | $this->convertToSabreException($e); |
||
305 | } |
||
306 | |||
307 | try { |
||
308 | if ($needsPartFile) { |
||
309 | if ($view && !$this->emitPreHooks($exists)) { |
||
310 | $partStorage->unlink($internalPartPath); |
||
311 | throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook')); |
||
312 | } |
||
313 | try { |
||
314 | $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
315 | } catch (LockedException $e) { |
||
316 | // during very large uploads, the shared lock we got at the start might have been expired |
||
317 | // meaning that the above lock can fail not just only because somebody else got a shared lock |
||
318 | // or because there is no existing shared lock to make exclusive |
||
319 | // |
||
320 | // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
||
321 | // lock this will still fail, if our original shared lock expired the new lock will be successful and |
||
322 | // the entire operation will be safe |
||
323 | |||
324 | try { |
||
325 | $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
326 | } catch (LockedException $ex) { |
||
327 | if ($needsPartFile) { |
||
328 | $partStorage->unlink($internalPartPath); |
||
329 | } |
||
330 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
331 | } |
||
332 | } |
||
333 | |||
334 | // rename to correct path |
||
335 | try { |
||
336 | $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
||
337 | $fileExists = $storage->file_exists($internalPath); |
||
338 | if ($renameOkay === false || $fileExists === false) { |
||
339 | \OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']); |
||
340 | throw new Exception($this->l10n->t('Could not rename part file to final file')); |
||
341 | } |
||
342 | } catch (ForbiddenException $ex) { |
||
343 | if (!$ex->getRetry()) { |
||
344 | $partStorage->unlink($internalPartPath); |
||
345 | } |
||
346 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
||
347 | } catch (\Exception $e) { |
||
348 | $partStorage->unlink($internalPartPath); |
||
349 | $this->convertToSabreException($e); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | // since we skipped the view we need to scan and emit the hooks ourselves |
||
354 | $storage->getUpdater()->update($internalPath); |
||
355 | |||
356 | try { |
||
357 | $this->changeLock(ILockingProvider::LOCK_SHARED); |
||
358 | } catch (LockedException $e) { |
||
359 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
360 | } |
||
361 | |||
362 | // allow sync clients to send the mtime along in a header |
||
363 | if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
||
364 | $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
||
365 | if ($this->fileView->touch($this->path, $mtime)) { |
||
366 | $this->header('X-OC-MTime: accepted'); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | $fileInfoUpdate = [ |
||
371 | 'upload_time' => time() |
||
372 | ]; |
||
373 | |||
374 | // allow sync clients to send the creation time along in a header |
||
375 | if (isset($this->request->server['HTTP_X_OC_CTIME'])) { |
||
376 | $ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']); |
||
377 | $fileInfoUpdate['creation_time'] = $ctime; |
||
378 | $this->header('X-OC-CTime: accepted'); |
||
379 | } |
||
380 | |||
381 | $this->fileView->putFileInfo($this->path, $fileInfoUpdate); |
||
382 | |||
383 | if ($view) { |
||
384 | $this->emitPostHooks($exists); |
||
385 | } |
||
386 | |||
387 | $this->refreshInfo(); |
||
388 | |||
389 | if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
||
390 | $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
||
391 | $this->setChecksum($checksum); |
||
392 | } elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
||
393 | $this->setChecksum(''); |
||
394 | } |
||
395 | } catch (StorageNotAvailableException $e) { |
||
396 | throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e); |
||
397 | } |
||
398 | |||
399 | return '"' . $this->info->getEtag() . '"'; |
||
400 | } |
||
757 |