You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storagesservice.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_external\Service;
  24. use \OCP\IUserSession;
  25. use \OC\Files\Filesystem;
  26. use \OCA\Files_external\Lib\StorageConfig;
  27. use \OCA\Files_external\NotFoundException;
  28. /**
  29. * Service class to manage external storages
  30. */
  31. abstract class StoragesService {
  32. /**
  33. * Read legacy config data
  34. *
  35. * @return array list of mount configs
  36. */
  37. protected function readLegacyConfig() {
  38. // read global config
  39. return \OC_Mount_Config::readData();
  40. }
  41. /**
  42. * Copy legacy storage options into the given storage config object.
  43. *
  44. * @param StorageConfig $storageConfig storage config to populate
  45. * @param string $mountType mount type
  46. * @param string $applicable applicable user or group
  47. * @param array $storageOptions legacy storage options
  48. *
  49. * @return StorageConfig populated storage config
  50. */
  51. protected function populateStorageConfigWithLegacyOptions(
  52. &$storageConfig,
  53. $mountType,
  54. $applicable,
  55. $storageOptions
  56. ) {
  57. $storageConfig->setBackendClass($storageOptions['class']);
  58. $storageConfig->setBackendOptions($storageOptions['options']);
  59. if (isset($storageOptions['mountOptions'])) {
  60. $storageConfig->setMountOptions($storageOptions['mountOptions']);
  61. }
  62. if (isset($storageOptions['priority'])) {
  63. $storageConfig->setPriority($storageOptions['priority']);
  64. }
  65. if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) {
  66. $applicableUsers = $storageConfig->getApplicableUsers();
  67. if ($applicable !== 'all') {
  68. $applicableUsers[] = $applicable;
  69. $storageConfig->setApplicableUsers($applicableUsers);
  70. }
  71. } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) {
  72. $applicableGroups = $storageConfig->getApplicableGroups();
  73. $applicableGroups[] = $applicable;
  74. $storageConfig->setApplicableGroups($applicableGroups);
  75. }
  76. return $storageConfig;
  77. }
  78. /**
  79. * Read the external storages config
  80. *
  81. * @return array map of storage id to storage config
  82. */
  83. protected function readConfig() {
  84. $mountPoints = $this->readLegacyConfig();
  85. /**
  86. * Here is the how the horribly messy mount point array looks like
  87. * from the mount.json file:
  88. *
  89. * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath]
  90. *
  91. * - $mountType is either "user" or "group"
  92. * - $applicable is the name of a user or group (or the current user for personal mounts)
  93. * - $mountPath is the mount point path (where the storage must be mounted)
  94. * - $storageOptions is a map of storage options:
  95. * - "priority": storage priority
  96. * - "backend": backend class name
  97. * - "options": backend-specific options
  98. * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc)
  99. */
  100. // group by storage id
  101. $storages = [];
  102. // for storages without id (legacy), group by config hash for
  103. // later processing
  104. $storagesWithConfigHash = [];
  105. foreach ($mountPoints as $mountType => $applicables) {
  106. foreach ($applicables as $applicable => $mountPaths) {
  107. foreach ($mountPaths as $rootMountPath => $storageOptions) {
  108. $currentStorage = null;
  109. /**
  110. * Flag whether the config that was read already has an id.
  111. * If not, it will use a config hash instead and generate
  112. * a proper id later
  113. *
  114. * @var boolean
  115. */
  116. $hasId = false;
  117. // the root mount point is in the format "/$user/files/the/mount/point"
  118. // we remove the "/$user/files" prefix
  119. $parts = explode('/', trim($rootMountPath, '/'), 3);
  120. if (count($parts) < 3) {
  121. // something went wrong, skip
  122. \OCP\Util::writeLog(
  123. 'files_external',
  124. 'Could not parse mount point "' . $rootMountPath . '"',
  125. \OCP\Util::ERROR
  126. );
  127. continue;
  128. }
  129. $relativeMountPath = $parts[2];
  130. // note: we cannot do this after the loop because the decrypted config
  131. // options might be needed for the config hash
  132. $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']);
  133. if (isset($storageOptions['id'])) {
  134. $configId = (int)$storageOptions['id'];
  135. if (isset($storages[$configId])) {
  136. $currentStorage = $storages[$configId];
  137. }
  138. $hasId = true;
  139. } else {
  140. // missing id in legacy config, need to generate
  141. // but at this point we don't know the max-id, so use
  142. // first group it by config hash
  143. $storageOptions['mountpoint'] = $rootMountPath;
  144. $configId = \OC_Mount_Config::makeConfigHash($storageOptions);
  145. if (isset($storagesWithConfigHash[$configId])) {
  146. $currentStorage = $storagesWithConfigHash[$configId];
  147. }
  148. }
  149. if (is_null($currentStorage)) {
  150. // create new
  151. $currentStorage = new StorageConfig($configId);
  152. $currentStorage->setMountPoint($relativeMountPath);
  153. }
  154. $this->populateStorageConfigWithLegacyOptions(
  155. $currentStorage,
  156. $mountType,
  157. $applicable,
  158. $storageOptions
  159. );
  160. if ($hasId) {
  161. $storages[$configId] = $currentStorage;
  162. } else {
  163. $storagesWithConfigHash[$configId] = $currentStorage;
  164. }
  165. }
  166. }
  167. }
  168. // process storages with config hash, they must get a real id
  169. if (!empty($storagesWithConfigHash)) {
  170. $nextId = $this->generateNextId($storages);
  171. foreach ($storagesWithConfigHash as $storage) {
  172. $storage->setId($nextId);
  173. $storages[$nextId] = $storage;
  174. $nextId++;
  175. }
  176. // re-save the config with the generated ids
  177. $this->writeConfig($storages);
  178. }
  179. return $storages;
  180. }
  181. /**
  182. * Add mount point into the messy mount point structure
  183. *
  184. * @param array $mountPoints messy array of mount points
  185. * @param string $mountType mount type
  186. * @param string $applicable single applicable user or group
  187. * @param string $rootMountPoint root mount point to use
  188. * @param array $storageConfig storage config to set to the mount point
  189. */
  190. protected function addMountPoint(&$mountPoints, $mountType, $applicable, $rootMountPoint, $storageConfig) {
  191. if (!isset($mountPoints[$mountType])) {
  192. $mountPoints[$mountType] = [];
  193. }
  194. if (!isset($mountPoints[$mountType][$applicable])) {
  195. $mountPoints[$mountType][$applicable] = [];
  196. }
  197. $options = [
  198. 'id' => $storageConfig->getId(),
  199. 'class' => $storageConfig->getBackendClass(),
  200. 'options' => $storageConfig->getBackendOptions(),
  201. ];
  202. if (!is_null($storageConfig->getPriority())) {
  203. $options['priority'] = $storageConfig->getPriority();
  204. }
  205. $mountOptions = $storageConfig->getMountOptions();
  206. if (!empty($mountOptions)) {
  207. $options['mountOptions'] = $mountOptions;
  208. }
  209. $mountPoints[$mountType][$applicable][$rootMountPoint] = $options;
  210. }
  211. /**
  212. * Write the storages to the configuration.
  213. *
  214. * @param array $storages map of storage id to storage config
  215. */
  216. abstract protected function writeConfig($storages);
  217. /**
  218. * Get a storage with status
  219. *
  220. * @param int $id storage id
  221. *
  222. * @return StorageConfig
  223. * @throws NotFoundException if the storage with the given id was not found
  224. */
  225. public function getStorage($id) {
  226. $allStorages = $this->readConfig();
  227. if (!isset($allStorages[$id])) {
  228. throw new NotFoundException('Storage with id "' . $id . '" not found');
  229. }
  230. return $allStorages[$id];
  231. }
  232. /**
  233. * Gets all storages
  234. *
  235. * @return array array of storage configs
  236. */
  237. public function getAllStorages() {
  238. return $this->readConfig();
  239. }
  240. /**
  241. * Add new storage to the configuration
  242. *
  243. * @param array $newStorage storage attributes
  244. *
  245. * @return StorageConfig storage config, with added id
  246. */
  247. public function addStorage(StorageConfig $newStorage) {
  248. $allStorages = $this->readConfig();
  249. $configId = $this->generateNextId($allStorages);
  250. $newStorage->setId($configId);
  251. // add new storage
  252. $allStorages[$configId] = $newStorage;
  253. $this->writeConfig($allStorages);
  254. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  255. $newStorage->setStatus(\OC_Mount_Config::STATUS_SUCCESS);
  256. return $newStorage;
  257. }
  258. /**
  259. * Triggers the given hook signal for all the applicables given
  260. *
  261. * @param string $signal signal
  262. * @param string $mountPoint hook mount pount param
  263. * @param string $mountType hook mount type param
  264. * @param array $applicableArray array of applicable users/groups for which to trigger the hook
  265. */
  266. protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) {
  267. foreach ($applicableArray as $applicable) {
  268. \OC_Hook::emit(
  269. Filesystem::CLASSNAME,
  270. $signal,
  271. [
  272. Filesystem::signal_param_path => $mountPoint,
  273. Filesystem::signal_param_mount_type => $mountType,
  274. Filesystem::signal_param_users => $applicable,
  275. ]
  276. );
  277. }
  278. }
  279. /**
  280. * Triggers $signal for all applicable users of the given
  281. * storage
  282. *
  283. * @param StorageConfig $storage storage data
  284. * @param string $signal signal to trigger
  285. */
  286. abstract protected function triggerHooks(StorageConfig $storage, $signal);
  287. /**
  288. * Triggers signal_create_mount or signal_delete_mount to
  289. * accomodate for additions/deletions in applicableUsers
  290. * and applicableGroups fields.
  291. *
  292. * @param StorageConfig $oldStorage old storage data
  293. * @param StorageConfig $newStorage new storage data
  294. */
  295. abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage);
  296. /**
  297. * Update storage to the configuration
  298. *
  299. * @param StorageConfig $updatedStorage storage attributes
  300. *
  301. * @return StorageConfig storage config
  302. * @throws NotFoundException if the given storage does not exist in the config
  303. */
  304. public function updateStorage(StorageConfig $updatedStorage) {
  305. $allStorages = $this->readConfig();
  306. $id = $updatedStorage->getId();
  307. if (!isset($allStorages[$id])) {
  308. throw new NotFoundException('Storage with id "' . $id . '" not found');
  309. }
  310. $oldStorage = $allStorages[$id];
  311. $allStorages[$id] = $updatedStorage;
  312. $this->writeConfig($allStorages);
  313. $this->triggerChangeHooks($oldStorage, $updatedStorage);
  314. return $this->getStorage($id);
  315. }
  316. /**
  317. * Delete the storage with the given id.
  318. *
  319. * @param int $id storage id
  320. *
  321. * @throws NotFoundException if no storage was found with the given id
  322. */
  323. public function removeStorage($id) {
  324. $allStorages = $this->readConfig();
  325. if (!isset($allStorages[$id])) {
  326. throw new NotFoundException('Storage with id "' . $id . '" not found');
  327. }
  328. $deletedStorage = $allStorages[$id];
  329. unset($allStorages[$id]);
  330. $this->writeConfig($allStorages);
  331. $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount);
  332. }
  333. /**
  334. * Generates a configuration id to use for a new configuration entry.
  335. *
  336. * @param array $allStorages array of all storage configs
  337. *
  338. * @return int id
  339. */
  340. protected function generateNextId($allStorages) {
  341. if (empty($allStorages)) {
  342. return 1;
  343. }
  344. // note: this will mess up with with concurrency,
  345. // but so did the mount.json. This horribly hack
  346. // will disappear once we move to DB tables to
  347. // store the config
  348. return (max(array_keys($allStorages)) + 1);
  349. }
  350. }