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

previewservice.php « service - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27157e0f5020ab29292d8d2e8829b5b5dc7dbbdb (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
228
229
230
231
232
<?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>
 *
 * @copyright Olivier Paroz 2014-2015
 */

namespace OCA\Gallery\Service;

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

use OCA\Gallery\Environment\Environment;
use OCA\Gallery\Preview\Preview;

/**
 * Generates previews
 *
 * @package OCA\Gallery\Service
 */
class PreviewService extends Service {

	use Base64Encode;

	/** @var Preview */
	private $previewManager;

	/**
	 * Constructor
	 *
	 * @param string $appName
	 * @param Environment $environment
	 * @param Preview $previewManager
	 * @param ILogger $logger
	 */
	public function __construct(
		$appName,
		Environment $environment,
		Preview $previewManager,
		ILogger $logger
	) {
		parent::__construct($appName, $environment, $logger);

		$this->previewManager = $previewManager;
	}

	/**
	 * Decides if we should download the file instead of generating a preview
	 *
	 * @param File $file
	 * @param bool $animatedPreview
	 *
	 * @return bool
	 */
	public function isPreviewRequired($file, $animatedPreview) {
		$mime = $file->getMimeType();

		if ($mime === 'image/svg+xml') {
			return $this->isSvgPreviewRequired();
		}
		if ($mime === 'image/gif') {
			return $this->isGifPreviewRequired($file, $animatedPreview);
		}

		return true;
	}

	/**
	 * Returns an array containing everything needed by the client to be able to display a preview
	 *
	 *    * fileid:  the file's ID
	 *    * mimetype: the file's media type
	 *    * preview: the preview's content
	 *
	 * Example logger
	 * $this->logger->debug(
	 * "[PreviewService] Path : {path} / mime: {mimetype} / fileid: {fileid}",
	 * [
	 * 'path'     => $preview['data']['path'],
	 * 'mimetype' => $preview['data']['mimetype'],
	 * 'fileid'   => $preview['fileid']
	 * ]
	 * );
	 *
	 * @todo Get the max size from the settings
	 *
	 * @param File $file
	 * @param int $maxX asked width for the preview
	 * @param int $maxY asked height for the preview
	 * @param bool $keepAspect
	 * @param bool $base64Encode
	 *
	 * @return array <string,\OC_Image|string>|false preview data
	 * @throws InternalServerErrorServiceException
	 */
	public function createPreview(
		$file, $maxX = 0, $maxY = 0, $keepAspect = true, $base64Encode = false
	) {
		try {
			$userId = $this->environment->getUserId();
			$imagePathFromFolder = $this->environment->getPathFromUserFolder($file);
			$this->previewManager->setupView($userId, $file, $imagePathFromFolder);
			$preview = $this->previewManager->preparePreview($maxX, $maxY, $keepAspect);
			if ($preview && $base64Encode) {
				$preview['preview'] = $this->encode($preview['preview']);
			}

			return $preview;
		} catch (\Exception $exception) {
			throw new InternalServerErrorServiceException('Preview generation has failed');
		}
	}

	/**
	 * Makes sure we return previews of the asked dimensions and fix the cache
	 * if necessary
	 *
	 * @param bool $square
	 * @param bool $base64Encode
	 *
	 * @return \OC_Image|string
	 * @throws InternalServerErrorServiceException
	 */
	public function previewValidator($square, $base64Encode) {
		try {
			$preview = $this->previewManager->previewValidator($square);
			if ($base64Encode) {
				$preview = $this->encode($preview);
			}

			return $preview;
		} catch (\Exception $exception) {
			throw new InternalServerErrorServiceException(
				'There was an error while trying to fix the preview'
			);
		}
	}

	/**
	 * Returns true if the passed mime type is supported
	 *
	 * In case of a failure, we just return that the media type is not supported
	 *
	 * @param string $mimeType
	 *
	 * @return boolean
	 */
	private function isMimeSupported($mimeType = '*') {
		try {
			return $this->previewManager->isMimeSupported($mimeType);
		} catch (\Exception $exception) {
			unset($exception);

			return false;
		}
	}

	/**
	 * Decides if we should download the SVG or generate a preview
	 *
	 * SVGs are downloaded if the SVG converter is disabled
	 * Files of any media type are downloaded if requested by the client
	 *
	 * @return bool
	 */
	private function isSvgPreviewRequired() {
		return $this->isMimeSupported('image/svg+xml');
	}

	/**
	 * Decides if we should download the GIF or generate a preview
	 *
	 * GIFs are downloaded if they're animated and we want to show
	 * animations
	 *
	 * @param File $file
	 * @param bool $animatedPreview
	 *
	 * @return bool
	 */
	private function isGifPreviewRequired($file, $animatedPreview) {
		$gifSupport = $this->isMimeSupported('image/gif');
		$animatedGif = $this->isGifAnimated($file);

		return $gifSupport && !($animatedGif && $animatedPreview);
	}

	/**
	 * Tests if a GIF is animated
	 *
	 * An animated gif contains multiple "frames", with each frame having a
	 * header made up of:
	 *    * a static 4-byte sequence (\x00\x21\xF9\x04)
	 *    * 4 variable bytes
	 *    * a static 2-byte sequence (\x00\x2C) (Photoshop uses \x00\x21)
	 *
	 * We read through the file until we reach the end of the file, or we've
	 * found at least 2 frame headers
	 *
	 * @link http://php.net/manual/en/function.imagecreatefromgif.php#104473
	 *
	 * @param File $file
	 *
	 * @return bool
	 * @throws InternalServerErrorServiceException
	 */
	private function isGifAnimated($file) {
		$count = 0;
		try {
			$fileHandle = $file->fopen('rb');
			while (!feof($fileHandle) && $count < 2) {
				$chunk = fread($fileHandle, 1024 * 100); //read 100kb at a time
				$count += preg_match_all(
					'#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches
				);
			}
			fclose($fileHandle);
		} catch (\Exception $exception) {
			throw new InternalServerErrorServiceException(
				'Something went wrong when trying to read a GIF file'
			);
		}

		return $count > 1;
	}

}