Test Failed
Branch master (4a3c5b)
by Greg
12:31
created

MediaController::fileExtensionAsImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2017 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 ErrorException;
21
use Fisharebest\Webtrees\Media;
22
use Fisharebest\Webtrees\MediaFile;
23
use Fisharebest\Webtrees\Site;
24
use Fisharebest\Webtrees\Tree;
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
37
/**
38
 * Controller for the media page and displaying images.
39
 */
40
class MediaController extends BaseController {
41
	/**
42
	 * Show an image/thumbnail, with/without a watermark.
43
	 *
44
	 * @param Request $request
45
	 *
46
	 * @return Response
47
	 */
48
	public function mediaThumbnail(Request $request): Response {
49
		/** @var Tree $tree */
50
		$tree    = $request->attributes->get('tree');
51
		$xref    = $request->get('xref');
52
		$fact_id = $request->get('fact_id');
53
		$media   = Media::getInstance($xref, $tree);
54
55
		if ($media === null) {
56
			return $this->httpStatusAsImage(Response::HTTP_NOT_FOUND);
57
		}
58
59
		if (!$media->canShow()) {
60
			return $this->httpStatusAsImage(Response::HTTP_FORBIDDEN);
61
		}
62
63
		// @TODO handle SVG files
64
		foreach ($media->mediaFiles() as $media_file) {
0 ignored issues
show
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

64
		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\Family. ( Ignorable by Annotation )

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

64
		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

64
		foreach ($media->/** @scrutinizer ignore-call */ mediaFiles() as $media_file) {
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

64
		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\Individual. ( Ignorable by Annotation )

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

64
		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

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