1
0

FileInfo.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. namespace OC\Files;
  8. use OC\Files\Mount\HomeMountPoint;
  9. use OCA\Files_Sharing\External\Mount;
  10. use OCA\Files_Sharing\ISharedMountPoint;
  11. use OCP\Files\Cache\ICacheEntry;
  12. use OCP\Files\Mount\IMountPoint;
  13. use OCP\IUser;
  14. /**
  15. * @template-implements \ArrayAccess<string,mixed>
  16. */
  17. class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
  18. private array|ICacheEntry $data;
  19. /**
  20. * @var string
  21. */
  22. private $path;
  23. /**
  24. * @var \OC\Files\Storage\Storage $storage
  25. */
  26. private $storage;
  27. /**
  28. * @var string
  29. */
  30. private $internalPath;
  31. /**
  32. * @var \OCP\Files\Mount\IMountPoint
  33. */
  34. private $mount;
  35. private ?IUser $owner;
  36. /**
  37. * @var string[]
  38. */
  39. private array $childEtags = [];
  40. /**
  41. * @var IMountPoint[]
  42. */
  43. private array $subMounts = [];
  44. private bool $subMountsUsed = false;
  45. /**
  46. * The size of the file/folder without any sub mount
  47. */
  48. private int|float $rawSize = 0;
  49. /**
  50. * @param string|boolean $path
  51. * @param Storage\Storage $storage
  52. * @param string $internalPath
  53. * @param array|ICacheEntry $data
  54. * @param IMountPoint $mount
  55. * @param ?IUser $owner
  56. */
  57. public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) {
  58. $this->path = $path;
  59. $this->storage = $storage;
  60. $this->internalPath = $internalPath;
  61. $this->data = $data;
  62. $this->mount = $mount;
  63. $this->owner = $owner;
  64. if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] !== 0) {
  65. $this->rawSize = $this->data['unencrypted_size'];
  66. } else {
  67. $this->rawSize = $this->data['size'] ?? 0;
  68. }
  69. }
  70. public function offsetSet($offset, $value): void {
  71. if (is_null($offset)) {
  72. throw new \TypeError('Null offset not supported');
  73. }
  74. $this->data[$offset] = $value;
  75. }
  76. public function offsetExists($offset): bool {
  77. return isset($this->data[$offset]);
  78. }
  79. public function offsetUnset($offset): void {
  80. unset($this->data[$offset]);
  81. }
  82. public function offsetGet(mixed $offset): mixed {
  83. return match ($offset) {
  84. 'type' => $this->getType(),
  85. 'etag' => $this->getEtag(),
  86. 'size' => $this->getSize(),
  87. 'mtime' => $this->getMTime(),
  88. 'permissions' => $this->getPermissions(),
  89. default => $this->data[$offset] ?? null,
  90. };
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getPath() {
  96. return $this->path;
  97. }
  98. public function getStorage() {
  99. return $this->storage;
  100. }
  101. /**
  102. * @return string
  103. */
  104. public function getInternalPath() {
  105. return $this->internalPath;
  106. }
  107. /**
  108. * Get FileInfo ID or null in case of part file
  109. *
  110. * @return int|null
  111. */
  112. public function getId() {
  113. return isset($this->data['fileid']) ? (int)$this->data['fileid'] : null;
  114. }
  115. /**
  116. * @return string
  117. */
  118. public function getMimetype() {
  119. return $this->data['mimetype'];
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getMimePart() {
  125. return $this->data['mimepart'];
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getName() {
  131. return empty($this->data['name'])
  132. ? basename($this->getPath())
  133. : $this->data['name'];
  134. }
  135. /**
  136. * @return string
  137. */
  138. public function getEtag() {
  139. $this->updateEntryFromSubMounts();
  140. if (count($this->childEtags) > 0) {
  141. $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
  142. return md5($combinedEtag);
  143. } else {
  144. return $this->data['etag'];
  145. }
  146. }
  147. /**
  148. * @param bool $includeMounts
  149. * @return int|float
  150. */
  151. public function getSize($includeMounts = true) {
  152. if ($includeMounts) {
  153. $this->updateEntryFromSubMounts();
  154. if ($this->isEncrypted() && isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
  155. return $this->data['unencrypted_size'];
  156. } else {
  157. return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
  158. }
  159. } else {
  160. return $this->rawSize;
  161. }
  162. }
  163. /**
  164. * @return int
  165. */
  166. public function getMTime() {
  167. $this->updateEntryFromSubMounts();
  168. return (int)$this->data['mtime'];
  169. }
  170. /**
  171. * @return bool
  172. */
  173. public function isEncrypted() {
  174. return $this->data['encrypted'] ?? false;
  175. }
  176. /**
  177. * Return the current version used for the HMAC in the encryption app
  178. */
  179. public function getEncryptedVersion(): int {
  180. return isset($this->data['encryptedVersion']) ? (int)$this->data['encryptedVersion'] : 1;
  181. }
  182. /**
  183. * @return int
  184. */
  185. public function getPermissions() {
  186. return (int)$this->data['permissions'];
  187. }
  188. /**
  189. * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  190. */
  191. public function getType() {
  192. if (!isset($this->data['type'])) {
  193. $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE;
  194. }
  195. return $this->data['type'];
  196. }
  197. public function getData() {
  198. return $this->data;
  199. }
  200. /**
  201. * @param int $permissions
  202. * @return bool
  203. */
  204. protected function checkPermissions($permissions) {
  205. return ($this->getPermissions() & $permissions) === $permissions;
  206. }
  207. /**
  208. * @return bool
  209. */
  210. public function isReadable() {
  211. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  212. }
  213. /**
  214. * @return bool
  215. */
  216. public function isUpdateable() {
  217. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  218. }
  219. /**
  220. * Check whether new files or folders can be created inside this folder
  221. *
  222. * @return bool
  223. */
  224. public function isCreatable() {
  225. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  226. }
  227. /**
  228. * @return bool
  229. */
  230. public function isDeletable() {
  231. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  232. }
  233. /**
  234. * @return bool
  235. */
  236. public function isShareable() {
  237. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  238. }
  239. /**
  240. * Check if a file or folder is shared
  241. *
  242. * @return bool
  243. */
  244. public function isShared() {
  245. return $this->mount instanceof ISharedMountPoint;
  246. }
  247. public function isMounted() {
  248. $isHome = $this->mount instanceof HomeMountPoint;
  249. return !$isHome && !$this->isShared();
  250. }
  251. /**
  252. * Get the mountpoint the file belongs to
  253. *
  254. * @return \OCP\Files\Mount\IMountPoint
  255. */
  256. public function getMountPoint() {
  257. return $this->mount;
  258. }
  259. /**
  260. * Get the owner of the file
  261. *
  262. * @return ?IUser
  263. */
  264. public function getOwner() {
  265. return $this->owner;
  266. }
  267. /**
  268. * @param IMountPoint[] $mounts
  269. */
  270. public function setSubMounts(array $mounts) {
  271. $this->subMounts = $mounts;
  272. }
  273. private function updateEntryFromSubMounts(): void {
  274. if ($this->subMountsUsed) {
  275. return;
  276. }
  277. $this->subMountsUsed = true;
  278. foreach ($this->subMounts as $mount) {
  279. $subStorage = $mount->getStorage();
  280. if ($subStorage) {
  281. $subCache = $subStorage->getCache('');
  282. $rootEntry = $subCache->get('');
  283. $this->addSubEntry($rootEntry, $mount->getMountPoint());
  284. }
  285. }
  286. }
  287. /**
  288. * Add a cache entry which is the child of this folder
  289. *
  290. * Sets the size, etag and size to for cross-storage childs
  291. *
  292. * @param array|ICacheEntry $data cache entry for the child
  293. * @param string $entryPath full path of the child entry
  294. */
  295. public function addSubEntry($data, $entryPath) {
  296. if (!$data) {
  297. return;
  298. }
  299. $hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
  300. if ($hasUnencryptedSize) {
  301. $subSize = $data['unencrypted_size'];
  302. } else {
  303. $subSize = $data['size'] ?: 0;
  304. }
  305. $this->data['size'] += $subSize;
  306. if ($hasUnencryptedSize) {
  307. $this->data['unencrypted_size'] += $subSize;
  308. }
  309. if (isset($data['mtime'])) {
  310. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  311. }
  312. if (isset($data['etag'])) {
  313. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  314. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  315. // attach the permissions to propagate etag on permission changes of submounts
  316. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  317. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  318. }
  319. }
  320. /**
  321. * @inheritdoc
  322. */
  323. public function getChecksum() {
  324. return $this->data['checksum'];
  325. }
  326. public function getExtension(): string {
  327. return pathinfo($this->getName(), PATHINFO_EXTENSION);
  328. }
  329. public function getCreationTime(): int {
  330. return (int)$this->data['creation_time'];
  331. }
  332. public function getUploadTime(): int {
  333. return (int)$this->data['upload_time'];
  334. }
  335. public function getParentId(): int {
  336. return $this->data['parent'] ?? -1;
  337. }
  338. /**
  339. * @inheritDoc
  340. * @return array<string, int|string|bool|float|string[]|int[]>
  341. */
  342. public function getMetadata(): array {
  343. return $this->data['metadata'] ?? [];
  344. }
  345. }