StreamWrapper.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. use OC\Files\Storage\Common;
  9. abstract class StreamWrapper extends Common {
  10. abstract public function constructUrl(string $path): ?string;
  11. public function mkdir(string $path): bool {
  12. return mkdir($this->constructUrl($path));
  13. }
  14. public function rmdir(string $path): bool {
  15. if ($this->is_dir($path) && $this->isDeletable($path)) {
  16. $dh = $this->opendir($path);
  17. if (!is_resource($dh)) {
  18. return false;
  19. }
  20. while (($file = readdir($dh)) !== false) {
  21. if ($this->is_dir($path . '/' . $file)) {
  22. $this->rmdir($path . '/' . $file);
  23. } else {
  24. $this->unlink($path . '/' . $file);
  25. }
  26. }
  27. $url = $this->constructUrl($path);
  28. $success = rmdir($url);
  29. clearstatcache(false, $url);
  30. return $success;
  31. } else {
  32. return false;
  33. }
  34. }
  35. public function opendir(string $path) {
  36. return opendir($this->constructUrl($path));
  37. }
  38. public function filetype(string $path): string|false {
  39. return @filetype($this->constructUrl($path));
  40. }
  41. public function file_exists(string $path): bool {
  42. return file_exists($this->constructUrl($path));
  43. }
  44. public function unlink(string $path): bool {
  45. $url = $this->constructUrl($path);
  46. $success = unlink($url);
  47. // normally unlink() is supposed to do this implicitly,
  48. // but doing it anyway just to be sure
  49. clearstatcache(false, $url);
  50. return $success;
  51. }
  52. public function fopen(string $path, string $mode) {
  53. return fopen($this->constructUrl($path), $mode);
  54. }
  55. public function touch(string $path, ?int $mtime = null): bool {
  56. if ($this->file_exists($path)) {
  57. if (is_null($mtime)) {
  58. $fh = $this->fopen($path, 'a');
  59. fwrite($fh, '');
  60. fclose($fh);
  61. return true;
  62. } else {
  63. return false; //not supported
  64. }
  65. } else {
  66. $this->file_put_contents($path, '');
  67. return true;
  68. }
  69. }
  70. public function getFile(string $path, string $target): bool {
  71. return copy($this->constructUrl($path), $target);
  72. }
  73. public function uploadFile(string $path, string $target): bool {
  74. return copy($path, $this->constructUrl($target));
  75. }
  76. public function rename(string $source, string $target): bool {
  77. return rename($this->constructUrl($source), $this->constructUrl($target));
  78. }
  79. public function stat(string $path): array|false {
  80. return stat($this->constructUrl($path));
  81. }
  82. }