1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
file.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Web\WebPacker\Output;
4
5use Bitrix\Main\Config\Option;
6use Bitrix\Main\Error;
7use Bitrix\Main\SystemException;
8use Bitrix\Main\Web\WebPacker;
9use Bitrix\Main\Web\MimeType;
10
16class File extends Base
17{
18 protected ?int $id = null;
19 protected $moduleId;
20 protected $uploadDir;
21 protected $dir;
22 protected $name;
23 protected $type;
24 protected $content;
25
32 public static function removeById($id)
33 {
34 \CFile::Delete($id);
35 }
36
43 public function output(WebPacker\Builder $builder)
44 {
45 if (!$this->moduleId)
46 {
47 throw new SystemException('Module ID is empty.');
48 }
49 if (!$this->name)
50 {
51 throw new SystemException('File name is empty.');
52 }
53
54 $content = $builder->stringify();
55 $id = $this->saveFile($content);
56
57 $result = (new Result())->setId($id)->setContent($content);
58 if (!$id)
59 {
60 $result->addError(new Error('Empty file ID.'));
61 }
62
63 return $result;
64 }
65
72 public function setContent($content)
73 {
74 $this->content = $content;
75 return $this;
76 }
77
84 public function setId(?int $id): static
85 {
86 $this->id = $id;
87
88 return $this;
89 }
90
96 public function getId(): ?int
97 {
98 return $this->id;
99 }
100
107 public function setModuleId($moduleId)
108 {
109 $this->moduleId = $moduleId;
110 return $this;
111 }
112
119 public function setUploadDir($uploadDir)
120 {
121 $this->uploadDir = $uploadDir;
122 return $this;
123 }
124
131 public function setDir($dir)
132 {
133 $this->dir = $dir;
134 return $this;
135 }
136
143 public function setName($name)
144 {
145 $this->name = $name;
146 return $this;
147 }
148
155 public function setType($type)
156 {
157 $this->type = $type;
158 return $this;
159 }
160
166 public function remove()
167 {
168 if (!$this->id)
169 {
170 return;
171 }
172
173 static::removeById($this->id);
174 }
175
181 public function getUri()
182 {
183 $uri = '';
184 if (!$this->id)
185 {
186 return $uri;
187 }
188
189 $file = \CFile::GetFileArray($this->id);
190 if (!$file)
191 {
192 return $uri;
193 }
194
195 $uri = $file['SRC'];
196 if ($uri && !preg_match('#^(https?://)#', $uri))
197 {
199 }
200
201 if ($uri)
202 {
203 return $uri;
204 }
205
206 $uploadDir = $this->uploadDir ?: Option::get("main", "upload_dir", "upload");
208 '/' . $uploadDir .
209 '/' . $file['SUBDIR'] .
210 '/' . $file['FILE_NAME']
211 ;
212 }
213
214 protected function saveFile($content)
215 {
216 $this->remove();
217
219 if (!$type && $this->name)
220 {
221 $type = static::getMimeTypeByFileName($this->name);
222 }
223
224 $fileArray = [
225 'MODULE_ID' => $this->moduleId,
226 'name' => $this->name,
227 'content' => $content
228 ];
229
230 if ($type)
231 {
232 $fileArray['type'] = $type;
233 }
234
235 $this->id = \CFile::SaveFile(
236 $fileArray,
237 $this->moduleId,
238 false,
239 false,
240 $this->dir ?: ''
241 );
242
243 return $this->id;
244 }
245
246 protected static function getMimeTypeByFileName($fileName)
247 {
248 $extension = mb_strtolower(getFileExtension($fileName));
250 if (isset($list[$extension]))
251 {
252 return $list[$extension];
253 }
254
255 return 'text/plain';
256 }
257}
$type
Определения options.php:106
Определения error.php:15
static getMimeTypeList()
Определения mimetype.php:235
static getDefaultSiteUri()
Определения builder.php:288
setId(?int $id)
Определения file.php:84
setContent($content)
Определения file.php:72
saveFile($content)
Определения file.php:214
output(WebPacker\Builder $builder)
Определения file.php:43
setUploadDir($uploadDir)
Определения file.php:119
static getMimeTypeByFileName($fileName)
Определения file.php:246
setModuleId($moduleId)
Определения file.php:107
static removeById($id)
Определения file.php:32
$result
Определения get_property_values.php:14
$moduleId
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
$name
Определения menu_edit.php:35
Определения Image.php:9
$fileName
Определения quickway.php:305