FileInfo.php 9.7 KB

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