IStorage.php 8.9 KB

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