FileInfo.php 9.5 KB

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