diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-19 02:52:12 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-19 02:52:13 +0300 |
commit | 7d393650e7e231a68038ccb9fd069cd95746806b (patch) | |
tree | 1ff0b8eab9bc03c84d995269b00afd40d928e0d8 /controller | |
parent | 8a002c4633c896c531d60d4042fe5276172bb157 (diff) |
Add download API
Diffstat (limited to 'controller')
-rw-r--r-- | controller/files.php | 49 | ||||
-rw-r--r-- | controller/filesapicontroller.php | 44 | ||||
-rw-r--r-- | controller/filescontroller.php | 42 | ||||
-rw-r--r-- | controller/filespubliccontroller.php | 14 | ||||
-rw-r--r-- | controller/pagecontroller.php | 45 | ||||
-rw-r--r-- | controller/preview.php | 54 | ||||
-rw-r--r-- | controller/previewapicontroller.php | 20 | ||||
-rw-r--r-- | controller/previewcontroller.php | 21 | ||||
-rw-r--r-- | controller/previewpubliccontroller.php | 5 |
9 files changed, 178 insertions, 116 deletions
diff --git a/controller/files.php b/controller/files.php index 30615c0e..55b5b760 100644 --- a/controller/files.php +++ b/controller/files.php @@ -14,6 +14,7 @@ namespace OCA\Gallery\Controller; +use OCP\Files\File; use OCP\Files\Folder; use OCP\ILogger; @@ -22,6 +23,8 @@ use OCP\AppFramework\Http; use OCA\Gallery\Service\SearchFolderService; use OCA\Gallery\Service\ConfigService; use OCA\Gallery\Service\SearchMediaService; +use OCA\Gallery\Service\DownloadService; +use OCA\Gallery\Service\ServiceException; /** * Trait Files @@ -45,6 +48,10 @@ trait Files { */ private $searchMediaService; /** + * @var DownloadService + */ + private $downloadService; + /** * @var ILogger */ private $logger; @@ -127,4 +134,46 @@ trait Files { ]; } + /** + * Generates the download data + * + * @param int $fileId the ID of the file of which we need a large preview of + * @param string|null $filename + * + * @return array|false + */ + private function getDownload($fileId, $filename) { + $download = false; + $file = $this->getFile($fileId); + if ($file) { + $download = $this->downloadService->downloadFile($file); + } + if ($download) { + if (is_null($filename)) { + $filename = $file->getName(); + } + $download['name'] = $filename; + } + + return $download; + } + + /** + * Retrieves the file based on the given ID + * + * @param int $fileId + * + * @return File + */ + private function getFile($fileId) { + try { + /** @type File $file */ + $file = $this->downloadService->getResourceFromId($fileId); + } catch (ServiceException $exception) { + $file = false; + } + + return $file; + } + } diff --git a/controller/filesapicontroller.php b/controller/filesapicontroller.php index 283a4e65..8c0be4c1 100644 --- a/controller/filesapicontroller.php +++ b/controller/filesapicontroller.php @@ -13,13 +13,18 @@ namespace OCA\Gallery\Controller; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\ILogger; use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; +use OCA\Gallery\Http\ImageResponse; use OCA\Gallery\Service\SearchFolderService; use OCA\Gallery\Service\ConfigService; use OCA\Gallery\Service\SearchMediaService; +use OCA\Gallery\Service\DownloadService; /** * Class FilesApiController @@ -31,29 +36,38 @@ class FilesApiController extends ApiController { use Files; use JsonHttpError; + /** @var IURLGenerator */ + private $urlGenerator; + /** * Constructor * * @param string $appName * @param IRequest $request + * @param IURLGenerator $urlGenerator * @param SearchFolderService $searchFolderService * @param ConfigService $configService * @param SearchMediaService $searchMediaService + * @param DownloadService $downloadService * @param ILogger $logger */ public function __construct( $appName, IRequest $request, + IURLGenerator $urlGenerator, SearchFolderService $searchFolderService, ConfigService $configService, SearchMediaService $searchMediaService, + DownloadService $downloadService, ILogger $logger ) { parent::__construct($appName, $request); + $this->urlGenerator = $urlGenerator; $this->searchFolderService = $searchFolderService; $this->configService = $configService; $this->searchMediaService = $searchMediaService; + $this->downloadService = $downloadService; $this->logger = $logger; } @@ -83,4 +97,34 @@ class FilesApiController extends ApiController { } } + /** + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * + * Sends the file matching the fileId + * + * @param int $fileId the ID of the file we want to download + * @param string|null $filename + * + * @return ImageResponse|RedirectResponse + */ + public function download($fileId, $filename = null) { + $download = $this->getDownload($fileId, $filename); + + if (!$download) { + $url = $this->urlGenerator->linkToRoute( + $this->appName . '.page.error_page', + [ + 'message' => 'There was a problem accessing the file', + 'code' => Http::STATUS_NOT_FOUND + ] + ); + + return new RedirectResponse($url); + } + + return new ImageResponse($download); + } + } diff --git a/controller/filescontroller.php b/controller/filescontroller.php index 4bd388eb..a925e8f6 100644 --- a/controller/filescontroller.php +++ b/controller/filescontroller.php @@ -13,13 +13,18 @@ namespace OCA\Gallery\Controller; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\ILogger; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; +use OCA\Gallery\Http\ImageResponse; use OCA\Gallery\Service\SearchFolderService; use OCA\Gallery\Service\ConfigService; use OCA\Gallery\Service\SearchMediaService; +use OCA\Gallery\Service\DownloadService; /** * Class FilesController @@ -31,29 +36,38 @@ class FilesController extends Controller { use Files; use JsonHttpError; + /** @var IURLGenerator */ + private $urlGenerator; + /** * Constructor * * @param string $appName * @param IRequest $request + * @param IURLGenerator $urlGenerator * @param SearchFolderService $searchFolderService * @param ConfigService $configService * @param SearchMediaService $searchMediaService + * @param DownloadService $downloadService * @param ILogger $logger */ public function __construct( $appName, IRequest $request, + IURLGenerator $urlGenerator, SearchFolderService $searchFolderService, ConfigService $configService, SearchMediaService $searchMediaService, + DownloadService $downloadService, ILogger $logger ) { parent::__construct($appName, $request); + $this->urlGenerator = $urlGenerator; $this->searchFolderService = $searchFolderService; $this->configService = $configService; $this->searchMediaService = $searchMediaService; + $this->downloadService = $downloadService; $this->logger = $logger; } @@ -86,4 +100,32 @@ class FilesController extends Controller { } } + /** + * @NoAdminRequired + * + * Sends the file matching the fileId + * + * @param int $fileId the ID of the file we want to download + * @param string|null $filename + * + * @return ImageResponse|RedirectResponse + */ + public function download($fileId, $filename = null) { + $download = $this->getDownload($fileId, $filename); + + if (!$download) { + $url = $this->urlGenerator->linkToRoute( + $this->appName . '.page.error_page', + [ + 'message' => 'There was a problem accessing the file', + 'code' => Http::STATUS_NOT_FOUND + ] + ); + + return new RedirectResponse($url); + } + + return new ImageResponse($download); + } + } diff --git a/controller/filespubliccontroller.php b/controller/filespubliccontroller.php index e894c7b6..90c06a1a 100644 --- a/controller/filespubliccontroller.php +++ b/controller/filespubliccontroller.php @@ -38,4 +38,18 @@ class FilesPublicController extends FilesController { return parent::getList($location, $features, $etag, $mediatypes); } + /** + * @PublicPage + * @NoCSRFRequired + * + * Sends the file matching the fileId + * + * @inheritDoc + * + * @param int $fileId the ID of the file we want to download + * @param string|null $filename + */ + public function download($fileId, $filename = null) { + return parent::download($fileId, $filename); + } } diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index e90d7d68..d3c9f146 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -17,7 +17,6 @@ namespace OCA\Gallery\Controller; use OCP\IURLGenerator; use OCP\IRequest; use OCP\IConfig; -use OCP\Files\File; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -26,7 +25,6 @@ use OCP\AppFramework\Http\RedirectResponse; use OCA\Gallery\Environment\Environment; use OCA\Gallery\Http\ImageResponse; -use OCA\Gallery\Service\ServiceException; use OCA\Gallery\Service\DownloadService; /** @@ -111,7 +109,7 @@ class PageController extends Controller { * @PublicPage * @NoCSRFRequired * - * Shows the albums and pictures or download the single file the token gives access to + * Shows the albums and pictures or redirects to the download location the token gives access to * * @param string $token * @param null|string $filename @@ -123,7 +121,16 @@ class PageController extends Controller { if ($node->getType() === 'dir') { return $this->showPublicPage($token); } else { - return $this->downloadFile($node, $filename); + $url = $this->urlGenerator->linkToRoute( + $this->appName . '.files_public.download', + [ + 'token' => $token, + 'fileId' => $node->getId(), + 'filename' => $filename + ] + ); + + return new RedirectResponse($url); } } @@ -209,36 +216,6 @@ class PageController extends Controller { } /** - * Downloads the file associated with a token - * - * @param File $file - * @param string|null $filename - * - * @return ImageResponse|RedirectResponse - */ - private function downloadFile($file, $filename) { - try { - $download = $this->downloadService->downloadFile($file); - if (is_null($filename)) { - $filename = $file->getName(); - } - $download['name'] = $filename; - - return new ImageResponse($download); - } catch (ServiceException $exception) { - $url = $this->urlGenerator->linkToRoute( - $this->appName . '.page.error_page', - [ - 'message' => $exception->getMessage(), - 'code' => Http::STATUS_NOT_FOUND - ] - ); - - return new RedirectResponse($url); - } - } - - /** * Determines if we can add external shared to this instance * * @return array diff --git a/controller/preview.php b/controller/preview.php index e8345897..ebae0068 100644 --- a/controller/preview.php +++ b/controller/preview.php @@ -115,9 +115,8 @@ trait Preview { /** @type File $file */ $file = $this->previewService->getResourceFromId($fileId); if (!is_null($file)) { - $previewRequired = $this->isPreviewRequired($file, $animatedPreview); $data = $this->getPreviewData( - $file, $previewRequired, $width, $height, $keepAspect, $base64Encode + $file, $animatedPreview, $width, $height, $keepAspect, $base64Encode ); } else { // Uncaught problem, should never reach this point... @@ -133,39 +132,20 @@ trait Preview { } /** - * Returns true if we need to generate a preview for that file - * - * @param $file + * @param File $file * @param bool $animatedPreview - * - * @return bool - */ - private function isPreviewRequired($file, $animatedPreview) { - $previewRequired = false; - - if (!$this->download) { - $previewRequired = - $this->previewService->isPreviewRequired($file, $animatedPreview); - } - - return $previewRequired; - } - - /** - * @param $file - * @param $previewRequired - * @param $width - * @param $height - * @param $keepAspect - * @param $base64Encode + * @param int $width + * @param int $height + * @param bool $keepAspect + * @param bool $base64Encode * * @return array */ private function getPreviewData( - $file, $previewRequired, $width, $height, $keepAspect, $base64Encode + $file, $animatedPreview, $width, $height, $keepAspect, $base64Encode ) { $status = Http::STATUS_OK; - if ($previewRequired) { + if ($this->previewService->isPreviewRequired($file, $animatedPreview)) { $type = 'preview'; $preview = $this->previewService->createPreview( $file, $width, $height, $keepAspect, $base64Encode @@ -211,22 +191,4 @@ trait Preview { return $this->getErrorData($status); } - /** - * Returns an URL based on the HTTP status code - * - * @param string $appName - * @param int $status - * - * @return string - */ - private function getErrorUrl($appName, $status) { - return $this->urlGenerator->linkToRoute( - $appName . '.page.error_page', - [ - 'message' => 'There was a problem accessing the file', - 'code' => $status - ] - ); - } - } diff --git a/controller/previewapicontroller.php b/controller/previewapicontroller.php index d59548b7..dfa2b5b3 100644 --- a/controller/previewapicontroller.php +++ b/controller/previewapicontroller.php @@ -19,7 +19,6 @@ use OCP\Files\File; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; -use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\JSONResponse; use OCA\Gallery\Http\ImageResponse; @@ -116,26 +115,15 @@ class PreviewApiController extends ApiController { * @param int $fileId the ID of the file of which we need a large preview of * @param int $width * @param int $height - * @param string|null $download * - * @return ImageResponse|RedirectResponse|Http\JSONResponse + * @return ImageResponse|Http\JSONResponse */ - public function getPreview($fileId, $width, $height, $download) { - if (!is_null($download)) { - $this->download = true; - } + public function getPreview($fileId, $width, $height) { /** @type File $file */ list($file, $preview, $status) = $this->getData($fileId, $width, $height); - if ($preview === null) { - if ($this->download) { - $url = $this->getErrorUrl($this->appName, $status); - - return new RedirectResponse($url); - } else { - - return new JSONResponse(['message' => 'Oh Nooooes!', 'success' => false], $status); - } + if (!$preview) { + return new JSONResponse(['message' => 'Oh Nooooes!', 'success' => false], $status); } $preview['name'] = $file->getName(); diff --git a/controller/previewcontroller.php b/controller/previewcontroller.php index a25519b9..2bc695e6 100644 --- a/controller/previewcontroller.php +++ b/controller/previewcontroller.php @@ -14,13 +14,11 @@ namespace OCA\Gallery\Controller; use OCP\IRequest; use OCP\IURLGenerator; -use OCP\IEventSource; use OCP\ILogger; use OCP\Files\File; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; -use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\JSONResponse; use OCA\Gallery\Http\ImageResponse; @@ -120,26 +118,15 @@ class PreviewController extends Controller { * @param int $fileId the ID of the file of which we need a large preview of * @param int $width * @param int $height - * @param string|null $download * - * @return ImageResponse|RedirectResponse|Http\JSONResponse + * @return ImageResponse|Http\JSONResponse */ - public function getPreview($fileId, $width, $height, $download) { - if (!is_null($download)) { - $this->download = true; - } + public function getPreview($fileId, $width, $height) { /** @type File $file */ list($file, $preview, $status) = $this->getData($fileId, $width, $height); - if ($preview === null) { - if ($this->download) { - $url = $this->getErrorUrl($this->appName, $status); - - return new RedirectResponse($url); - } else { - - return new JSONResponse(['message' => 'Oh Nooooes!', 'success' => false], $status); - } + if (!$preview) { + return new JSONResponse(['message' => 'Oh Nooooes!', 'success' => false], $status); } $preview['name'] = $file->getName(); diff --git a/controller/previewpubliccontroller.php b/controller/previewpubliccontroller.php index 0570e746..bacaf266 100644 --- a/controller/previewpubliccontroller.php +++ b/controller/previewpubliccontroller.php @@ -55,10 +55,9 @@ class PreviewPublicController extends PreviewController { * @param int $fileId the ID of the file of which we need a large preview of * @param int $width * @param int $height - * @param string|null $download */ - public function getPreview($fileId, $width, $height, $download) { - return parent::getPreview($fileId, $width, $height, $download); + public function getPreview($fileId, $width, $height) { + return parent::getPreview($fileId, $width, $height); } } |