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.

ApiController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. ];
  205. $shareTypes = [];
  206. $nodeIds = array_map(function (Node $node) {
  207. return $node->getId();
  208. }, $nodes);
  209. foreach ($requestedShareTypes as $shareType) {
  210. $nodesLeft = array_combine($nodeIds, array_fill(0, count($nodeIds), true));
  211. $offset = 0;
  212. // fetch shares until we've either found shares for all nodes or there are no more shares left
  213. while (count($nodesLeft) > 0) {
  214. $shares = $this->shareManager->getSharesBy($userId, $shareType, null, false, 100, $offset);
  215. foreach ($shares as $share) {
  216. $fileId = $share->getNodeId();
  217. if (isset($nodesLeft[$fileId])) {
  218. if (!isset($shareTypes[$fileId])) {
  219. $shareTypes[$fileId] = [];
  220. }
  221. $shareTypes[$fileId][] = $shareType;
  222. unset($nodesLeft[$fileId]);
  223. }
  224. }
  225. if (count($shares) < 100) {
  226. break;
  227. } else {
  228. $offset += count($shares);
  229. }
  230. }
  231. }
  232. return $shareTypes;
  233. }
  234. /**
  235. * Returns a list of recently modified files.
  236. *
  237. * @NoAdminRequired
  238. *
  239. * @return DataResponse
  240. */
  241. public function getRecentFiles() {
  242. $nodes = $this->userFolder->getRecent(100);
  243. $files = $this->formatNodes($nodes);
  244. return new DataResponse(['files' => $files]);
  245. }
  246. /**
  247. * Change the default sort mode
  248. *
  249. * @NoAdminRequired
  250. *
  251. * @param string $mode
  252. * @param string $direction
  253. * @return Response
  254. * @throws \OCP\PreConditionNotMetException
  255. */
  256. public function updateFileSorting($mode, $direction) {
  257. $allowedMode = ['name', 'size', 'mtime'];
  258. $allowedDirection = ['asc', 'desc'];
  259. if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
  260. $response = new Response();
  261. $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  262. return $response;
  263. }
  264. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
  265. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
  266. return new Response();
  267. }
  268. /**
  269. * Toggle default for showing/hiding hidden files
  270. *
  271. * @NoAdminRequired
  272. *
  273. * @param bool $show
  274. * @return Response
  275. * @throws \OCP\PreConditionNotMetException
  276. */
  277. public function showHiddenFiles(bool $show): Response {
  278. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', $show ? '1' : '0');
  279. return new Response();
  280. }
  281. /**
  282. * Toggle default for cropping preview images
  283. *
  284. * @NoAdminRequired
  285. *
  286. * @param bool $crop
  287. * @return Response
  288. * @throws \OCP\PreConditionNotMetException
  289. */
  290. public function cropImagePreviews(bool $crop): Response {
  291. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', $crop ? '1' : '0');
  292. return new Response();
  293. }
  294. /**
  295. * Toggle default for files grid view
  296. *
  297. * @NoAdminRequired
  298. *
  299. * @param bool $show
  300. * @return Response
  301. * @throws \OCP\PreConditionNotMetException
  302. */
  303. public function showGridView(bool $show): Response {
  304. $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', $show ? '1' : '0');
  305. return new Response();
  306. }
  307. /**
  308. * Get default settings for the grid view
  309. *
  310. * @NoAdminRequired
  311. */
  312. public function getGridView() {
  313. $status = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', '0') === '1';
  314. return new JSONResponse(['gridview' => $status]);
  315. }
  316. /**
  317. * Toggle default for showing/hiding xxx folder
  318. *
  319. * @NoAdminRequired
  320. *
  321. * @param int $show
  322. * @param string $key the key of the folder
  323. *
  324. * @return Response
  325. * @throws \OCP\PreConditionNotMetException
  326. */
  327. public function toggleShowFolder(int $show, string $key): Response {
  328. if ($show !== 0 && $show !== 1) {
  329. return new DataResponse([
  330. 'message' => 'Invalid show value. Only 0 and 1 are allowed.'
  331. ], Http::STATUS_BAD_REQUEST);
  332. }
  333. $userId = $this->userSession->getUser()->getUID();
  334. // Set the new value and return it
  335. // Using a prefix prevents the user from setting arbitrary keys
  336. $this->config->setUserValue($userId, 'files', 'show_' . $key, (string)$show);
  337. return new JSONResponse([$key => $show]);
  338. }
  339. /**
  340. * Get sorting-order for custom sorting
  341. *
  342. * @NoAdminRequired
  343. *
  344. * @param string $folderpath
  345. * @return string
  346. * @throws \OCP\Files\NotFoundException
  347. */
  348. public function getNodeType($folderpath) {
  349. $node = $this->userFolder->get($folderpath);
  350. return $node->getType();
  351. }
  352. }