1
0

LocalTempFileTrait.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. protected function removeCachedFile(string $path): void {
  29. unset($this->cachedFiles[$path]);
  30. }
  31. protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here
  32. $source = $this->fopen($path, 'r');
  33. if (!$source) {
  34. return false;
  35. }
  36. if ($pos = strrpos($path, '.')) {
  37. $extension = substr($path, $pos);
  38. } else {
  39. $extension = '';
  40. }
  41. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
  42. $target = fopen($tmpFile, 'w');
  43. \OC_Helper::streamCopy($source, $target);
  44. fclose($target);
  45. return $tmpFile;
  46. }
  47. }