nextcloud /
gallery
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Nextcloud - Gallery |
||||||
| 4 | * |
||||||
| 5 | * This file is licensed under the Affero General Public License version 3 or |
||||||
| 6 | * later. See the COPYING file. |
||||||
| 7 | * |
||||||
| 8 | * @author Olivier Paroz <[email protected]> |
||||||
| 9 | * |
||||||
| 10 | * @copyright Olivier Paroz 2017 |
||||||
| 11 | */ |
||||||
| 12 | |||||||
| 13 | namespace OCA\Gallery\Controller; |
||||||
| 14 | |||||||
| 15 | use OCP\AppFramework\Http\DataResponse; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 16 | use OCP\IRequest; |
||||||
|
0 ignored issues
–
show
The type
OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 17 | use OCP\IURLGenerator; |
||||||
|
0 ignored issues
–
show
The type
OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 18 | use OCP\ILogger; |
||||||
|
0 ignored issues
–
show
The type
OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 19 | use OCP\Files\File; |
||||||
|
0 ignored issues
–
show
The type
OCP\Files\File was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 20 | |||||||
| 21 | use OCP\AppFramework\Controller; |
||||||
|
0 ignored issues
–
show
The type
OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 22 | use OCP\AppFramework\Http; |
||||||
|
0 ignored issues
–
show
The type
OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 23 | use OCP\AppFramework\Http\JSONResponse; |
||||||
|
0 ignored issues
–
show
The type
OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 24 | |||||||
| 25 | use OCA\Gallery\Http\ImageResponse; |
||||||
| 26 | use OCA\Gallery\Service\ConfigService; |
||||||
| 27 | use OCA\Gallery\Service\ThumbnailService; |
||||||
| 28 | use OCA\Gallery\Service\PreviewService; |
||||||
| 29 | use OCA\Gallery\Service\DownloadService; |
||||||
| 30 | use OCA\Gallery\Utility\EventSource; |
||||||
| 31 | |||||||
| 32 | /** |
||||||
| 33 | * Class PreviewController |
||||||
| 34 | * |
||||||
| 35 | * @package OCA\Gallery\Controller |
||||||
| 36 | */ |
||||||
| 37 | class PreviewController extends Controller { |
||||||
| 38 | |||||||
| 39 | use Preview; |
||||||
| 40 | |||||||
| 41 | /** @var EventSource */ |
||||||
| 42 | private $eventSource; |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * Constructor |
||||||
| 46 | * |
||||||
| 47 | * @param string $appName |
||||||
| 48 | * @param IRequest $request |
||||||
| 49 | * @param IURLGenerator $urlGenerator |
||||||
| 50 | * @param ConfigService $configService |
||||||
| 51 | * @param ThumbnailService $thumbnailService |
||||||
| 52 | * @param PreviewService $previewService |
||||||
| 53 | * @param DownloadService $downloadService |
||||||
| 54 | * @param EventSource $eventSource |
||||||
| 55 | * @param ILogger $logger |
||||||
| 56 | */ |
||||||
| 57 | public function __construct( |
||||||
| 58 | $appName, |
||||||
| 59 | IRequest $request, |
||||||
| 60 | IURLGenerator $urlGenerator, |
||||||
| 61 | ConfigService $configService, |
||||||
| 62 | ThumbnailService $thumbnailService, |
||||||
| 63 | PreviewService $previewService, |
||||||
| 64 | DownloadService $downloadService, |
||||||
| 65 | EventSource $eventSource, |
||||||
| 66 | ILogger $logger |
||||||
| 67 | ) { |
||||||
| 68 | parent::__construct($appName, $request); |
||||||
| 69 | |||||||
| 70 | $this->urlGenerator = $urlGenerator; |
||||||
| 71 | $this->configService = $configService; |
||||||
| 72 | $this->thumbnailService = $thumbnailService; |
||||||
| 73 | $this->previewService = $previewService; |
||||||
| 74 | $this->downloadService = $downloadService; |
||||||
| 75 | $this->eventSource = $eventSource; |
||||||
| 76 | $this->logger = $logger; |
||||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /** |
||||||
| 80 | * @NoAdminRequired |
||||||
| 81 | * |
||||||
| 82 | * Generates thumbnails |
||||||
| 83 | * |
||||||
| 84 | * Uses EventSource to send thumbnails back as soon as they're created |
||||||
| 85 | * |
||||||
| 86 | * FIXME: @LukasReschke says: The exit is required here because |
||||||
| 87 | * otherwise the AppFramework is trying to add headers as well after |
||||||
| 88 | * dispatching the request which results in a "Cannot modify header |
||||||
| 89 | * information" notice. |
||||||
| 90 | * |
||||||
| 91 | * WARNING: Returning a JSON response does not get rid of the problem |
||||||
| 92 | * |
||||||
| 93 | * @param string $ids the ID of the files of which we need thumbnail previews of |
||||||
| 94 | * @param bool $square |
||||||
| 95 | * @param double $scale |
||||||
| 96 | * |
||||||
| 97 | * @return array<string,array|string|null> |
||||||
| 98 | */ |
||||||
| 99 | public function getThumbnails($ids, $square, $scale) { |
||||||
| 100 | $idsArray = explode(';', $ids); |
||||||
| 101 | |||||||
| 102 | foreach ($idsArray as $id) { |
||||||
| 103 | // Casting to integer here instead of using array_map to extract IDs from the URL |
||||||
| 104 | list($thumbnail, $status) = $this->getThumbnail((int)$id, $square, $scale); |
||||||
| 105 | $thumbnail['fileid'] = $id; |
||||||
| 106 | $thumbnail['status'] = $status; |
||||||
| 107 | |||||||
| 108 | $this->eventSource->send('preview', $thumbnail); |
||||||
| 109 | } |
||||||
| 110 | $this->eventSource->close(); |
||||||
| 111 | |||||||
| 112 | $this->exitController(); |
||||||
| 113 | // @codeCoverageIgnoreStart |
||||||
| 114 | } // @codeCoverageIgnoreEnd |
||||||
| 115 | |||||||
| 116 | /** |
||||||
| 117 | * @NoAdminRequired |
||||||
| 118 | * @NoCSRFRequired |
||||||
| 119 | * |
||||||
| 120 | * Sends either a large preview of the requested file or the original file itself |
||||||
| 121 | * |
||||||
| 122 | * @param int $fileId the ID of the file of which we need a large preview of |
||||||
| 123 | * @param int $width |
||||||
| 124 | * @param int $height |
||||||
| 125 | * |
||||||
| 126 | * @return DataResponse|ImageResponse|JSONResponse |
||||||
| 127 | */ |
||||||
| 128 | public function getPreview($fileId, $width, $height) { |
||||||
| 129 | /** @type File $file */ |
||||||
| 130 | list($file, $status) = $this->getFile($fileId); |
||||||
| 131 | if ($this->request->getHeader('If-None-Match') === $file->getEtag()) { |
||||||
| 132 | return new DataResponse([], Http::STATUS_NOT_MODIFIED); |
||||||
| 133 | } |
||||||
| 134 | list($file, $preview, $status) = $this->getData($fileId, $width, $height); |
||||||
| 135 | |||||||
| 136 | if (!$preview) { |
||||||
| 137 | return new JSONResponse( |
||||||
| 138 | [ |
||||||
| 139 | 'message' => "I'm truly sorry, but we were unable to generate a preview for this file", |
||||||
| 140 | 'success' => false |
||||||
| 141 | ], $status |
||||||
| 142 | ); |
||||||
| 143 | } |
||||||
| 144 | $preview['name'] = $file->getName(); |
||||||
| 145 | |||||||
| 146 | $response = new ImageResponse($preview, $status); |
||||||
|
0 ignored issues
–
show
$preview of type OC_Image|string is incompatible with the type array expected by parameter $image of OCA\Gallery\Http\ImageResponse::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
$status of type OC_Image|string is incompatible with the type integer expected by parameter $statusCode of OCA\Gallery\Http\ImageResponse::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 147 | $response->setETag($file->getEtag()); |
||||||
| 148 | $lastModified = new \DateTime(); |
||||||
| 149 | $lastModified->setTimestamp($file->getMTime()); |
||||||
| 150 | $response->setLastModified($lastModified); |
||||||
| 151 | $response->cacheFor(3600*24); |
||||||
| 152 | return $response; |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | } |
||||||
| 156 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths