1
0

LocalTempFileTrait.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 string[] */
  38. protected $cachedFiles = [];
  39. /**
  40. * @param string $path
  41. * @return string
  42. */
  43. protected function getCachedFile($path) {
  44. if (!isset($this->cachedFiles[$path])) {
  45. $this->cachedFiles[$path] = $this->toTmpFile($path);
  46. }
  47. return $this->cachedFiles[$path];
  48. }
  49. /**
  50. * @param string $path
  51. */
  52. protected function removeCachedFile($path) {
  53. unset($this->cachedFiles[$path]);
  54. }
  55. /**
  56. * @param string $path
  57. * @return string
  58. */
  59. protected function toTmpFile($path) { //no longer in the storage api, still useful here
  60. $source = $this->fopen($path, 'r');
  61. if (!$source) {
  62. return false;
  63. }
  64. if ($pos = strrpos($path, '.')) {
  65. $extension = substr($path, $pos);
  66. } else {
  67. $extension = '';
  68. }
  69. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
  70. $target = fopen($tmpFile, 'w');
  71. \OC_Helper::streamCopy($source, $target);
  72. fclose($target);
  73. return $tmpFile;
  74. }
  75. }