IStorage.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. // use OCP namespace for all classes that are considered public.
  8. // This means that they should be used by apps instead of the internal Nextcloud classes
  9. namespace OCP\Files\Storage;
  10. use OCP\Files\Cache\ICache;
  11. use OCP\Files\Cache\IPropagator;
  12. use OCP\Files\Cache\IScanner;
  13. use OCP\Files\Cache\IUpdater;
  14. use OCP\Files\Cache\IWatcher;
  15. use OCP\Files\InvalidPathException;
  16. /**
  17. * Provide a common interface to all different storage options
  18. *
  19. * All paths passed to the storage are relative to the storage and should NOT have a leading slash.
  20. *
  21. * @since 9.0.0
  22. */
  23. interface IStorage {
  24. /**
  25. * $parameters is a free form array with the configuration options needed to construct the storage
  26. *
  27. * @param array $parameters
  28. * @since 9.0.0
  29. */
  30. public function __construct($parameters);
  31. /**
  32. * Get the identifier for the storage,
  33. * the returned id should be the same for every storage object that is created with the same parameters
  34. * and two storage objects with the same id should refer to two storages that display the same files.
  35. *
  36. * @return string
  37. * @since 9.0.0
  38. */
  39. public function getId();
  40. /**
  41. * see https://www.php.net/manual/en/function.mkdir.php
  42. * implementations need to implement a recursive mkdir
  43. *
  44. * @param string $path
  45. * @return bool
  46. * @since 9.0.0
  47. */
  48. public function mkdir($path);
  49. /**
  50. * see https://www.php.net/manual/en/function.rmdir.php
  51. *
  52. * @param string $path
  53. * @return bool
  54. * @since 9.0.0
  55. */
  56. public function rmdir($path);
  57. /**
  58. * see https://www.php.net/manual/en/function.opendir.php
  59. *
  60. * @param string $path
  61. * @return resource|false
  62. * @since 9.0.0
  63. */
  64. public function opendir($path);
  65. /**
  66. * see https://www.php.net/manual/en/function.is-dir.php
  67. *
  68. * @param string $path
  69. * @return bool
  70. * @since 9.0.0
  71. */
  72. public function is_dir($path);
  73. /**
  74. * see https://www.php.net/manual/en/function.is-file.php
  75. *
  76. * @param string $path
  77. * @return bool
  78. * @since 9.0.0
  79. */
  80. public function is_file($path);
  81. /**
  82. * see https://www.php.net/manual/en/function.stat.php
  83. * only the following keys are required in the result: size and mtime
  84. *
  85. * @param string $path
  86. * @return array|bool
  87. * @since 9.0.0
  88. */
  89. public function stat($path);
  90. /**
  91. * see https://www.php.net/manual/en/function.filetype.php
  92. *
  93. * @param string $path
  94. * @return string|bool
  95. * @since 9.0.0
  96. */
  97. public function filetype($path);
  98. /**
  99. * see https://www.php.net/manual/en/function.filesize.php
  100. * The result for filesize when called on a folder is required to be 0
  101. *
  102. * @param string $path
  103. * @return false|int|float
  104. * @since 9.0.0
  105. */
  106. public function filesize($path);
  107. /**
  108. * check if a file can be created in $path
  109. *
  110. * @param string $path
  111. * @return bool
  112. * @since 9.0.0
  113. */
  114. public function isCreatable($path);
  115. /**
  116. * check if a file can be read
  117. *
  118. * @param string $path
  119. * @return bool
  120. * @since 9.0.0
  121. */
  122. public function isReadable($path);
  123. /**
  124. * check if a file can be written to
  125. *
  126. * @param string $path
  127. * @return bool
  128. * @since 9.0.0
  129. */
  130. public function isUpdatable($path);
  131. /**
  132. * check if a file can be deleted
  133. *
  134. * @param string $path
  135. * @return bool
  136. * @since 9.0.0
  137. */
  138. public function isDeletable($path);
  139. /**
  140. * check if a file can be shared
  141. *
  142. * @param string $path
  143. * @return bool
  144. * @since 9.0.0
  145. */
  146. public function isSharable($path);
  147. /**
  148. * get the full permissions of a path.
  149. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  150. *
  151. * @param string $path
  152. * @return int
  153. * @since 9.0.0
  154. */
  155. public function getPermissions($path);
  156. /**
  157. * see https://www.php.net/manual/en/function.file_exists.php
  158. *
  159. * @param string $path
  160. * @return bool
  161. * @since 9.0.0
  162. */
  163. public function file_exists($path);
  164. /**
  165. * see https://www.php.net/manual/en/function.filemtime.php
  166. *
  167. * @param string $path
  168. * @return int|bool
  169. * @since 9.0.0
  170. */
  171. public function filemtime($path);
  172. /**
  173. * see https://www.php.net/manual/en/function.file_get_contents.php
  174. *
  175. * @param string $path
  176. * @return string|false
  177. * @since 9.0.0
  178. */
  179. public function file_get_contents($path);
  180. /**
  181. * see https://www.php.net/manual/en/function.file_put_contents.php
  182. *
  183. * @param string $path
  184. * @param mixed $data
  185. * @return int|float|false
  186. * @since 9.0.0
  187. */
  188. public function file_put_contents($path, $data);
  189. /**
  190. * see https://www.php.net/manual/en/function.unlink.php
  191. *
  192. * @param string $path
  193. * @return bool
  194. * @since 9.0.0
  195. */
  196. public function unlink($path);
  197. /**
  198. * see https://www.php.net/manual/en/function.rename.php
  199. *
  200. * @param string $source
  201. * @param string $target
  202. * @return bool
  203. * @since 9.0.0
  204. */
  205. public function rename($source, $target);
  206. /**
  207. * see https://www.php.net/manual/en/function.copy.php
  208. *
  209. * @param string $source
  210. * @param string $target
  211. * @return bool
  212. * @since 9.0.0
  213. */
  214. public function copy($source, $target);
  215. /**
  216. * see https://www.php.net/manual/en/function.fopen.php
  217. *
  218. * @param string $path
  219. * @param string $mode
  220. * @return resource|bool
  221. * @since 9.0.0
  222. */
  223. public function fopen($path, $mode);
  224. /**
  225. * get the mimetype for a file or folder
  226. * The mimetype for a folder is required to be "httpd/unix-directory"
  227. *
  228. * @param string $path
  229. * @return string|bool
  230. * @since 9.0.0
  231. */
  232. public function getMimeType($path);
  233. /**
  234. * see https://www.php.net/manual/en/function.hash-file.php
  235. *
  236. * @param string $type
  237. * @param string $path
  238. * @param bool $raw
  239. * @return string|bool
  240. * @since 9.0.0
  241. */
  242. public function hash($type, $path, $raw = false);
  243. /**
  244. * see https://www.php.net/manual/en/function.free_space.php
  245. *
  246. * @param string $path
  247. * @return int|float|bool
  248. * @since 9.0.0
  249. */
  250. public function free_space($path);
  251. /**
  252. * see https://www.php.net/manual/en/function.touch.php
  253. * If the backend does not support the operation, false should be returned
  254. *
  255. * @param string $path
  256. * @param int $mtime
  257. * @return bool
  258. * @since 9.0.0
  259. */
  260. public function touch($path, $mtime = null);
  261. /**
  262. * get the path to a local version of the file.
  263. * The local version of the file can be temporary and doesn't have to be persistent across requests
  264. *
  265. * @param string $path
  266. * @return string|false
  267. * @since 9.0.0
  268. */
  269. public function getLocalFile($path);
  270. /**
  271. * check if a file or folder has been updated since $time
  272. *
  273. * @param string $path
  274. * @param int $time
  275. * @return bool
  276. * @since 9.0.0
  277. *
  278. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  279. * returning true for other changes in the folder is optional
  280. */
  281. public function hasUpdated($path, $time);
  282. /**
  283. * get the ETag for a file or folder
  284. *
  285. * @param string $path
  286. * @return string|false
  287. * @since 9.0.0
  288. */
  289. public function getETag($path);
  290. /**
  291. * Returns whether the storage is local, which means that files
  292. * are stored on the local filesystem instead of remotely.
  293. * Calling getLocalFile() for local storages should always
  294. * return the local files, whereas for non-local storages
  295. * it might return a temporary file.
  296. *
  297. * @return bool true if the files are stored locally, false otherwise
  298. * @since 9.0.0
  299. */
  300. public function isLocal();
  301. /**
  302. * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
  303. *
  304. * @template T of IStorage
  305. * @param string $class
  306. * @psalm-param class-string<T> $class
  307. * @return bool
  308. * @since 9.0.0
  309. * @psalm-assert-if-true T $this
  310. */
  311. public function instanceOfStorage($class);
  312. /**
  313. * A custom storage implementation can return an url for direct download of a give file.
  314. *
  315. * For now the returned array can hold the parameter url - in future more attributes might follow.
  316. *
  317. * @param string $path
  318. * @return array|bool
  319. * @since 9.0.0
  320. */
  321. public function getDirectDownload($path);
  322. /**
  323. * @param string $path the path of the target folder
  324. * @param string $fileName the name of the file itself
  325. * @return void
  326. * @throws InvalidPathException
  327. * @since 9.0.0
  328. */
  329. public function verifyPath($path, $fileName);
  330. /**
  331. * @param IStorage $sourceStorage
  332. * @param string $sourceInternalPath
  333. * @param string $targetInternalPath
  334. * @return bool
  335. * @since 9.0.0
  336. */
  337. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath);
  338. /**
  339. * @param IStorage $sourceStorage
  340. * @param string $sourceInternalPath
  341. * @param string $targetInternalPath
  342. * @return bool
  343. * @since 9.0.0
  344. */
  345. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath);
  346. /**
  347. * Test a storage for availability
  348. *
  349. * @since 9.0.0
  350. * @return bool
  351. */
  352. public function test();
  353. /**
  354. * @since 9.0.0
  355. * @return array [ available, last_checked ]
  356. */
  357. public function getAvailability();
  358. /**
  359. * @since 9.0.0
  360. * @param bool $isAvailable
  361. */
  362. public function setAvailability($isAvailable);
  363. /**
  364. * @param string $path path for which to retrieve the owner
  365. * @since 9.0.0
  366. */
  367. public function getOwner($path);
  368. /**
  369. * @param string $path
  370. * @param IStorage|null $storage
  371. * @return ICache
  372. * @since 9.0.0
  373. */
  374. public function getCache($path = '', $storage = null);
  375. /**
  376. * @return IPropagator
  377. * @since 9.0.0
  378. */
  379. public function getPropagator();
  380. /**
  381. * @return IScanner
  382. * @since 9.0.0
  383. */
  384. public function getScanner();
  385. /**
  386. * @return IUpdater
  387. * @since 9.0.0
  388. */
  389. public function getUpdater();
  390. /**
  391. * @return IWatcher
  392. * @since 9.0.0
  393. */
  394. public function getWatcher();
  395. /**
  396. * Allow setting the storage owner
  397. *
  398. * This can be used for storages that do not have a dedicated owner, where we want to
  399. * pass the user that we setup the mountpoint for along to the storage layer
  400. *
  401. * @param string|null $user Owner user id
  402. * @return void
  403. * @since 30.0.0
  404. */
  405. public function setOwner(?string $user): void;
  406. }