Completed
Pull Request — master (#490)
by
unknown
01:54
created

PreviewController::setModifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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;
16
use OCP\IRequest;
17
use OCP\IURLGenerator;
18
use OCP\ILogger;
19
use OCP\Files\File;
20
21
use OCP\AppFramework\Controller;
22
use OCP\AppFramework\Http;
23
use OCP\AppFramework\Http\JSONResponse;
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\Service\FilePropertiesService;
31
use OCA\Gallery\Utility\EventSource;
32
33
/**
34
 * Class PreviewController
35
 *
36
 * @package OCA\Gallery\Controller
37
 */
38
class PreviewController extends Controller {
39
40
	use Preview;
41
42
	/** @var EventSource */
43
	private $eventSource;
44
45
	/**
46
	 * Constructor
47
	 *
48
	 * @param string $appName
49
	 * @param IRequest $request
50
	 * @param IURLGenerator $urlGenerator
51
	 * @param ConfigService $configService
52
	 * @param ThumbnailService $thumbnailService
53
	 * @param PreviewService $previewService
54
	 * @param DownloadService $downloadService
55
	 * @param EventSource $eventSource
56
	 * @param ILogger $logger
57
	 */
58
	public function __construct(
59
		$appName,
60
		IRequest $request,
61
		IURLGenerator $urlGenerator,
62
		ConfigService $configService,
63
		ThumbnailService $thumbnailService,
64
		PreviewService $previewService,
65
		DownloadService $downloadService,
66
		FilePropertiesService $filePropertiesService,
67
		EventSource $eventSource,
68
		ILogger $logger
69
	) {
70
		parent::__construct($appName, $request);
71
72
		$this->urlGenerator = $urlGenerator;
73
		$this->configService = $configService;
74
		$this->thumbnailService = $thumbnailService;
75
		$this->previewService = $previewService;
76
		$this->downloadService = $downloadService;
77
		$this->filePropertiesService = $filePropertiesService;
78
		$this->eventSource = $eventSource;
79
		$this->logger = $logger;
80
	}
81
82
	/**
83
	 * @NoAdminRequired
84
	 *
85
	 * Generates thumbnails
86
	 *
87
	 * Uses EventSource to send thumbnails back as soon as they're created
88
	 *
89
	 * FIXME: @LukasReschke says: The exit is required here because
90
	 * otherwise the AppFramework is trying to add headers as well after
91
	 * dispatching the request which results in a "Cannot modify header
92
	 * information" notice.
93
	 *
94
	 * WARNING: Returning a JSON response does not get rid of the problem
95
	 *
96
	 * @param string $ids the ID of the files of which we need thumbnail previews of
97
	 * @param bool $square
98
	 * @param double $scale
99
	 *
100
	 * @return array<string,array|string|null>
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,array|string|null>|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
101
	 */
102
	public function getThumbnails($ids, $square, $scale) {
103
		$idsArray = explode(';', $ids);
104
105
		foreach ($idsArray as $id) {
106
			// Casting to integer here instead of using array_map to extract IDs from the URL
107
			list($thumbnail, $status) = $this->getThumbnail((int)$id, $square, $scale);
108
			$thumbnail['fileid'] = $id;
109
			$thumbnail['status'] = $status;
110
111
			$this->eventSource->send('preview', $thumbnail);
112
		}
113
		$this->eventSource->close();
114
115
		$this->exitController();
116
		// @codeCoverageIgnoreStart
117
	} // @codeCoverageIgnoreEnd
118
119
	/**
120
	 * @NoAdminRequired
121
	 * @NoCSRFRequired
122
	 *
123
	 * Sends either a large preview of the requested file or the original file itself
124
	 *
125
	 * @param int $fileId the ID of the file of which we need a large preview of
126
	 * @param int $width
127
	 * @param int $height
128
	 *
129
	 * @return DataResponse|ImageResponse|JSONResponse
130
	 */
131
	public function getPreview($fileId, $width, $height) {
132
		/** @type File $file */
133
		list($file, $status) = $this->getFile($fileId);
134
		if ($this->request->getHeader('If-None-Match') === $file->getEtag()) {
135
			return new DataResponse([], Http::STATUS_NOT_MODIFIED);
136
		}
137
		list($file, $preview, $status) = $this->getData($fileId, $width, $height);
138
139
		if (!$preview) {
140
			return new JSONResponse(
141
				[
142
					'message' => "I'm truly sorry, but we were unable to generate a preview for this file",
143
					'success' => false
144
				], $status
145
			);
146
		}
147
		$preview['name'] = $file->getName();
148
149
		$response = new ImageResponse($preview, $status);
150
		$response->setETag($file->getEtag());
151
		$lastModified = new \DateTime();
152
		$lastModified->setTimestamp($file->getMTime());
153
		$response->setLastModified($lastModified);
154
		$response->cacheFor(3600*24);
155
		return $response;
156
	}
157
158
	public function setModifications($fileId) {
159
		$requestBody = file_get_contents('php://input');
160
		$this->filePropertiesService->setModifications($fileId, $requestBody);
161
	}
162
163
	public function getModifications($fileId) {
164
		return new DataResponse(json_decode($this->filePropertiesService->getModifications($fileId)), 200);
165
	}
166
167
}
168