LocalTempFileTrait.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Files\Storage;
  25. /**
  26. * Storage backend class for providing common filesystem operation methods
  27. * which are not storage-backend specific.
  28. *
  29. * \OC\Files\Storage\Common is never used directly; it is extended by all other
  30. * storage backends, where its methods may be overridden, and additional
  31. * (backend-specific) methods are defined.
  32. *
  33. * Some \OC\Files\Storage\Common methods call functions which are first defined
  34. * in classes which extend it, e.g. $this->stat() .
  35. */
  36. trait LocalTempFileTrait {
  37. /** @var array<string,string|false> */
  38. protected array $cachedFiles = [];
  39. protected function getCachedFile(string $path): string|false {
  40. if (!isset($this->cachedFiles[$path])) {
  41. $this->cachedFiles[$path] = $this->toTmpFile($path);
  42. }
  43. return $this->cachedFiles[$path];
  44. }
  45. /**
  46. * @param string $path
  47. */
  48. protected function removeCachedFile($path) {
  49. unset($this->cachedFiles[$path]);
  50. }
  51. protected function toTmpFile(string $path): string|false { //no longer in the storage api, still useful here
  52. $source = $this->fopen($path, 'r');
  53. if (!$source) {
  54. return false;
  55. }
  56. if ($pos = strrpos($path, '.')) {
  57. $extension = substr($path, $pos);
  58. } else {
  59. $extension = '';
  60. }
  61. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
  62. $target = fopen($tmpFile, 'w');
  63. \OC_Helper::streamCopy($source, $target);
  64. fclose($target);
  65. return $tmpFile;
  66. }
  67. }