<commit_before><?php
namespace wcf\page;
use wcf\data\media\Media;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\PermissionDeniedException;
use wcf\util\FileReader;
use wcf\util\StringUtil;

/**
 * Shows a media file.
 * 
 * @author	<NAME>
 * @copyright	2001-2017 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Page
 * @since	3.0
 */
class MediaPage extends AbstractPage {
	/**
	 * etag for the media file
	 * @var	string
	 */
	public $eTag;
	
	/**
	 * file reader object
	 * @var	FileReader
	 */
	public $fileReader;
	
	/**
	 * requested media file
	 * @var	Media
	 */
	public $media;
	
	/**
	 * id of the requested media file
	 * @var	integer
	 */
	public $mediaID = 0;
	
	/**
	 * size of the requested thumbnail
	 * @var	string
	 */
	public $thumbnail = '';
	
	/**
	 * @inheritDoc
	 */
	public $useTemplate = false;
	
	/**
	 * list of mime types which belong to files that are displayed inline
	 * @var	string[]
	 */
	public static $inlineMimeTypes = [
		'image/gif',
		'image/jpeg',
		'image/png',
		'image/x-png',
		'application/pdf',
		'image/pjpeg'
	];
	
	/**
	 * @inheritDoc
	 */
	public function readData() {
		parent::readData();
		
		// get file data
		if ($this->thumbnail) {
			$mimeType = $this->media->{$this->thumbnail.'ThumbnailType'};
			$filesize = $this->media->{$this->thumbnail.'ThumbnailSize'};
			$location = $this->media->getThumbnailLocation($this->thumbnail);
			$this->eTag = strtoupper($this->thumbnail).'_'.$this->mediaID;
		}
		else {
			$mimeType = $this->media->fileType;
			$filesize = $this->media->filesize;
			$location = $this->media->getLocation();
			$this->eTag = $this->mediaID;
		}
		
		// init file reader
		$this->fileReader = new FileReader($location, [
			'filename' => $this->media->filename,
			'mimeType' => $mimeType,
			'filesize' => $filesize,
			'showInline' => in_array($mimeType, self::$inlineMimeTypes),
			'enableRangeSupport' => $this->thumbnail ? true : false,
			'lastModificationTime' => $this->media->uploadTime,
			'expirationDate' => TIME_NOW + 31536000,
			'maxAge' => 31536000
		]);
		
		if ($this->eTag !== null) {
			$this->fileReader->addHeader('ETag', '"'.$this->eTag.'"');
		}
	}
	
	/**
	 * @inheritDoc
	 */
	public function readParameters() {
		parent::readParameters();
		
		if (isset($_REQUEST['id'])) $this->mediaID = intval($_REQUEST['id']);
		$this->media = new Media($this->mediaID);
		if (!$this->media->mediaID) {
			throw new IllegalLinkException();
		}
		if (!$this->media->isAccessible()) {
			throw new PermissionDeniedException();
		}
		
		if (isset($_REQUEST['thumbnail'])) $this->thumbnail = StringUtil::trim($_REQUEST['thumbnail']);
		if ($this->thumbnail && !isset(Media::getThumbnailSizes()[$this->thumbnail])) {
			throw new IllegalLinkException();
		}
		
		if ($this->thumbnail && !$this->media->{$this->thumbnail.'ThumbnailType'}) {
			$this->thumbnail = '';
		}
	}
	
	/**<commit_msg>Fixed broken media images in the editor<commit_after><?php
namespace wcf\page;
use wcf\data\media\Media;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\PermissionDeniedException;
use wcf\util\FileReader;
use wcf\util\StringUtil;

/**
 * Shows a media file.
 * 
 * @author	<NAME>
 * @copyright	2001-2017 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Page
 * @since	3.0
 */
class MediaPage extends AbstractPage {
	/**
	 * etag for the media file
	 * @var	string
	 */
	public $eTag;
	
	/**
	 * file reader object
	 * @var	FileReader
	 */
	public $fileReader;
	
	/**
	 * requested media file
	 * @var	Media
	 */
	public $media;
	
	/**
	 * id of the requested media file
	 * @var	integer
	 */
	public $mediaID = 0;
	
	/**
	 * size of the requested thumbnail
	 * @var	string
	 */
	public $thumbnail = '';
	
	/**
	 * @inheritDoc
	 */
	public $useTemplate = false;
	
	/**
	 * list of mime types which belong to files that are displayed inline
	 * @var	string[]
	 */
	public static $inlineMimeTypes = [
		'image/gif',
		'image/jpeg',
		'image/png',
		'image/x-png',
		'application/pdf',
		'image/pjpeg'
	];
	
	/**
	 * @inheritDoc
	 */
	public function readData() {
		parent::readData();
		
		// get file data
		if ($this->thumbnail) {
			$mimeType = $this->media->{$this->thumbnail.'ThumbnailType'};
			$filesize = $this->media->{$this->thumbnail.'ThumbnailSize'};
			$location = $this->media->getThumbnailLocation($this->thumbnail);
			$this->eTag = strtoupper($this->thumbnail).'_'.$this->mediaID;
		}
		else {
			$mimeType = $this->media->fileType;
			$filesize = $this->media->filesize;
			$location = $this->media->getLocation();
			$this->eTag = $this->mediaID;
		}
		
		// init file reader
		$this->fileReader = new FileReader($location, [
			'filename' => $this->media->filename,
			'mimeType' => $mimeType,
			'filesize' => $filesize,
			'showInline' => in_array($mimeType, self::$inlineMimeTypes),
			'enableRangeSupport' => $this->thumbnail ? true : false,
			'lastModificationTime' => $this->media->uploadTime,
			'expirationDate' => TIME_NOW + 31536000,
			'maxAge' => 31536000
		]);
		
		if ($this->eTag !== null) {
			$this->fileReader->addHeader('ETag', '"'.$this->eTag.'"');
		}
	}
	
	/**
	 * @inheritDoc
	 */
	public function readParameters() {
		parent::readParameters();
		
		if (isset($_REQUEST['id'])) $this->mediaID = intval($_REQUEST['id']);
		$this->media = new Media($this->mediaID);
		if (!$this->media->mediaID) {
			throw new IllegalLinkException();
		}
		if (!$this->media->isAccessible()) {
			throw new PermissionDeniedException();
		}
		
		if (isset($_REQUEST['thumbnail'])) $this->thumbnail = StringUtil::trim($_REQUEST['thumbnail']);
		if ($this->thumbnail === 'original') {
			// The 'original' size is required by the editor, but is not a valid thumbnail size.  
			$this->thumbnail = '';
		}
		if ($this->thumbnail && !isset(Media::getThumbnailSizes()[$this->thumbnail])) {
			throw new IllegalLinkException();
		}
		
		if ($this->thumbnail && !$this->media->{$this->thumbnail.'ThumbnailType'}) {
			$this->thumbnail = '';
		}
	}
	
	/**
{'fraction_non_alphanumeric': 0.18206829584075093, 'fraction_numerical': 0.008739278200356044, 'mean_word_length': 3.58117123795404, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6955251', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2069, 'n_words': 429}