5 use Laminas\Diactoros\Response\EmptyResponse
;
6 use wcf\data\media\Media
;
7 use wcf\data\media\MediaEditor
;
8 use wcf\system\exception\IllegalLinkException
;
9 use wcf\system\exception\PermissionDeniedException
;
10 use wcf\util\FileReader
;
11 use wcf\util\StringUtil
;
16 * @author Matthias Schmidt
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 class MediaPage
extends AbstractPage
23 const AVAILABLE_DURING_OFFLINE_MODE
= true;
26 * etag for the media file
38 * requested media file
44 * id of the requested media file
50 * size of the requested thumbnail
53 public $thumbnail = '';
58 public $useTemplate = false;
61 * list of mime types which belong to files that are displayed inline
64 public static $inlineMimeTypes = [
77 public function readData()
82 if ($this->thumbnail
) {
83 $mimeType = $this->media
->{$this->thumbnail
. 'ThumbnailType'};
84 $filesize = $this->media
->{$this->thumbnail
. 'ThumbnailSize'};
85 $location = $this->media
->getThumbnailLocation($this->thumbnail
);
86 $this->eTag
= \
strtoupper($this->thumbnail
) . '_' . $this->mediaID
;
88 $mimeType = $this->media
->fileType
;
89 $filesize = $this->media
->filesize
;
90 $location = $this->media
->getLocation();
91 $this->eTag
= $this->mediaID
;
94 $this->eTag
.= '_' . $this->media
->fileHash
;
98 $this->fileReader
= new FileReader($location, [
99 'filename' => $this->media
->filename
,
100 'mimeType' => $mimeType,
101 'filesize' => $filesize,
102 'showInline' => \
in_array($mimeType, self
::$inlineMimeTypes),
103 'enableRangeSupport' => $this->thumbnail ?
true : false,
104 'lastModificationTime' => $this->media
->fileUpdateTime ??
$this->media
->uploadTime
,
105 'expirationDate' => TIME_NOW +
$maxAge,
109 if ($this->eTag
!== null) {
110 $this->fileReader
->addHeader('ETag', '"' . $this->eTag
. '"');
117 public function readParameters()
119 parent
::readParameters();
121 if (isset($_REQUEST['id'])) {
122 $this->mediaID
= \
intval($_REQUEST['id']);
124 $this->media
= new Media($this->mediaID
);
125 if (!$this->media
->mediaID
) {
126 throw new IllegalLinkException();
128 if (!$this->media
->isAccessible()) {
129 throw new PermissionDeniedException();
132 if (isset($_REQUEST['thumbnail'])) {
133 $this->thumbnail
= StringUtil
::trim($_REQUEST['thumbnail']);
135 if ($this->thumbnail
=== 'original') {
136 // The 'original' size is required by the editor, but is not a valid thumbnail size.
137 $this->thumbnail
= '';
139 if ($this->thumbnail
&& !isset(Media
::getThumbnailSizes()[$this->thumbnail
])) {
140 throw new IllegalLinkException();
143 if ($this->thumbnail
&& !$this->media
->{$this->thumbnail
. 'ThumbnailType'}) {
144 $this->thumbnail
= '';
151 public function show()
156 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"' . $this->eTag
. '"') {
157 return new EmptyResponse(304);
160 if (!$this->thumbnail
) {
161 // update download count
162 (new MediaEditor($this->media
))->update([
163 'downloads' => $this->media
->downloads +
1,
164 'lastDownloadTime' => TIME_NOW
,
168 // send file to client
169 $this->fileReader
->send();