StreamWrapper.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Storage;
  8. abstract class StreamWrapper extends \OC\Files\Storage\Common {
  9. /**
  10. * @param string $path
  11. * @return string|null
  12. */
  13. abstract public function constructUrl($path);
  14. public function mkdir($path) {
  15. return mkdir($this->constructUrl($path));
  16. }
  17. public function rmdir($path) {
  18. if ($this->is_dir($path) && $this->isDeletable($path)) {
  19. $dh = $this->opendir($path);
  20. if (!is_resource($dh)) {
  21. return false;
  22. }
  23. while (($file = readdir($dh)) !== false) {
  24. if ($this->is_dir($path . '/' . $file)) {
  25. $this->rmdir($path . '/' . $file);
  26. } else {
  27. $this->unlink($path . '/' . $file);
  28. }
  29. }
  30. $url = $this->constructUrl($path);
  31. $success = rmdir($url);
  32. clearstatcache(false, $url);
  33. return $success;
  34. } else {
  35. return false;
  36. }
  37. }
  38. public function opendir($path) {
  39. return opendir($this->constructUrl($path));
  40. }
  41. public function filetype($path) {
  42. return @filetype($this->constructUrl($path));
  43. }
  44. public function file_exists($path) {
  45. return file_exists($this->constructUrl($path));
  46. }
  47. public function unlink($path) {
  48. $url = $this->constructUrl($path);
  49. $success = unlink($url);
  50. // normally unlink() is supposed to do this implicitly,
  51. // but doing it anyway just to be sure
  52. clearstatcache(false, $url);
  53. return $success;
  54. }
  55. public function fopen($path, $mode) {
  56. return fopen($this->constructUrl($path), $mode);
  57. }
  58. public function touch($path, $mtime = null) {
  59. if ($this->file_exists($path)) {
  60. if (is_null($mtime)) {
  61. $fh = $this->fopen($path, 'a');
  62. fwrite($fh, '');
  63. fclose($fh);
  64. return true;
  65. } else {
  66. return false; //not supported
  67. }
  68. } else {
  69. $this->file_put_contents($path, '');
  70. return true;
  71. }
  72. }
  73. /**
  74. * @param string $path
  75. * @param string $target
  76. */
  77. public function getFile($path, $target) {
  78. return copy($this->constructUrl($path), $target);
  79. }
  80. /**
  81. * @param string $target
  82. */
  83. public function uploadFile($path, $target) {
  84. return copy($path, $this->constructUrl($target));
  85. }
  86. public function rename($source, $target) {
  87. return rename($this->constructUrl($source), $this->constructUrl($target));
  88. }
  89. public function stat($path) {
  90. return stat($this->constructUrl($path));
  91. }
  92. }