Welcome to mirror list, hosted at ThFree Co, Russian Federation.

preview.php « controller - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4ebf8a569ed8862271c23f87467e10aa1d22191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
 * ownCloud - gallery
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Olivier Paroz <owncloud@interfasys.ch>
 * @author Robin Appelman <icewind@owncloud.com>
 *
 * @copyright Olivier Paroz 2014-2015
 * @copyright Robin Appelman 2012-2014
 */

namespace OCA\Gallery\Controller;

use OCP\IURLGenerator;
use OCP\ILogger;
use OCP\Files\File;

use OCP\AppFramework\Http;

use OCA\Gallery\Service\ServiceException;
use OCA\Gallery\Service\NotFoundServiceException;
use OCA\Gallery\Service\ThumbnailService;
use OCA\Gallery\Service\PreviewService;
use OCA\Gallery\Service\DownloadService;

/**
 * Class Preview
 *
 * @package OCA\Gallery\Controller
 */
trait Preview {

	use HttpError;

	/** @var IURLGenerator */
	private $urlGenerator;
	/**  @var ThumbnailService */
	private $thumbnailService;
	/**  @var PreviewService */
	private $previewService;
	/** @var DownloadService */
	private $downloadService;
	/** @var ILogger */
	private $logger;
	/** @type bool */
	private $download = false;

	/**
	 * Exits the controller in a live environment and throws an exception when testing
	 */
	protected function exitController() {
		if (defined('PHPUNIT_RUN')) {
			throw new \Exception();
			// @codeCoverageIgnoreStart
		} else {
			exit();
		}
		// @codeCoverageIgnoreEnd
	}

	/**
	 * Retrieves the thumbnail to send back to the browser
	 *
	 * The thumbnail is either a resized preview of the file or the original file
	 * Thumbnails are base64encoded before getting sent back
	 *
	 *
	 * @param int $fileId the ID of the file of which we need a thumbnail preview of
	 * @param bool $square whether the thumbnail should be square
	 * @param double $scale whether we're allowed to scale the preview up
	 *
	 * @return array<string,array|string>
	 */
	private function getThumbnail($fileId, $square, $scale) {
		list($width, $height, $aspect, $animatedPreview, $base64Encode) =
			$this->thumbnailService->getThumbnailSpecs($square, $scale);
		/** @type File $file */
		list($file, $preview, $status, $type) =
			$this->getData(
				$fileId, $width, $height, $aspect, $animatedPreview, $base64Encode
			);
		if ($preview === null) {
			$preview = $this->prepareEmptyThumbnail($file, $status);
		} else {
			if ($type === 'preview') {
				$preview['preview'] =
					$this->previewService->previewValidator($square, $base64Encode);
			}
		}

		return [$preview, $status];
	}

	/**
	 * Returns either a generated preview, the file as-is or an empty object
	 *
	 * @param int $fileId
	 * @param int $width
	 * @param int $height
	 * @param bool $keepAspect
	 * @param bool $animatedPreview
	 * @param bool $base64Encode
	 *
	 * @return array<string,\OC_Image|string>
	 *
	 * @throws NotFoundServiceException
	 */
	private function getData(
		$fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false
	) {
		/** @type File $file */
		$file = $this->getFile($fileId);
		try {
			if (!is_null($file)) {
				$data = $this->getPreviewData(
					$file, $animatedPreview, $width, $height, $keepAspect, $base64Encode
				);
			} else {
				$data = $this->getErrorData(Http::STATUS_NOT_FOUND);
			}
		} catch (ServiceException $exception) {
			$data = $this->getExceptionData($exception);
		}
		array_unshift($data, $file);

		return $data;
	}

	/**
	 * Returns the file of which a preview will be generated
	 *
	 * @param $fileId
	 *
	 * @return File|null
	 */
	private function getFile($fileId) {
		try {
			/** @type File $file */
			$file = $this->previewService->getResourceFromId($fileId);
		} catch (ServiceException $exception) {
			$file = null;
		}

		return $file;
	}

	/**
	 * @param File $file
	 * @param bool $animatedPreview
	 * @param int $width
	 * @param int $height
	 * @param bool $keepAspect
	 * @param bool $base64Encode
	 *
	 * @return array
	 */
	private function getPreviewData(
		$file, $animatedPreview, $width, $height, $keepAspect, $base64Encode
	) {
		$status = Http::STATUS_OK;
		if ($this->previewService->isPreviewRequired($file, $animatedPreview)) {
			$type = 'preview';
			$preview = $this->previewService->createPreview(
				$file, $width, $height, $keepAspect, $base64Encode
			);
		} else {
			$type = 'download';
			$preview = $this->downloadService->downloadFile($file, $base64Encode);
		}
		if (!$preview) {
			list($preview, $status, $type) = $this->getErrorData();
		}

		return [$preview, $status, $type];
	}

	/**
	 * Returns an error array
	 *
	 * @param $status
	 *
	 * @return array<null|int|string>
	 */
	private function getErrorData($status = Http::STATUS_INTERNAL_SERVER_ERROR) {
		return [null, $status, 'error'];
	}

	/**
	 * Returns an error array
	 *
	 * @param $exception
	 *
	 * @return array<null|int|string>
	 */
	private function getExceptionData($exception) {
		$code = $this->getHttpStatusCode($exception);

		return $this->getErrorData($code);
	}

	/**
	 * Prepares an empty Thumbnail array to send back
	 *
	 * When we can't even get the file information, we send an empty mimeType
	 *
	 * @param File $file
	 * @param int $status
	 *
	 * @return array<string,null|string>
	 */
	private function prepareEmptyThumbnail($file, $status) {
		$thumbnail = [];
		if ($status !== Http::STATUS_NOT_FOUND) {
			$mimeType = '';
			if ($file) {
				$mimeType = $file->getMimeType();
			}
			$thumbnail = ['preview' => null, 'mimetype' => $mimeType];
		}

		return $thumbnail;
	}

}