1
0

ApiController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Tobias Kaminsky <tobias@kaminsky.me>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Controller;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\NotFoundException;
  35. use OCP\IConfig;
  36. use OCP\IRequest;
  37. use OCP\AppFramework\Http\DataResponse;
  38. use OCP\AppFramework\Http\FileDisplayResponse;
  39. use OCP\AppFramework\Http\Response;
  40. use OCA\Files\Service\TagService;
  41. use OCP\IPreview;
  42. use OCP\Share\IManager;
  43. use OC\Files\Node\Node;
  44. use OCP\IUserSession;
  45. /**
  46. * Class ApiController
  47. *
  48. * @package OCA\Files\Controller
  49. */
  50. class ApiController extends Controller {
  51. /** @var TagService */
  52. private $tagService;
  53. /** @var IManager **/
  54. private $shareManager;
  55. /** @var IPreview */
  56. private $previewManager;
  57. /** IUserSession */
  58. private $userSession;
  59. /** IConfig */
  60. private $config;
  61. /** @var Folder */
  62. private $userFolder;
  63. /**
  64. * @param string $appName
  65. * @param IRequest $request
  66. * @param IUserSession $userSession
  67. * @param TagService $tagService
  68. * @param IPreview $previewManager
  69. * @param IManager $shareManager
  70. * @param IConfig $config
  71. * @param Folder $userFolder
  72. */
  73. public function __construct($appName,
  74. IRequest $request,
  75. IUserSession $userSession,
  76. TagService $tagService,
  77. IPreview $previewManager,
  78. IManager $shareManager,
  79. IConfig $config,
  80. Folder $userFolder) {
  81. parent::__construct($appName, $request);
  82. $this->userSession = $userSession;
  83. $this->tagService = $tagService;
  84. $this->previewManager = $previewManager;
  85. $this->shareManager = $shareManager;
  86. $this->config = $config;
  87. $this->userFolder = $userFolder;
  88. }
  89. /**
  90. * Gets a thumbnail of the specified file
  91. *
  92. * @since API version 1.0
  93. *
  94. * @NoAdminRequired
  95. * @NoCSRFRequired
  96. * @StrictCookieRequired
  97. *
  98. * @param int $x
  99. * @param int $y
  100. * @param string $file URL-encoded filename
  101. * @return DataResponse|FileDisplayResponse
  102. */
  103. public function getThumbnail($x, $y, $file) {
  104. if($x < 1 || $y < 1) {
  105. return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  106. }
  107. try {
  108. $file = $this->userFolder->get($file);
  109. if ($file instanceof Folder) {
  110. throw new NotFoundException();
  111. }
  112. /** @var File $file */
  113. $preview = $this->previewManager->getPreview($file, $x, $y, true);
  114. return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
  115. } catch (NotFoundException $e) {
  116. return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  117. } catch (\Exception $e) {
  118. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  119. }
  120. }
  121. /**
  122. * Updates the info of the specified file path
  123. * The passed tags are absolute, which means they will
  124. * replace the actual tag selection.
  125. *
  126. * @NoAdminRequired
  127. *
  128. * @param string $path path
  129. * @param array|string $tags array of tags
  130. * @return DataResponse
  131. */
  132. public function updateFileTags($path, $tags = null) {
  133. $result = [];
  134. // if tags specified or empty array, update tags
  135. if (!is_null($tags)) {
  136. try {
  137. $this->tagService->updateFileTags($path, $tags);
  138. } catch (\OCP\Files\NotFoundException $e) {
  139. return new DataResponse([
  140. 'message' => $e->getMessage()
  141. ], Http::STATUS_NOT_FOUND);
  142. } catch (\OCP\Files\StorageNotAvailableException $e) {
  143. return new DataResponse([
  144. 'message' => $e->getMessage()
  145. ], Http::STATUS_SERVICE_UNAVAILABLE);
  146. } catch (\Exception $e) {
  147. return new DataResponse([
  148. 'message' => $e->getMessage()
  149. ], Http::STATUS_NOT_FOUND);
  150. }
  151. $result['tags'] = $tags;
  152. }
  153. return new DataResponse($result);
  154. }
  155. /**
  156. * @param \OCP\Files\Node[] $nodes
  157. * @return array
  158. */
  159. private function formatNodes(array $nodes) {
  160. return array_values(array_map(function (Node $node) {
  161. /** @var \OC\Files\Node\Node $shareTypes */
  162. $shareTypes = $this->getShareTypes($node);
  163. $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo());
  164. $parts = explode('/', dirname($node->getPath()), 4);
  165. if (isset($parts[3])) {
  166. $file['path'] = '/' . $parts[3];
  167. } else {
  168. $file['path'] = '/';
  169. }
  170. if (!empty($shareTypes)) {
  171. $file['shareTypes'] = $shareTypes;
  172. }
  173. return $file;
  174. }, $nodes));
  175. }
  176. /**
  177. * Returns a list of recently modifed files.
  178. *
  179. * @NoAdminRequired
  180. *
  181. * @return DataResponse
  182. */
  183. public function getRecentFiles() {
  184. $nodes = $this->userFolder->getRecent(100);
  185. $files = $this->formatNodes($nodes);
  186. return new DataResponse(['files' => $files]);
  187. }
  188. /**
  189. * Return a list of share types for outgoing shares
  190. *
  191. * @param Node $node file node
  192. *
  193. * @return int[] array of share types
  194. */
  195. private function getShareTypes(Node $node) {
  196. $userId = $this->userSession->getUser()->getUID();
  197. $shareTypes = [];
  198. $requestedShareTypes = [
  199. \OCP\Share::SHARE_TYPE_USER,
  200. \OCP\Share::SHARE_TYPE_GROUP,
  201. \OCP\Share::SHARE_TYPE_LINK,
  202. \OCP\Share::SHARE_TYPE_REMOTE,
  203. \OCP\Share::SHARE_TYPE_EMAIL
  204. ];
  205. foreach ($requestedShareTypes as $requestedShareType) {
  206. // one of each type is enough to find out about the types
  207. $shares = $this->shareManager->getSharesBy(
  208. $userId,
  209. $requestedShareType,
  210. $node,
  211. false,
  212. 1
  213. );
  214. if (!empty($shares)) {
  215. $shareTypes[] = $requestedShareType;
  216. }
  217. }
  218. return $shareTypes;
  219. }
  220. /**
  221. * Change the default sort mode
  222. *
  223. * @NoAdminRequired
  224. *
  225. * @param string $mode
  226. * @param string $direction
  227. * @return Response
  228. */
  229. public function updateFileSorting($mode, $direction) {
  230. $allowedMode = ['name', 'size', 'mtime'];
  231. $allowedDirection = ['asc', 'desc'];
  232. if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
  233. $response = new Response();
  234. $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  235. return $response;
  236. }
  237. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
  238. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
  239. return new Response();
  240. }
  241. /**
  242. * Toggle default for showing/hiding hidden files
  243. *
  244. * @NoAdminRequired
  245. *
  246. * @param bool $show
  247. */
  248. public function showHiddenFiles($show) {
  249. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int) $show);
  250. return new Response();
  251. }
  252. }