Completed
Push — develop ( 9087a8...c9b4ef )
by Greg
16:31 queued 05:44
created

MediaFileController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fileExtensionAsImage() 0 7 1
B generateImage() 0 22 4
C mediaThumbnail() 0 29 7
A httpStatusAsImage() 0 6 1
A graphicsDriver() 0 8 2
A glideSignature() 0 5 1
A glideServer() 0 11 1
A unusedMediaThumbnail() 0 21 4
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2018 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Controllers;
19
20
use Fisharebest\Webtrees\Media;
21
use Fisharebest\Webtrees\MediaFile;
22
use Fisharebest\Webtrees\Site;
23
use Fisharebest\Webtrees\Tree;
24
use Intervention\Image\Exception\NotReadableException;
25
use League\Flysystem\Adapter\Local;
26
use League\Flysystem\Filesystem;
27
use League\Glide\Filesystem\FileNotFoundException;
28
use League\Glide\Server;
29
use League\Glide\ServerFactory;
30
use League\Glide\Signatures\Signature;
31
use League\Glide\Signatures\SignatureException;
32
use League\Glide\Signatures\SignatureFactory;
33
use Symfony\Component\HttpFoundation\RedirectResponse;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpFoundation\Response;
36
use Throwable;
37
38
/**
39
 * Controller for the media page and displaying images.
40
 */
