1
0

apicontroller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Tobias Kaminsky <tobias@kaminsky.me>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Controller;
  29. use OCP\IRequest;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Http\DataDisplayResponse;
  32. use OCP\AppFramework\Http\DownloadResponse;
  33. use OCA\Files\Service\TagService;
  34. use OCP\IPreview;
  35. /**
  36. * Class ApiController
  37. *
  38. * @package OCA\Files\Controller
  39. */
  40. class ApiController extends Controller {
  41. /** @var TagService */
  42. private $tagService;
  43. /** @var IPreview */
  44. private $previewManager;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param TagService $tagService
  49. * @param IPreview $previewManager
  50. */
  51. public function __construct($appName,
  52. IRequest $request,
  53. TagService $tagService,
  54. IPreview $previewManager){
  55. parent::__construct($appName, $request);
  56. $this->tagService = $tagService;
  57. $this->previewManager = $previewManager;
  58. }
  59. /**
  60. * Gets a thumbnail of the specified file
  61. *
  62. * @since API version 1.0
  63. *
  64. * @NoAdminRequired
  65. * @NoCSRFRequired
  66. *
  67. * @param int $x
  68. * @param int $y
  69. * @param string $file URL-encoded filename
  70. * @return DataResponse|DataDisplayResponse
  71. */
  72. public function getThumbnail($x, $y, $file) {
  73. if($x < 1 || $y < 1) {
  74. return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
  75. }
  76. $preview = $this->previewManager->createPreview('files/'.urldecode($file), $x, $y, true);
  77. if ($preview->valid()) {
  78. return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  79. } else {
  80. return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
  81. }
  82. }
  83. /**
  84. * Updates the info of the specified file path
  85. * The passed tags are absolute, which means they will
  86. * replace the actual tag selection.
  87. *
  88. * @NoAdminRequired
  89. * @CORS
  90. *
  91. * @param string $path path
  92. * @param array|string $tags array of tags
  93. * @return DataResponse
  94. */
  95. public function updateFileTags($path, $tags = null) {
  96. $result = [];
  97. // if tags specified or empty array, update tags
  98. if (!is_null($tags)) {
  99. try {
  100. $this->tagService->updateFileTags($path, $tags);
  101. } catch (\OCP\Files\NotFoundException $e) {
  102. return new DataResponse([
  103. 'message' => $e->getMessage()
  104. ], Http::STATUS_NOT_FOUND);
  105. } catch (\OCP\Files\StorageNotAvailableException $e) {
  106. return new DataResponse([
  107. 'message' => $e->getMessage()
  108. ], Http::STATUS_SERVICE_UNAVAILABLE);
  109. } catch (\Exception $e) {
  110. return new DataResponse([
  111. 'message' => $e->getMessage()
  112. ], Http::STATUS_NOT_FOUND);
  113. }
  114. $result['tags'] = $tags;
  115. }
  116. return new DataResponse($result);
  117. }
  118. /**
  119. * Returns a list of all files tagged with the given tag.
  120. *
  121. * @NoAdminRequired
  122. * @CORS
  123. *
  124. * @param array|string $tagName tag name to filter by
  125. * @return DataResponse
  126. */
  127. public function getFilesByTag($tagName) {
  128. $files = array();
  129. $fileInfos = $this->tagService->getFilesByTag($tagName);
  130. foreach ($fileInfos as &$fileInfo) {
  131. $file = \OCA\Files\Helper::formatFileInfo($fileInfo);
  132. $parts = explode('/', dirname($fileInfo->getPath()), 4);
  133. if(isset($parts[3])) {
  134. $file['path'] = '/' . $parts[3];
  135. } else {
  136. $file['path'] = '/';
  137. }
  138. $file['tags'] = [$tagName];
  139. $files[] = $file;
  140. }
  141. return new DataResponse(['files' => $files]);
  142. }
  143. }