Flysystem.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Storage;
  23. use Icewind\Streams\CallbackWrapper;
  24. use Icewind\Streams\IteratorDirectory;
  25. use League\Flysystem\AdapterInterface;
  26. use League\Flysystem\FileNotFoundException;
  27. use League\Flysystem\Filesystem;
  28. use League\Flysystem\Plugin\GetWithMetadata;
  29. /**
  30. * Generic adapter between flysystem adapters and owncloud's storage system
  31. *
  32. * To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice
  33. */
  34. abstract class Flysystem extends Common {
  35. /**
  36. * @var Filesystem
  37. */
  38. protected $flysystem;
  39. /**
  40. * @var string
  41. */
  42. protected $root = '';
  43. /**
  44. * Initialize the storage backend with a flyssytem adapter
  45. *
  46. * @param \League\Flysystem\AdapterInterface $adapter
  47. */
  48. protected function buildFlySystem(AdapterInterface $adapter) {
  49. $this->flysystem = new Filesystem($adapter);
  50. $this->flysystem->addPlugin(new GetWithMetadata());
  51. }
  52. protected function buildPath($path) {
  53. $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
  54. return ltrim($fullPath, '/');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function file_get_contents($path) {
  60. return $this->flysystem->read($this->buildPath($path));
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function file_put_contents($path, $data) {
  66. return $this->flysystem->put($this->buildPath($path), $data);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function file_exists($path) {
  72. return $this->flysystem->has($this->buildPath($path));
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function unlink($path) {
  78. if ($this->is_dir($path)) {
  79. return $this->rmdir($path);
  80. }
  81. try {
  82. return $this->flysystem->delete($this->buildPath($path));
  83. } catch (FileNotFoundException $e) {
  84. return false;
  85. }
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function rename($source, $target) {
  91. if ($this->file_exists($target)) {
  92. $this->unlink($target);
  93. }
  94. return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target));
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function copy($source, $target) {
  100. if ($this->file_exists($target)) {
  101. $this->unlink($target);
  102. }
  103. return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target));
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function filesize($path) {
  109. if ($this->is_dir($path)) {
  110. return 0;
  111. } else {
  112. return $this->flysystem->getSize($this->buildPath($path));
  113. }
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function mkdir($path) {
  119. if ($this->file_exists($path)) {
  120. return false;
  121. }
  122. return $this->flysystem->createDir($this->buildPath($path));
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function filemtime($path) {
  128. return $this->flysystem->getTimestamp($this->buildPath($path));
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function rmdir($path) {
  134. try {
  135. return @$this->flysystem->deleteDir($this->buildPath($path));
  136. } catch (FileNotFoundException $e) {
  137. return false;
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function opendir($path) {
  144. try {
  145. $content = $this->flysystem->listContents($this->buildPath($path));
  146. } catch (FileNotFoundException $e) {
  147. return false;
  148. }
  149. $names = array_map(function ($object) {
  150. return $object['basename'];
  151. }, $content);
  152. return IteratorDirectory::wrap($names);
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function fopen($path, $mode) {
  158. $fullPath = $this->buildPath($path);
  159. $useExisting = true;
  160. switch ($mode) {
  161. case 'r':
  162. case 'rb':
  163. try {
  164. return $this->flysystem->readStream($fullPath);
  165. } catch (FileNotFoundException $e) {
  166. return false;
  167. }
  168. case 'w':
  169. case 'w+':
  170. case 'wb':
  171. case 'wb+':
  172. $useExisting = false;
  173. case 'a':
  174. case 'ab':
  175. case 'r+':
  176. case 'a+':
  177. case 'x':
  178. case 'x+':
  179. case 'c':
  180. case 'c+':
  181. //emulate these
  182. if ($useExisting and $this->file_exists($path)) {
  183. if (!$this->isUpdatable($path)) {
  184. return false;
  185. }
  186. $tmpFile = $this->getCachedFile($path);
  187. } else {
  188. if (!$this->isCreatable(dirname($path))) {
  189. return false;
  190. }
  191. $tmpFile = \OCP\Files::tmpFile();
  192. }
  193. $source = fopen($tmpFile, $mode);
  194. return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
  195. $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
  196. unlink($tmpFile);
  197. });
  198. }
  199. return false;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function touch($path, $mtime = null) {
  205. if ($this->file_exists($path)) {
  206. return false;
  207. } else {
  208. $this->file_put_contents($path, '');
  209. return true;
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function stat($path) {
  216. $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']);
  217. return [
  218. 'mtime' => $info['timestamp'],
  219. 'size' => $info['size']
  220. ];
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function filetype($path) {
  226. if ($path === '' or $path === '/' or $path === '.') {
  227. return 'dir';
  228. }
  229. try {
  230. $info = $this->flysystem->getMetadata($this->buildPath($path));
  231. } catch (FileNotFoundException $e) {
  232. return false;
  233. }
  234. return $info['type'];
  235. }
  236. }