fileinfo.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files;
  28. class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
  29. /**
  30. * @var array $data
  31. */
  32. private $data;
  33. /**
  34. * @var string $path
  35. */
  36. private $path;
  37. /**
  38. * @var \OC\Files\Storage\Storage $storage
  39. */
  40. private $storage;
  41. /**
  42. * @var string $internalPath
  43. */
  44. private $internalPath;
  45. /**
  46. * @var \OCP\Files\Mount\IMountPoint
  47. */
  48. private $mount;
  49. /**
  50. * @param string|boolean $path
  51. * @param Storage\Storage $storage
  52. * @param string $internalPath
  53. * @param array $data
  54. * @param \OCP\Files\Mount\IMountPoint $mount
  55. */
  56. public function __construct($path, $storage, $internalPath, $data, $mount) {
  57. $this->path = $path;
  58. $this->storage = $storage;
  59. $this->internalPath = $internalPath;
  60. $this->data = $data;
  61. $this->mount = $mount;
  62. }
  63. public function offsetSet($offset, $value) {
  64. $this->data[$offset] = $value;
  65. }
  66. public function offsetExists($offset) {
  67. return isset($this->data[$offset]);
  68. }
  69. public function offsetUnset($offset) {
  70. unset($this->data[$offset]);
  71. }
  72. public function offsetGet($offset) {
  73. if ($offset === 'type') {
  74. return $this->getType();
  75. } elseif (isset($this->data[$offset])) {
  76. return $this->data[$offset];
  77. } else {
  78. return null;
  79. }
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getPath() {
  85. return $this->path;
  86. }
  87. /**
  88. * @return \OCP\Files\Storage
  89. */
  90. public function getStorage() {
  91. return $this->storage;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getInternalPath() {
  97. return $this->internalPath;
  98. }
  99. /**
  100. * @return int
  101. */
  102. public function getId() {
  103. return $this->data['fileid'];
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getMimetype() {
  109. return $this->data['mimetype'];
  110. }
  111. /**
  112. * @return string
  113. */
  114. public function getMimePart() {
  115. return $this->data['mimepart'];
  116. }
  117. /**
  118. * @return string
  119. */
  120. public function getName() {
  121. return basename($this->getPath());
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getEtag() {
  127. return $this->data['etag'];
  128. }
  129. /**
  130. * @return int
  131. */
  132. public function getSize() {
  133. return isset($this->data['size']) ? $this->data['size'] : 0;
  134. }
  135. /**
  136. * @return int
  137. */
  138. public function getMTime() {
  139. return $this->data['mtime'];
  140. }
  141. /**
  142. * @return bool
  143. */
  144. public function isEncrypted() {
  145. return $this->data['encrypted'];
  146. }
  147. /**
  148. * @return int
  149. */
  150. public function getPermissions() {
  151. return $this->data['permissions'];
  152. }
  153. /**
  154. * @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  155. */
  156. public function getType() {
  157. if (!isset($this->data['type'])) {
  158. $this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE;
  159. }
  160. return $this->data['type'];
  161. }
  162. public function getData() {
  163. return $this->data;
  164. }
  165. /**
  166. * @param int $permissions
  167. * @return bool
  168. */
  169. protected function checkPermissions($permissions) {
  170. return ($this->getPermissions() & $permissions) === $permissions;
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function isReadable() {
  176. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  177. }
  178. /**
  179. * @return bool
  180. */
  181. public function isUpdateable() {
  182. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  183. }
  184. /**
  185. * Check whether new files or folders can be created inside this folder
  186. *
  187. * @return bool
  188. */
  189. public function isCreatable() {
  190. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  191. }
  192. /**
  193. * @return bool
  194. */
  195. public function isDeletable() {
  196. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  197. }
  198. /**
  199. * @return bool
  200. */
  201. public function isShareable() {
  202. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  203. }
  204. /**
  205. * Check if a file or folder is shared
  206. *
  207. * @return bool
  208. */
  209. public function isShared() {
  210. $sid = $this->getStorage()->getId();
  211. if (!is_null($sid)) {
  212. $sid = explode(':', $sid);
  213. return ($sid[0] === 'shared');
  214. }
  215. return false;
  216. }
  217. public function isMounted() {
  218. $sid = $this->getStorage()->getId();
  219. if (!is_null($sid)) {
  220. $sid = explode(':', $sid);
  221. return ($sid[0] !== 'local' and $sid[0] !== 'home' and $sid[0] !== 'shared');
  222. }
  223. return false;
  224. }
  225. /**
  226. * Get the mountpoint the file belongs to
  227. *
  228. * @return \OCP\Files\Mount\IMountPoint
  229. */
  230. public function getMountPoint() {
  231. return $this->mount;
  232. }
  233. }