FileInfo.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Maxence Lange <maxence@artificial-owl.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Piotr M <mrow4a@yahoo.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author tbartenstein <tbartenstein@users.noreply.github.com>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\Files;
  35. use OC\Files\Mount\HomeMountPoint;
  36. use OCA\Files_Sharing\External\Mount;
  37. use OCA\Files_Sharing\ISharedMountPoint;
  38. use OCP\Files\Cache\ICacheEntry;
  39. use OCP\Files\Mount\IMountPoint;
  40. use OCP\IUser;
  41. /**
  42. * @template-implements \ArrayAccess<string,mixed>
  43. */
  44. class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
  45. private array|ICacheEntry $data;
  46. /**
  47. * @var string
  48. */
  49. private $path;
  50. /**
  51. * @var \OC\Files\Storage\Storage $storage
  52. */
  53. private $storage;
  54. /**
  55. * @var string
  56. */
  57. private $internalPath;
  58. /**
  59. * @var \OCP\Files\Mount\IMountPoint
  60. */
  61. private $mount;
  62. private ?IUser $owner;
  63. /**
  64. * @var string[]
  65. */
  66. private array $childEtags = [];
  67. /**
  68. * @var IMountPoint[]
  69. */
  70. private array $subMounts = [];
  71. private bool $subMountsUsed = false;
  72. /**
  73. * The size of the file/folder without any sub mount
  74. */
  75. private int|float $rawSize = 0;
  76. /**
  77. * @param string|boolean $path
  78. * @param Storage\Storage $storage
  79. * @param string $internalPath
  80. * @param array|ICacheEntry $data
  81. * @param IMountPoint $mount
  82. * @param ?IUser $owner
  83. */
  84. public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) {
  85. $this->path = $path;
  86. $this->storage = $storage;
  87. $this->internalPath = $internalPath;
  88. $this->data = $data;
  89. $this->mount = $mount;
  90. $this->owner = $owner;
  91. if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] !== 0) {
  92. $this->rawSize = $this->data['unencrypted_size'];
  93. } else {
  94. $this->rawSize = $this->data['size'] ?? 0;
  95. }
  96. }
  97. public function offsetSet($offset, $value): void {
  98. if (is_null($offset)) {
  99. throw new \TypeError('Null offset not supported');
  100. }
  101. $this->data[$offset] = $value;
  102. }
  103. public function offsetExists($offset): bool {
  104. return isset($this->data[$offset]);
  105. }
  106. public function offsetUnset($offset): void {
  107. unset($this->data[$offset]);
  108. }
  109. /**
  110. * @return mixed
  111. */
  112. #[\ReturnTypeWillChange]
  113. public function offsetGet($offset) {
  114. return match ($offset) {
  115. 'type' => $this->getType(),
  116. 'etag' => $this->getEtag(),
  117. 'size' => $this->getSize(),
  118. 'mtime' => $this->getMTime(),
  119. 'permissions' => $this->getPermissions(),
  120. default => $this->data[$offset] ?? null,
  121. };
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getPath() {
  127. return $this->path;
  128. }
  129. public function getStorage() {
  130. return $this->storage;
  131. }
  132. /**
  133. * @return string
  134. */
  135. public function getInternalPath() {
  136. return $this->internalPath;
  137. }
  138. /**
  139. * Get FileInfo ID or null in case of part file
  140. *
  141. * @return int|null
  142. */
  143. public function getId() {
  144. return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null;
  145. }
  146. /**
  147. * @return string
  148. */
  149. public function getMimetype() {
  150. return $this->data['mimetype'];
  151. }
  152. /**
  153. * @return string
  154. */
  155. public function getMimePart() {
  156. return $this->data['mimepart'];
  157. }
  158. /**
  159. * @return string
  160. */
  161. public function getName() {
  162. return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath());
  163. }
  164. /**
  165. * @return string
  166. */
  167. public function getEtag() {
  168. $this->updateEntryfromSubMounts();
  169. if (count($this->childEtags) > 0) {
  170. $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
  171. return md5($combinedEtag);
  172. } else {
  173. return $this->data['etag'];
  174. }
  175. }
  176. /**
  177. * @param bool $includeMounts
  178. * @return int|float
  179. */
  180. public function getSize($includeMounts = true) {
  181. if ($includeMounts) {
  182. $this->updateEntryfromSubMounts();
  183. if ($this->isEncrypted() && isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
  184. return $this->data['unencrypted_size'];
  185. } else {
  186. return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
  187. }
  188. } else {
  189. return $this->rawSize;
  190. }
  191. }
  192. /**
  193. * @return int
  194. */
  195. public function getMTime() {
  196. $this->updateEntryfromSubMounts();
  197. return (int) $this->data['mtime'];
  198. }
  199. /**
  200. * @return bool
  201. */
  202. public function isEncrypted() {
  203. return $this->data['encrypted'] ?? false;
  204. }
  205. /**
  206. * Return the current version used for the HMAC in the encryption app
  207. */
  208. public function getEncryptedVersion(): int {
  209. return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
  210. }
  211. /**
  212. * @return int
  213. */
  214. public function getPermissions() {
  215. return (int) $this->data['permissions'];
  216. }
  217. /**
  218. * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  219. */
  220. public function getType() {
  221. if (!isset($this->data['type'])) {
  222. $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE;
  223. }
  224. return $this->data['type'];
  225. }
  226. public function getData() {
  227. return $this->data;
  228. }
  229. /**
  230. * @param int $permissions
  231. * @return bool
  232. */
  233. protected function checkPermissions($permissions) {
  234. return ($this->getPermissions() & $permissions) === $permissions;
  235. }
  236. /**
  237. * @return bool
  238. */
  239. public function isReadable() {
  240. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  241. }
  242. /**
  243. * @return bool
  244. */
  245. public function isUpdateable() {
  246. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  247. }
  248. /**
  249. * Check whether new files or folders can be created inside this folder
  250. *
  251. * @return bool
  252. */
  253. public function isCreatable() {
  254. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  255. }
  256. /**
  257. * @return bool
  258. */
  259. public function isDeletable() {
  260. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  261. }
  262. /**
  263. * @return bool
  264. */
  265. public function isShareable() {
  266. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  267. }
  268. /**
  269. * Check if a file or folder is shared
  270. *
  271. * @return bool
  272. */
  273. public function isShared() {
  274. return $this->mount instanceof ISharedMountPoint;
  275. }
  276. public function isMounted() {
  277. $isHome = $this->mount instanceof HomeMountPoint;
  278. return !$isHome && !$this->isShared();
  279. }
  280. /**
  281. * Get the mountpoint the file belongs to
  282. *
  283. * @return \OCP\Files\Mount\IMountPoint
  284. */
  285. public function getMountPoint() {
  286. return $this->mount;
  287. }
  288. /**
  289. * Get the owner of the file
  290. *
  291. * @return ?IUser
  292. */
  293. public function getOwner() {
  294. return $this->owner;
  295. }
  296. /**
  297. * @param IMountPoint[] $mounts
  298. */
  299. public function setSubMounts(array $mounts) {
  300. $this->subMounts = $mounts;
  301. }
  302. private function updateEntryfromSubMounts(): void {
  303. if ($this->subMountsUsed) {
  304. return;
  305. }
  306. $this->subMountsUsed = true;
  307. foreach ($this->subMounts as $mount) {
  308. $subStorage = $mount->getStorage();
  309. if ($subStorage) {
  310. $subCache = $subStorage->getCache('');
  311. $rootEntry = $subCache->get('');
  312. $this->addSubEntry($rootEntry, $mount->getMountPoint());
  313. }
  314. }
  315. }
  316. /**
  317. * Add a cache entry which is the child of this folder
  318. *
  319. * Sets the size, etag and size to for cross-storage childs
  320. *
  321. * @param array|ICacheEntry $data cache entry for the child
  322. * @param string $entryPath full path of the child entry
  323. */
  324. public function addSubEntry($data, $entryPath) {
  325. if (!$data) {
  326. return;
  327. }
  328. $hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
  329. if ($hasUnencryptedSize) {
  330. $subSize = $data['unencrypted_size'];
  331. } else {
  332. $subSize = $data['size'] ?: 0;
  333. }
  334. $this->data['size'] += $subSize;
  335. if ($hasUnencryptedSize) {
  336. $this->data['unencrypted_size'] += $subSize;
  337. }
  338. if (isset($data['mtime'])) {
  339. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  340. }
  341. if (isset($data['etag'])) {
  342. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  343. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  344. // attach the permissions to propagate etag on permission changes of submounts
  345. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  346. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  347. }
  348. }
  349. /**
  350. * @inheritdoc
  351. */
  352. public function getChecksum() {
  353. return $this->data['checksum'];
  354. }
  355. public function getExtension(): string {
  356. return pathinfo($this->getName(), PATHINFO_EXTENSION);
  357. }
  358. public function getCreationTime(): int {
  359. return (int) $this->data['creation_time'];
  360. }
  361. public function getUploadTime(): int {
  362. return (int) $this->data['upload_time'];
  363. }
  364. public function getParentId(): int {
  365. return $this->data['parent'] ?? -1;
  366. }
  367. /**
  368. * @inheritDoc
  369. * @return array<string, int|string|bool|float|string[]|int[]>
  370. */
  371. public function getMetadata(): array {
  372. return $this->data['metadata'] ?? [];
  373. }
  374. }