FileInfo.php 9.6 KB

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