LocalTempFileTrait.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage;
  8. /**
  9. * Storage backend class for providing common filesystem operation methods
  10. * which are not storage-backend specific.
  11. *
  12. * \OC\Files\Storage\Common is never used directly; it is extended by all other
  13. * storage backends, where its methods may be overridden, and additional
  14. * (backend-specific) methods are defined.
  15. *
  16. * Some \OC\Files\Storage\Common methods call functions which are first defined
  17. * in classes which extend it, e.g. $this->stat() .
  18. */
  19. trait LocalTempFileTrait {
  20. /** @var array<string,string|false> */
  21. protected array $cachedFiles = [];
  22. protected function getCachedFile(string $path): string|false {
  23. if (!isset($this->cachedFiles[$path])) {
  24. $this->cachedFiles[$path] = $this->toTmpFile($path);
  25. }
  26. return $this->cachedFiles[$path];
  27. }
  28. /**
  29. * @param string $path
  30. */
  31. protected function removeCachedFile($path) {
  32. unset($this->cachedFiles[$path]);
  33. }
  34. protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here
  35. $source = $this->fopen($path, 'r');
  36. if (!$source) {
  37. return false;
  38. }
  39. if ($pos = strrpos($path, '.')) {
  40. $extension = substr($path, $pos);
  41. } else {
  42. $extension = '';
  43. }
  44. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
  45. $target = fopen($tmpFile, 'w');
  46. \OC_Helper::streamCopy($source, $target);
  47. fclose($target);
  48. return $tmpFile;
  49. }
  50. }