FileInfo.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 empty($this->data['name'])
  163. ? basename($this->getPath())
  164. : $this->data['name'];
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function getEtag() {
  170. $this->updateEntryfromSubMounts();
  171. if (count($this->childEtags) > 0) {
  172. $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
  173. return md5($combinedEtag);
  174. } else {
  175. return $this->data['etag'];
  176. }
  177. }
  178. /**
  179. * @param bool $includeMounts
  180. * @return int|float
  181. */
  182. public function getSize($includeMounts = true) {
  183. if ($includeMounts) {
  184. $this->updateEntryfromSubMounts();
  185. if ($this->isEncrypted() && isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
  186. return $this->data['unencrypted_size'];
  187. } else {
  188. return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
  189. }
  190. } else {
  191. return $this->rawSize;
  192. }
  193. }
  194. /**
  195. * @return int
  196. */
  197. public function getMTime() {
  198. $this->updateEntryfromSubMounts();
  199. return (int) $this->data['mtime'];
  200. }
  201. /**
  202. * @return bool
  203. */
  204. public function isEncrypted() {
  205. return $this->data['encrypted'] ?? false;
  206. }
  207. /**
  208. * Return the current version used for the HMAC in the encryption app
  209. */
  210. public function getEncryptedVersion(): int {
  211. return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
  212. }
  213. /**
  214. * @return int
  215. */
  216. public function getPermissions() {
  217. return (int) $this->data['permissions'];
  218. }
  219. /**
  220. * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  221. */
  222. public function getType() {
  223. if (!isset($this->data['type'])) {
  224. $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE;
  225. }
  226. return $this->data['type'];
  227. }
  228. public function getData() {
  229. return $this->data;
  230. }
  231. /**
  232. * @param int $permissions
  233. * @return bool
  234. */
  235. protected function checkPermissions($permissions) {
  236. return ($this->getPermissions() & $permissions) === $permissions;
  237. }
  238. /**
  239. * @return bool
  240. */
  241. public function isReadable() {
  242. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  243. }
  244. /**
  245. * @return bool
  246. */
  247. public function isUpdateable() {
  248. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  249. }
  250. /**
  251. * Check whether new files or folders can be created inside this folder
  252. *
  253. * @return bool
  254. */
  255. public function isCreatable() {
  256. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  257. }
  258. /**
  259. * @return bool
  260. */
  261. public function isDeletable() {
  262. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  263. }
  264. /**
  265. * @return bool
  266. */
  267. public function isShareable() {
  268. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  269. }
  270. /**
  271. * Check if a file or folder is shared
  272. *
  273. * @return bool
  274. */
  275. public function isShared() {
  276. return $this->mount instanceof ISharedMountPoint;
  277. }
  278. public function isMounted() {
  279. $isHome = $this->mount instanceof HomeMountPoint;
  280. return !$isHome && !$this->isShared();
  281. }
  282. /**
  283. * Get the mountpoint the file belongs to
  284. *
  285. * @return \OCP\Files\Mount\IMountPoint
  286. */
  287. public function getMountPoint() {
  288. return $this->mount;
  289. }
  290. /**
  291. * Get the owner of the file
  292. *
  293. * @return ?IUser
  294. */
  295. public function getOwner() {
  296. return $this->owner;
  297. }
  298. /**
  299. * @param IMountPoint[] $mounts
  300. */
  301. public function setSubMounts(array $mounts) {
  302. $this->subMounts = $mounts;
  303. }
  304. private function updateEntryfromSubMounts(): void {
  305. if ($this->subMountsUsed) {
  306. return;
  307. }
  308. $this->subMountsUsed = true;
  309. foreach ($this->subMounts as $mount) {
  310. $subStorage = $mount->getStorage();
  311. if ($subStorage) {
  312. $subCache = $subStorage->getCache('');
  313. $rootEntry = $subCache->get('');
  314. $this->addSubEntry($rootEntry, $mount->getMountPoint());
  315. }
  316. }
  317. }
  318. /**
  319. * Add a cache entry which is the child of this folder
  320. *
  321. * Sets the size, etag and size to for cross-storage childs
  322. *
  323. * @param array|ICacheEntry $data cache entry for the child
  324. * @param string $entryPath full path of the child entry
  325. */
  326. public function addSubEntry($data, $entryPath) {
  327. if (!$data) {
  328. return;
  329. }
  330. $hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
  331. if ($hasUnencryptedSize) {
  332. $subSize = $data['unencrypted_size'];
  333. } else {
  334. $subSize = $data['size'] ?: 0;
  335. }
  336. $this->data['size'] += $subSize;
  337. if ($hasUnencryptedSize) {
  338. $this->data['unencrypted_size'] += $subSize;
  339. }
  340. if (isset($data['mtime'])) {
  341. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  342. }
  343. if (isset($data['etag'])) {
  344. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  345. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  346. // attach the permissions to propagate etag on permission changes of submounts
  347. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  348. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  349. }
  350. }
  351. /**
  352. * @inheritdoc
  353. */
  354. public function getChecksum() {
  355. return $this->data['checksum'];
  356. }
  357. public function getExtension(): string {
  358. return pathinfo($this->getName(), PATHINFO_EXTENSION);
  359. }
  360. public function getCreationTime(): int {
  361. return (int) $this->data['creation_time'];
  362. }
  363. public function getUploadTime(): int {
  364. return (int) $this->data['upload_time'];
  365. }
  366. public function getParentId(): int {
  367. return $this->data['parent'] ?? -1;
  368. }
  369. /**
  370. * @inheritDoc
  371. * @return array<string, int|string|bool|float|string[]|int[]>
  372. */
  373. public function getMetadata(): array {
  374. return $this->data['metadata'] ?? [];
  375. }
  376. }