1
0

ApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Felix Nüsse <Felix.nuesse@t-online.de>
  8. * @author fnuesse <felix.nuesse@t-online.de>
  9. * @author fnuesse <fnuesse@techfak.uni-bielefeld.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author John Molakvoæ <skjnldsv@protonmail.com>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Max Kovalenko <mxss1998@yandex.ru>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Nina Pypchenko <22447785+nina-py@users.noreply.github.com>
  17. * @author Richard Steinmetz <richard@steinmetz.cloud>
  18. * @author Robin Appelman <robin@icewind.nl>
  19. * @author Roeland Jago Douma <roeland@famdouma.nl>
  20. * @author Tobias Kaminsky <tobias@kaminsky.me>
  21. * @author Vincent Petry <vincent@nextcloud.com>
  22. *
  23. * @license AGPL-3.0
  24. *
  25. * This code is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU Affero General Public License, version 3,
  27. * as published by the Free Software Foundation.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Affero General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Affero General Public License, version 3,
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>
  36. *
  37. */
  38. namespace OCA\Files\Controller;
  39. use OC\Files\Node\Node;
  40. use OCA\Files\Service\TagService;
  41. use OCP\AppFramework\Controller;
  42. use OCP\AppFramework\Http;
  43. use OCP\AppFramework\Http\DataResponse;
  44. use OCP\AppFramework\Http\FileDisplayResponse;
  45. use OCP\AppFramework\Http\JSONResponse;
  46. use OCP\AppFramework\Http\Response;
  47. use OCP\Files\File;
  48. use OCP\Files\Folder;
  49. use OCP\Files\NotFoundException;
  50. use OCP\IConfig;
  51. use OCP\IPreview;
  52. use OCP\IRequest;
  53. use OCP\IUserSession;
  54. use OCP\Share\IManager;
  55. use OCP\Share\IShare;
  56. /**
  57. * Class ApiController
  58. *
  59. * @package OCA\Files\Controller
  60. */
  61. class ApiController extends Controller {
  62. /** @var TagService */
  63. private $tagService;
  64. /** @var IManager * */
  65. private $shareManager;
  66. /** @var IPreview */
  67. private $previewManager;
  68. /** @var IUserSession */
  69. private $userSession;
  70. /** @var IConfig */
  71. private $config;
  72. /** @var Folder */
  73. private $userFolder;
  74. /**
  75. * @param string $appName
  76. * @param IRequest $request
  77. * @param IUserSession $userSession
  78. * @param TagService $tagService
  79. * @param IPreview $previewManager
  80. * @param IManager $shareManager
  81. * @param IConfig $config
  82. * @param Folder $userFolder
  83. */
  84. public function __construct($appName,
  85. IRequest $request,
  86. IUserSession $userSession,
  87. TagService $tagService,
  88. IPreview $previewManager,
  89. IManager $shareManager,
  90. IConfig $config,
  91. Folder $userFolder) {
  92. parent::__construct($appName, $request);
  93. $this->userSession = $userSession;
  94. $this->tagService = $tagService;
  95. $this->previewManager = $previewManager;
  96. $this->shareManager = $shareManager;
  97. $this->config = $config;
  98. $this->userFolder = $userFolder;
  99. }
  100. /**
  101. * Gets a thumbnail of the specified file
  102. *
  103. * @since API version 1.0
  104. *
  105. * @NoAdminRequired
  106. * @NoCSRFRequired
  107. * @StrictCookieRequired
  108. *
  109. * @param int $x
  110. * @param int $y
  111. * @param string $file URL-encoded filename
  112. * @return DataResponse|FileDisplayResponse
  113. */
  114. public function getThumbnail($x, $y, $file) {
  115. if ($x < 1 || $y < 1) {
  116. return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  117. }
  118. try {
  119. $file = $this->userFolder->get($file);
  120. if ($file instanceof Folder) {
  121. throw new NotFoundException();
  122. }
  123. /** @var File $file */
  124. $preview = $this->previewManager->getPreview($file, $x, $y, true);
  125. return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
  126. } catch (NotFoundException $e) {
  127. return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  128. } catch (\Exception $e) {
  129. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  130. }
  131. }
  132. /**
  133. * Updates the info of the specified file path
  134. * The passed tags are absolute, which means they will
  135. * replace the actual tag selection.
  136. *
  137. * @NoAdminRequired
  138. *
  139. * @param string $path path
  140. * @param array|string $tags array of tags
  141. * @return DataResponse
  142. */
  143. public function updateFileTags($path, $tags = null) {
  144. $result = [];
  145. // if tags specified or empty array, update tags
  146. if (!is_null($tags)) {
  147. try {
  148. $this->tagService->updateFileTags($path, $tags);
  149. } catch (\OCP\Files\NotFoundException $e) {
  150. return new DataResponse([
  151. 'message' => $e->getMessage()
  152. ], Http::STATUS_NOT_FOUND);
  153. } catch (\OCP\Files\StorageNotAvailableException $e) {
  154. return new DataResponse([
  155. 'message' => $e->getMessage()
  156. ], Http::STATUS_SERVICE_UNAVAILABLE);
  157. } catch (\Exception $e) {
  158. return new DataResponse([
  159. 'message' => $e->getMessage()
  160. ], Http::STATUS_NOT_FOUND);
  161. }
  162. $result['tags'] = $tags;
  163. }
  164. return new DataResponse($result);
  165. }
  166. /**
  167. * @param \OCP\Files\Node[] $nodes
  168. * @return array
  169. */
  170. private function formatNodes(array $nodes) {
  171. $shareTypesForNodes = $this->getShareTypesForNodes($nodes);
  172. return array_values(array_map(function (Node $node) use ($shareTypesForNodes) {
  173. $shareTypes = $shareTypesForNodes[$node->getId()] ?? [];
  174. $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo());
  175. $file['hasPreview'] = $this->previewManager->isAvailable($node);
  176. $parts = explode('/', dirname($node->getPath()), 4);
  177. if (isset($parts[3])) {
  178. $file['path'] = '/' . $parts[3];
  179. } else {
  180. $file['path'] = '/';
  181. }
  182. if (!empty($shareTypes)) {
  183. $file['shareTypes'] = $shareTypes;
  184. }
  185. return $file;
  186. }, $nodes));
  187. }
  188. /**
  189. * Get the share types for each node
  190. *
  191. * @param \OCP\Files\Node[] $nodes
  192. * @return array<int, int[]> list of share types for each fileid
  193. */
  194. private function getShareTypesForNodes(array $nodes): array {
  195. $userId = $this->userSession->getUser()->getUID();
  196. $requestedShareTypes = [
  197. IShare::TYPE_USER,
  198. IShare::TYPE_GROUP,
  199. IShare::TYPE_LINK,
  200. IShare::TYPE_REMOTE,
  201. IShare::TYPE_EMAIL,
  202. IShare::TYPE_ROOM,
  203. IShare::TYPE_DECK,
  204. IShare::TYPE_SCIENCEMESH,
  205. ];
  206. $shareTypes = [];
  207. $nodeIds = array_map(function (Node $node) {
  208. return $node->getId();
  209. }, $nodes);
  210. foreach ($requestedShareTypes as $shareType) {
  211. $nodesLeft = array_combine($nodeIds, array_fill(0, count($nodeIds), true));
  212. $offset = 0;
  213. // fetch shares until we've either found shares for all nodes or there are no more shares left
  214. while (count($nodesLeft) > 0) {
  215. $shares = $this->shareManager->getSharesBy($userId, $shareType, null, false, 100, $offset);
  216. foreach ($shares as $share) {
  217. $fileId = $share->getNodeId();
  218. if (isset($nodesLeft[$fileId])) {
  219. if (!isset($shareTypes[$fileId])) {
  220. $shareTypes[$fileId] = [];
  221. }
  222. $shareTypes[$fileId][] = $shareType;
  223. unset($nodesLeft[$fileId]);
  224. }
  225. }
  226. if (count($shares) < 100) {
  227. break;
  228. } else {
  229. $offset += count($shares);
  230. }
  231. }
  232. }
  233. return $shareTypes;
  234. }
  235. /**
  236. * Returns a list of recently modified files.
  237. *
  238. * @NoAdminRequired
  239. *
  240. * @return DataResponse
  241. */
  242. public function getRecentFiles() {
  243. $nodes = $this->userFolder->getRecent(100);
  244. $files = $this->formatNodes($nodes);
  245. return new DataResponse(['files' => $files]);
  246. }
  247. /**
  248. * Change the default sort mode
  249. *
  250. * @NoAdminRequired
  251. *
  252. * @param string $mode
  253. * @param string $direction
  254. * @return Response
  255. * @throws \OCP\PreConditionNotMetException
  256. */
  257. public function updateFileSorting($mode, $direction) {
  258. $allowedMode = ['name', 'size', 'mtime'];
  259. $allowedDirection = ['asc', 'desc'];
  260. if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
  261. $response = new Response();
  262. $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  263. return $response;
  264. }
  265. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
  266. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
  267. return new Response();
  268. }
  269. /**
  270. * Toggle default for showing/hiding hidden files
  271. *
  272. * @NoAdminRequired
  273. *
  274. * @param bool $show
  275. * @return Response
  276. * @throws \OCP\PreConditionNotMetException
  277. */
  278. public function showHiddenFiles(bool $show): Response {
  279. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', $show ? '1' : '0');
  280. return new Response();
  281. }
  282. /**
  283. * Toggle default for cropping preview images
  284. *
  285. * @NoAdminRequired
  286. *
  287. * @param bool $crop
  288. * @return Response
  289. * @throws \OCP\PreConditionNotMetException
  290. */
  291. public function cropImagePreviews(bool $crop): Response {
  292. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', $crop ? '1' : '0');
  293. return new Response();
  294. }
  295. /**
  296. * Toggle default for files grid view
  297. *
  298. * @NoAdminRequired
  299. *
  300. * @param bool $show
  301. * @return Response
  302. * @throws \OCP\PreConditionNotMetException
  303. */
  304. public function showGridView(bool $show): Response {
  305. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', $show ? '1' : '0');
  306. return new Response();
  307. }
  308. /**
  309. * Get default settings for the grid view
  310. *
  311. * @NoAdminRequired
  312. */
  313. public function getGridView() {
  314. $status = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', '0') === '1';
  315. return new JSONResponse(['gridview' => $status]);
  316. }
  317. /**
  318. * Toggle default for showing/hiding xxx folder
  319. *
  320. * @NoAdminRequired
  321. *
  322. * @param int $show
  323. * @param string $key the key of the folder
  324. *
  325. * @return Response
  326. * @throws \OCP\PreConditionNotMetException
  327. */
  328. public function toggleShowFolder(int $show, string $key): Response {
  329. // ensure the edited key exists
  330. $navItems = \OCA\Files\App::getNavigationManager()->getAll();
  331. foreach ($navItems as $item) {
  332. // check if data is valid
  333. if (($show === 0 || $show === 1) && isset($item['expandedState']) && $key === $item['expandedState']) {
  334. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', $key, (string)$show);
  335. return new Response();
  336. }
  337. }
  338. $response = new Response();
  339. $response->setStatus(Http::STATUS_FORBIDDEN);
  340. return $response;
  341. }
  342. /**
  343. * Get sorting-order for custom sorting
  344. *
  345. * @NoAdminRequired
  346. *
  347. * @param string $folderpath
  348. * @return string
  349. * @throws \OCP\Files\NotFoundException
  350. */
  351. public function getNodeType($folderpath) {
  352. $node = $this->userFolder->get($folderpath);
  353. return $node->getType();
  354. }
  355. }