Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / file / upload / UploadFile.class.php
1 <?php
2
3 namespace wcf\system\file\upload;
4
5 use wcf\system\WCF;
6 use wcf\util\FileUtil;
7 use wcf\util\StringUtil;
8
9 /**
10 * An specific upload file.
11 *
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
16 * @since 5.2
17 */
18 class UploadFile
19 {
20 /**
21 * Location for the file.
22 * @var string
23 */
24 private $location;
25
26 /**
27 * Full image link.
28 * @var string
29 */
30 private $imageLink;
31
32 /**
33 * Flag whether svg files should detected as image
34 * @var bool
35 */
36 private $detectSvgAsImage;
37
38 /**
39 * Indicator, whether the file is already processed.
40 * @var bool
41 */
42 private $processed;
43
44 /**
45 * The filename.
46 * @var string
47 */
48 private $filename;
49
50 /**
51 * Indicator, whether the file is an image.
52 * @var bool
53 */
54 private $isImage;
55
56 /**
57 * Indicator, whether the file can be displayed as an image.
58 * @var bool
59 */
60 public $viewableImage;
61
62 /**
63 * The filesize of the file.
64 * @var int
65 */
66 public $filesize;
67
68 /**
69 * The unique id for the file.
70 * @var string
71 */
72 private $uniqueId;
73
74 /**
75 * UploadFile constructor.
76 *
77 * @param string $location
78 * @param string $filename
79 * @param bool $viewableImage
80 * @param bool $processed
81 * @param bool $detectSvgAsImage
82 */
83 public function __construct(
84 $location,
85 $filename,
86 $viewableImage = true,
87 $processed = false,
88 $detectSvgAsImage = false
89 ) {
90 if (!\file_exists($location)) {
91 throw new \InvalidArgumentException("File '" . $location . "' could not be found.");
92 }
93
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;
101
102 if (
103 @\getimagesize($location) !== false || ($detectSvgAsImage && \in_array(FileUtil::getMimeType($location), [
104 'image/svg',
105 'image/svg+xml',
106 ]))
107 ) {
108 $this->isImage = true;
109 }
110 }
111
112 /**
113 * Returns true, whether this file is an image.
114 *
115 * @return bool
116 */
117 public function isImage()
118 {
119 return $this->isImage;
120 }
121
122 /**
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.
125 *
126 * @return string|null
127 */
128 public function getImage()
129 {
130 if (!$this->isImage() || !$this->viewableImage) {
131 return;
132 }
133
134 if ($this->processed) {
135 if ($this->imageLink === null) {
136 // try to guess path
137 return \str_replace(WCF_DIR, WCF::getPath(), $this->location);
138 } else {
139 return $this->imageLink;
140 }
141 } else {
142 $imageData = @\getimagesize($this->location);
143
144 if ($imageData !== false) {
145 return 'data:' . $imageData['mime'] . ';base64,' . \base64_encode(\file_get_contents($this->location));
146 }
147
148 if (
149 $this->detectSvgAsImage && \in_array(FileUtil::getMimeType($this->location), [
150 'image/svg',
151 'image/svg+xml',
152 ])
153 ) {
154 return 'data:image/svg+xml;base64,' . \base64_encode(\file_get_contents($this->location));
155 }
156
157 throw new \LogicException('File is an image, but can not be rendered.');
158 }
159 }
160
161 /**
162 * Returns the location of the file.
163 *
164 * @return string
165 */
166 public function getLocation()
167 {
168 return $this->location;
169 }
170
171 /**
172 * Returns the filename of the file.
173 *
174 * @return string
175 */
176 public function getFilename()
177 {
178 return $this->filename;
179 }
180
181 /**
182 * Returns the unique file id for the file. It is used to identify the certain file.
183 * @return string
184 */
185 public function getUniqueFileId()
186 {
187 return $this->uniqueId;
188 }
189
190 /**
191 * Sets the new location of the file, after it is processed and
192 * sets the `processed` attribute to true.
193 *
194 * @param string $newLocation
195 */
196 public function setProcessed($newLocation)
197 {
198 if (!\file_exists($newLocation)) {
199 throw new \InvalidArgumentException("File '" . $newLocation . "' could not be found.");
200 }
201
202 $this->location = $newLocation;
203 $this->processed = true;
204 }
205
206 /**
207 * Sets the new image link of the file for processed files.
208 *
209 * @param string $link
210 */
211 public function setImageLink($link)
212 {
213 $this->imageLink = $link;
214 }
215
216 /**
217 * Returns true, if the file is already processed.
218 *
219 * @return bool
220 */
221 public function isProcessed()
222 {
223 return $this->processed;
224 }
225
226 /**
227 * Returns icon name for this attachment.
228 *
229 * @return string
230 */
231 public function getIconName()
232 {
233 if ($iconName = FileUtil::getIconNameByFilename($this->filename)) {
234 return 'file-' . $iconName . '-o';
235 }
236
237 return 'paperclip';
238 }
239 }