1
0

FileInfo.php 8.5 KB

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