aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Rest/Handler/MediaFileHandler.php
blob: 6c16b2cfbec453bef04544dbde9a6b4b9307aa92 (plain) (blame)
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
<?php

namespace MediaWiki\Rest\Handler;

use MediaWiki\FileRepo\File\File;
use MediaWiki\FileRepo\RepoGroup;
use MediaWiki\Page\ExistingPageRecord;
use MediaWiki\Page\PageLookup;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * Handler class for media meta-data
 */
class MediaFileHandler extends SimpleHandler {
	use \MediaWiki\FileRepo\File\MediaFileTrait;

	private RepoGroup $repoGroup;
	private PageLookup $pageLookup;

	/**
	 * @var ExistingPageRecord|false|null
	 */
	private $page = false;

	/**
	 * @var File|false|null
	 */
	private $file = false;

	public function __construct(
		RepoGroup $repoGroup,
		PageLookup $pageLookup
	) {
		$this->repoGroup = $repoGroup;
		$this->pageLookup = $pageLookup;
	}

	private function getPage(): ?ExistingPageRecord {
		if ( $this->page === false ) {
			$this->page = $this->pageLookup->getExistingPageByText(
					$this->getValidatedParams()['title'], NS_FILE
				);
		}
		return $this->page;
	}

	private function getFile(): ?File {
		if ( $this->file === false ) {
			$page = $this->getPage();
			$this->file =
				// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
				$this->repoGroup->findFile( $page, [ 'private' => $this->getAuthority() ] ) ?: null;
		}
		return $this->file;
	}

	/**
	 * @param string $title
	 * @return Response
	 * @throws LocalizedHttpException
	 */
	public function run( $title ) {
		$page = $this->getPage();

		if ( !$page ) {
			throw new LocalizedHttpException(
				MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
				404
			);
		}

		if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
			throw new LocalizedHttpException(
				MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
				403
			);
		}

		$fileObj = $this->getFile();
		if ( !$fileObj || !$fileObj->exists() ) {
			throw new LocalizedHttpException(
				MessageValue::new( 'rest-cannot-load-file' )->plaintextParams( $title ),
				404
			);
		}

		$response = $this->getResponse( $fileObj );
		return $this->getResponseFactory()->createJson( $response );
	}

	/**
	 * @param File $file the file to load media links for
	 * @return array response data
	 */
	private function getResponse( File $file ): array {
		[ $maxWidth, $maxHeight ] = self::getImageLimitsFromOption(
			$this->getAuthority()->getUser(), 'imagesize'
		);
		[ $maxThumbWidth, $maxThumbHeight ] = self::getImageLimitsFromOption(
			$this->getAuthority()->getUser(), 'thumbsize'
		);
		$transforms = [
			'preferred' => [
				'maxWidth' => $maxWidth,
				'maxHeight' => $maxHeight
			],
			'thumbnail' => [
				'maxWidth' => $maxThumbWidth,
				'maxHeight' => $maxThumbHeight
			]
		];

		return $this->getFileInfo( $file, $this->getAuthority(), $transforms );
	}

	public function needsWriteAccess() {
		return false;
	}

	public function getParamSettings() {
		return [
			'title' => [
				self::PARAM_SOURCE => 'path',
				ParamValidator::PARAM_TYPE => 'string',
				ParamValidator::PARAM_REQUIRED => true,
				Handler::PARAM_DESCRIPTION => new MessageValue( 'rest-param-desc-media-file-title' ),
			],
		];
	}

	/**
	 * @return string|null
	 * @throws LocalizedHttpException
	 */
	protected function getETag(): ?string {
		$file = $this->getFile();
		if ( !$file || !$file->exists() ) {
			return null;
		}

		return '"' . $file->getSha1() . '"';
	}

	/**
	 * @return string|null
	 * @throws LocalizedHttpException
	 */
	protected function getLastModified(): ?string {
		$file = $this->getFile();
		if ( !$file || !$file->exists() ) {
			return null;
		}

		return $file->getTimestamp();
	}

	/**
	 * @return bool
	 */
	protected function hasRepresentation() {
		$file = $this->getFile();
		return $file && $file->exists();
	}

	public function getResponseBodySchemaFileName( string $method ): ?string {
		return 'includes/Rest/Handler/Schema/MediaFile.json';
	}
}