ApiController.php 9.0 KB

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