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
|
<?php
namespace MediaWiki\Rest\Handler;
use File;
use MediaFileTrait;
use MediaWiki\Page\ExistingPageRecord;
use MediaWiki\Page\PageLookup;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use RepoGroup;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\ParamValidator;
/**
* Handler class for media meta-data
*/
class MediaFileHandler extends SimpleHandler {
use 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;
}
/**
* @return ExistingPageRecord|null
*/
private function getPage(): ?ExistingPageRecord {
if ( $this->page === false ) {
$this->page = $this->pageLookup->getExistingPageByText(
$this->getValidatedParams()['title'], NS_FILE
);
}
return $this->page;
}
/**
* @return File|null
*/
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,
],
];
}
/**
* @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();
}
}
|