41
class MediaFileController extends BaseController {
42
	/**
43
	 * Show an image/thumbnail, with/without a watermark.
44
	 *
45
	 * @param Request $request
46
	 *
47
	 * @return Response
48
	 */
49
	public function mediaThumbnail(Request $request): Response {
50
		/** @var Tree $tree */
51
		$tree    = $request->attributes->get('tree');
52
		$xref    = $request->get('xref');
53
		$fact_id = $request->get('fact_id');
54
		$media   = Media::getInstance($xref, $tree);
55
56
		if ($media === null) {
57
			return $this->httpStatusAsImage(Response::HTTP_NOT_FOUND);
58
		}
59
60
		if (!$media->canShow()) {
61
			return $this->httpStatusAsImage(Response::HTTP_FORBIDDEN);
62
		}
63
64
		// @TODO handle SVG files
65
		foreach ($media->mediaFiles() as $media_file) {
0 ignored issues
show
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\Individual. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\Source. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\Note. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\Repository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\GedcomRecord. It seems like you code against a sub-type of Fisharebest\Webtrees\GedcomRecord such as Fisharebest\Webtrees\Media. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {
Loading history...
Bug introduced by
The method mediaFiles() does not exist on Fisharebest\Webtrees\Family. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
			if ($media_file->factId() === $fact_id) {
67
				if ($media_file->isExternal()) {
68
					return new RedirectResponse($media_file->filename());
69
				} else if ($media_file->isImage()) {
70
					return $this->generateImage($media_file, $request->query->all());
71
				} else {
72
					return $this->fileExtensionAsImage($media_file->extension());
73
				}
74
			}
75
		}
76
77
		return $this->httpStatusAsImage(Response::HTTP_NOT_FOUND);
78
	}
79
80
	/**
81
	 * Generate a thumbnail for an unsed media file (i.e. not used by any media object).
82
	 *
83
	 * @param Request $request
84
	 *
85
	 * @return Response
86
	 */
87
	public function unusedMediaThumbnail(Request $request): Response {
88
		$folder = $request->get('folder');
89
		$file   = $request->get('file');
90
91
		try {
92
			$server = $this->glideServer($folder);
93
			$path   = $server->makeImage($file, $request->query->all());
94
			$cache  = $server->getCache();
95
96
			return new Response($cache->read($path), Response::HTTP_OK, [
97
				'Content-Type'   => $cache->getMimeType($path),
98
				'Content-Length' => $cache->getSize($path),
99
				'Cache-Control'  => 'max-age=31536000, public',
100
				'Expires'        => date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT',
101
			]);
102
		} catch (FileNotFoundException $ex) {
103
			return $this->httpStatusAsImage(Response::HTTP_NOT_FOUND);
104
		} catch (NotReadableException $ex) {
105
			return $this->httpStatusAsImage(Response::HTTP_INTERNAL_SERVER_ERROR);
106
		} catch (Throwable $ex) {
107
			return $this->httpStatusAsImage(Response::HTTP_INTERNAL_SERVER_ERROR);
108
		}
109
	}
110
111
	/**
112
	 * Generate a thumbnail image for a file.
113
	 *
114
	 * @param MediaFile $media_file
115
	 * @param array     $params
116
	 *
117
	 * @return Response
118
	 */
119
	private function generateImage(MediaFile $media_file, array $params): Response {
120
		try {
121
			// Validate HTTP signature
122
			$signature = $this->glideSignature();
123
124
			$signature->validateRequest(parse_url(WT_BASE_URL . 'index.php', PHP_URL_PATH), $params);
125
126
			$server = $this->glideServer($media_file->folder());
127
			$path   = $server->makeImage($media_file->filename(), $params);
128
129
			return new Response($server->getCache()->read($path), Response::HTTP_OK, [
130
				'Content-Type'   => $server->getCache()->getMimeType($path),
131
				'Content-Length' => $server->getCache()->getSize($path),
132
				'Cache-Control'  => 'max-age=31536000, public',
133
				'Expires'        => date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT',
134
			]);
135
		} catch (SignatureException $ex) {
136
			return $this->httpStatusAsImage(Response::HTTP_FORBIDDEN);
137
		} catch (FileNotFoundException $ex) {
138
			return $this->httpStatusAsImage(Response::HTTP_NOT_FOUND);
139
		} catch (Throwable $ex) {
140
			return $this->httpStatusAsImage(Response::HTTP_INTERNAL_SERVER_ERROR);
141
		}
142
	}
143
144
	/**
145
	 * Create a glide server to generate files in the specified folder
146
	 *
147
	 * Caution: $media_folder may contain relative paths: ../../
148
	 *
149
	 * @param string $media_folder
150
	 *
151
	 * @return Server
152
	 */
153
	private function glideServer(string $media_folder): Server {
154
		$cache_folder     = new Filesystem(new Local(WT_DATA_DIR . 'thumbnail-cache/' . md5($media_folder)));
155
		$driver           = $this->graphicsDriver();
156
		$source_folder    = new Filesystem(new Local($media_folder));
157
		$watermark_folder = new Filesystem(new Local('resources/assets/img'));
158
159
		return ServerFactory::create([
160
			'cache'      => $cache_folder,
161
			'driver'     => $driver,
162
			'source'     => $source_folder,
163
			'watermarks' => $watermark_folder,
164
		]);
165
	}
166
167
	/**
168
	 * Generate a signature, to verify the request parameters.
169
	 *
170
	 * @return Signature
171
	 */
172
	private function glideSignature(): Signature {
173
		$glide_key = Site::getPreference('glide-key');
174
		$signature = SignatureFactory::create($glide_key);
175
176
		return $signature;
177
	}
178
179
	/**
180
	 * Which graphics driver should we use for glide/intervention?
181
	 *
182
	 * Prefer ImageMagick
183
	 *
184
	 * @return string
185
	 */
186
	private function graphicsDriver(): string {
187
		if (extension_loaded('imagick')) {
188
			$driver = 'imagick';
189
		} else {
190
			$driver = 'gd';
191
		}
192
193
		return $driver;
194
	}
195
196
	/**
197
	 * Send a dummy image, to replace one that could not be found or created.
198
	 *
199
	 * @param int $status HTTP status code
200
	 *
201
	 * @return Response
202
	 */
203
	private function httpStatusAsImage(int $status): Response {
204
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="#F88" /><text x="5" y="55" font-family="Verdana" font-size="35">' . $status . '</text></svg>';
205
206
		// We can't use the actual status code, as browser's won't show images with 4xx/5xx
207
		return new Response($svg, Response::HTTP_OK, [
208
			'Content-Type' => 'image/svg+xml'
209
		]);
210
	}
211
212
	/**
213
	 * Send a dummy image, to replace a non-image file.
214
	 *
215
	 * @param string $extension
216
	 *
217
	 * @return Response
218
	 */
219
	private function fileExtensionAsImage(string $extension): Response {
220
		$extension = '.' . strtolower($extension);
221
222
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="#88F" /><text x="5" y="60" font-family="Verdana" font-size="30">' . $extension . '</text></svg>';
223
224
		return new Response($svg, Response::HTTP_OK, [
225
			'Content-Type' => 'image/svg+xml'
226
		]);
227
	}
228
}
229