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 9.4KB

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