FileInfo.php 9.6 KB

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