storagescontroller.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_External\Controller;
  22. use \OCP\IConfig;
  23. use \OCP\IUserSession;
  24. use \OCP\IRequest;
  25. use \OCP\IL10N;
  26. use \OCP\AppFramework\Http\DataResponse;
  27. use \OCP\AppFramework\Controller;
  28. use \OCP\AppFramework\Http;
  29. use \OCA\Files_external\Service\StoragesService;
  30. use \OCA\Files_external\NotFoundException;
  31. use \OCA\Files_external\Lib\StorageConfig;
  32. /**
  33. * Base class for storages controllers
  34. */
  35. abstract class StoragesController extends Controller {
  36. /**
  37. * L10N service
  38. *
  39. * @var IL10N
  40. */
  41. protected $l10n;
  42. /**
  43. * Storages service
  44. *
  45. * @var StoragesService
  46. */
  47. protected $service;
  48. /**
  49. * Creates a new storages controller.
  50. *
  51. * @param string $AppName application name
  52. * @param IRequest $request request object
  53. * @param IL10N $l10n l10n service
  54. * @param StoragesService $storagesService storage service
  55. */
  56. public function __construct(
  57. $AppName,
  58. IRequest $request,
  59. IL10N $l10n,
  60. StoragesService $storagesService
  61. ) {
  62. parent::__construct($AppName, $request);
  63. $this->l10n = $l10n;
  64. $this->service = $storagesService;
  65. }
  66. /**
  67. * Validate storage config
  68. *
  69. * @param StorageConfig $storage storage config
  70. *
  71. * @return DataResponse|null returns response in case of validation error
  72. */
  73. protected function validate(StorageConfig $storage) {
  74. $mountPoint = $storage->getMountPoint();
  75. if ($mountPoint === '' || $mountPoint === '/') {
  76. return new DataResponse(
  77. array(
  78. 'message' => (string)$this->l10n->t('Invalid mount point')
  79. ),
  80. Http::STATUS_UNPROCESSABLE_ENTITY
  81. );
  82. }
  83. // TODO: validate that other attrs are set
  84. $backends = \OC_Mount_Config::getBackends();
  85. if (!isset($backends[$storage->getBackendClass()])) {
  86. // invalid backend
  87. return new DataResponse(
  88. array(
  89. 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', array($storage->getBackendClass()))
  90. ),
  91. Http::STATUS_UNPROCESSABLE_ENTITY
  92. );
  93. }
  94. return null;
  95. }
  96. /**
  97. * Check whether the given storage is available / valid.
  98. *
  99. * Note that this operation can be time consuming depending
  100. * on whether the remote storage is available or not.
  101. *
  102. * @param StorageConfig $storage storage configuration
  103. */
  104. protected function updateStorageStatus(StorageConfig &$storage) {
  105. // update status (can be time-consuming)
  106. $storage->setStatus(
  107. \OC_Mount_Config::getBackendStatus(
  108. $storage->getBackendClass(),
  109. $storage->getBackendOptions(),
  110. false
  111. )
  112. );
  113. }
  114. /**
  115. * Get an external storage entry.
  116. *
  117. * @param int $id storage id
  118. *
  119. * @return DataResponse
  120. */
  121. public function show($id) {
  122. try {
  123. $storage = $this->service->getStorage($id);
  124. $this->updateStorageStatus($storage);
  125. } catch (NotFoundException $e) {
  126. return new DataResponse(
  127. [
  128. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  129. ],
  130. Http::STATUS_NOT_FOUND
  131. );
  132. }
  133. return new DataResponse(
  134. $storage,
  135. Http::STATUS_OK
  136. );
  137. }
  138. /**
  139. * Deletes the storage with the given id.
  140. *
  141. * @param int $id storage id
  142. *
  143. * @return DataResponse
  144. */
  145. public function destroy($id) {
  146. try {
  147. $this->service->removeStorage($id);
  148. } catch (NotFoundException $e) {
  149. return new DataResponse(
  150. [
  151. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  152. ],
  153. Http::STATUS_NOT_FOUND
  154. );
  155. }
  156. return new DataResponse([], Http::STATUS_NO_CONTENT);
  157. }
  158. }