StreamWrapper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Lib\Storage;
  29. abstract class StreamWrapper extends \OC\Files\Storage\Common {
  30. /**
  31. * @param string $path
  32. * @return string|null
  33. */
  34. abstract public function constructUrl($path);
  35. public function mkdir($path) {
  36. return mkdir($this->constructUrl($path));
  37. }
  38. public function rmdir($path) {
  39. if ($this->is_dir($path) && $this->isDeletable($path)) {
  40. $dh = $this->opendir($path);
  41. if (!is_resource($dh)) {
  42. return false;
  43. }
  44. while (($file = readdir($dh)) !== false) {
  45. if ($this->is_dir($path . '/' . $file)) {
  46. $this->rmdir($path . '/' . $file);
  47. } else {
  48. $this->unlink($path . '/' . $file);
  49. }
  50. }
  51. $url = $this->constructUrl($path);
  52. $success = rmdir($url);
  53. clearstatcache(false, $url);
  54. return $success;
  55. } else {
  56. return false;
  57. }
  58. }
  59. public function opendir($path) {
  60. return opendir($this->constructUrl($path));
  61. }
  62. public function filetype($path) {
  63. return @filetype($this->constructUrl($path));
  64. }
  65. public function file_exists($path) {
  66. return file_exists($this->constructUrl($path));
  67. }
  68. public function unlink($path) {
  69. $url = $this->constructUrl($path);
  70. $success = unlink($url);
  71. // normally unlink() is supposed to do this implicitly,
  72. // but doing it anyway just to be sure
  73. clearstatcache(false, $url);
  74. return $success;
  75. }
  76. public function fopen($path, $mode) {
  77. return fopen($this->constructUrl($path), $mode);
  78. }
  79. public function touch($path, $mtime = null) {
  80. if ($this->file_exists($path)) {
  81. if (is_null($mtime)) {
  82. $fh = $this->fopen($path, 'a');
  83. fwrite($fh, '');
  84. fclose($fh);
  85. return true;
  86. } else {
  87. return false; //not supported
  88. }
  89. } else {
  90. $this->file_put_contents($path, '');
  91. return true;
  92. }
  93. }
  94. /**
  95. * @param string $path
  96. * @param string $target
  97. */
  98. public function getFile($path, $target) {
  99. return copy($this->constructUrl($path), $target);
  100. }
  101. /**
  102. * @param string $target
  103. */
  104. public function uploadFile($path, $target) {
  105. return copy($path, $this->constructUrl($target));
  106. }
  107. public function rename($path1, $path2) {
  108. return rename($this->constructUrl($path1), $this->constructUrl($path2));
  109. }
  110. public function stat($path) {
  111. return stat($this->constructUrl($path));
  112. }
  113. }