apicontroller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <rullzer@owncloud.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Tobias Kaminsky <tobias@kaminsky.me>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Controller;
  30. use OCP\IRequest;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\Http\DataDisplayResponse;
  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/'.$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. *
  90. * @param string $path path
  91. * @param array|string $tags array of tags
  92. * @return DataResponse
  93. */
  94. public function updateFileTags($path, $tags = null) {
  95. $result = [];
  96. // if tags specified or empty array, update tags
  97. if (!is_null($tags)) {
  98. try {
  99. $this->tagService->updateFileTags($path, $tags);
  100. } catch (\OCP\Files\NotFoundException $e) {
  101. return new DataResponse([
  102. 'message' => $e->getMessage()
  103. ], Http::STATUS_NOT_FOUND);
  104. } catch (\OCP\Files\StorageNotAvailableException $e) {
  105. return new DataResponse([
  106. 'message' => $e->getMessage()
  107. ], Http::STATUS_SERVICE_UNAVAILABLE);
  108. } catch (\Exception $e) {
  109. return new DataResponse([
  110. 'message' => $e->getMessage()
  111. ], Http::STATUS_NOT_FOUND);
  112. }
  113. $result['tags'] = $tags;
  114. }
  115. return new DataResponse($result);
  116. }
  117. /**
  118. * Returns a list of all files tagged with the given tag.
  119. *
  120. * @NoAdminRequired
  121. *
  122. * @param string $tagName tag name to filter by
  123. * @return DataResponse
  124. */
  125. public function getFilesByTag($tagName) {
  126. $files = array();
  127. $fileInfos = $this->tagService->getFilesByTag($tagName);
  128. foreach ($fileInfos as &$fileInfo) {
  129. $file = \OCA\Files\Helper::formatFileInfo($fileInfo);
  130. $parts = explode('/', dirname($fileInfo->getPath()), 4);
  131. if(isset($parts[3])) {
  132. $file['path'] = '/' . $parts[3];
  133. } else {
  134. $file['path'] = '/';
  135. }
  136. $file['tags'] = [$tagName];
  137. $files[] = $file;
  138. }
  139. return new DataResponse(['files' => $files]);
  140. }
  141. }