3 namespace wcf\system\file\upload
;
7 use wcf\util\StringUtil
;
10 * An specific upload file.
12 * @author Joshua Ruesweg
13 * @copyright 2001-2019 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\File\Upload
21 * Location for the file.
33 * Flag whether svg files should detected as image
36 private $detectSvgAsImage;
39 * Indicator, whether the file is already processed.
51 * Indicator, whether the file is an image.
57 * Indicator, whether the file can be displayed as an image.
60 public $viewableImage;
63 * The filesize of the file.
69 * The unique id for the file.
75 * UploadFile constructor.
77 * @param string $location
78 * @param string $filename
79 * @param bool $viewableImage
80 * @param bool $processed
81 * @param bool $detectSvgAsImage
83 public function __construct(
86 $viewableImage = true,
88 $detectSvgAsImage = false
90 if (!\file_exists
($location)) {
91 throw new \
InvalidArgumentException("File '" . $location . "' could not be found.");
94 $this->location
= $location;
95 $this->filename
= $filename;
96 $this->filesize
= \filesize
($location);
97 $this->processed
= $processed;
98 $this->viewableImage
= $viewableImage;
99 $this->uniqueId
= StringUtil
::getRandomID();
100 $this->detectSvgAsImage
= $detectSvgAsImage;
103 @\
getimagesize($location) !== false ||
($detectSvgAsImage && \
in_array(FileUtil
::getMimeType($location), [
108 $this->isImage
= true;
113 * Returns true, whether this file is an image.
117 public function isImage()
119 return $this->isImage
;
123 * Returns the image location or a base64 encoded string of the image. Returns null
124 * if the file is not an image or the image is not viewable.
126 * @return string|null
128 public function getImage()
130 if (!$this->isImage() ||
!$this->viewableImage
) {
134 if ($this->processed
) {
135 if ($this->imageLink
=== null) {
137 return \
str_replace(WCF_DIR
, WCF
::getPath(), $this->location
);
139 return $this->imageLink
;
142 $imageData = @\
getimagesize($this->location
);
144 if ($imageData !== false) {
145 return 'data:' . $imageData['mime'] . ';base64,' . \base
64_encode
(\file_get_contents
($this->location
));
149 $this->detectSvgAsImage
&& \
in_array(FileUtil
::getMimeType($this->location
), [
154 return 'data:image/svg+xml;base64,' . \base
64_encode
(\file_get_contents
($this->location
));
157 throw new \
LogicException('File is an image, but can not be rendered.');
162 * Returns the location of the file.
166 public function getLocation()
168 return $this->location
;
172 * Returns the filename of the file.
176 public function getFilename()
178 return $this->filename
;
182 * Returns the unique file id for the file. It is used to identify the certain file.
185 public function getUniqueFileId()
187 return $this->uniqueId
;
191 * Sets the new location of the file, after it is processed and
192 * sets the `processed` attribute to true.
194 * @param string $newLocation
196 public function setProcessed($newLocation)
198 if (!\file_exists
($newLocation)) {
199 throw new \
InvalidArgumentException("File '" . $newLocation . "' could not be found.");
202 $this->location
= $newLocation;
203 $this->processed
= true;
207 * Sets the new image link of the file for processed files.
209 * @param string $link
211 public function setImageLink($link)
213 $this->imageLink
= $link;
217 * Returns true, if the file is already processed.
221 public function isProcessed()
223 return $this->processed
;
227 * Returns icon name for this attachment.
231 public function getIconName()
233 if ($iconName = FileUtil
::getIconNameByFilename($this->filename
)) {
234 return 'file-' . $iconName . '-o';