Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2
4
10
16class File extends Base
17{
18 protected $id;
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($id)
85 {
86 $this->id = $id;
87 return $this;
88 }
89
95 public function getId()
96 {
97 return $this->id;
98 }
99
106 public function setModuleId($moduleId)
107 {
108 $this->moduleId = $moduleId;
109 return $this;
110 }
111
118 public function setUploadDir($uploadDir)
119 {
120 $this->uploadDir = $uploadDir;
121 return $this;
122 }
123
130 public function setDir($dir)
131 {
132 $this->dir = $dir;
133 return $this;
134 }
135
142 public function setName($name)
143 {
144 $this->name = $name;
145 return $this;
146 }
147
154 public function setType($type)
155 {
156 $this->type = $type;
157 return $this;
158 }
159
165 public function remove()
166 {
167 if (!$this->id)
168 {
169 return;
170 }
171
172 static::removeById($this->id);
173 }
174
180 public function getUri()
181 {
182 $uri = '';
183 if (!$this->id)
184 {
185 return $uri;
186 }
187
188 $file = \CFile::GetFileArray($this->id);
189 if (!$file)
190 {
191 return $uri;
192 }
193
194 $uri = $file['SRC'];
195 if ($uri && !preg_match('#^(https?://)#', $uri))
196 {
197 return WebPacker\Builder::getDefaultSiteUri() . $uri;
198 }
199
200 if ($uri)
201 {
202 return $uri;
203 }
204
205 $uploadDir = $this->uploadDir ?: Option::get("main", "upload_dir", "upload");
206 return WebPacker\Builder::getDefaultSiteUri() .
207 '/' . $uploadDir .
208 '/' . $file['SUBDIR'] .
209 '/' . $file['FILE_NAME']
210 ;
211 }
212
213 protected function saveFile($content)
214 {
215 $this->remove();
216
217 $type = $this->type;
218 if (!$type && $this->name)
219 {
220 $type = static::getMimeTypeByFileName($this->name);
221 }
222
223 $fileArray = [
224 'MODULE_ID' => $this->moduleId,
225 'name' => $this->name,
226 'content' => $content
227 ];
228
229 if ($type)
230 {
231 $fileArray['type'] = $type;
232 }
233
234 $this->id = \CFile::SaveFile(
235 $fileArray,
236 $this->moduleId,
237 false,
238 false,
239 $this->dir ?: ''
240 );
241
242 return $this->id;
243 }
244
245 protected static function getMimeTypeByFileName($fileName)
246 {
247 $extension = mb_strtolower(getFileExtension($fileName));
249 if (isset($list[$extension]))
250 {
251 return $list[$extension];
252 }
253
254 return 'text/plain';
255 }
256}
output(WebPacker\Builder $builder)
Definition file.php:43
static getMimeTypeByFileName($fileName)
Definition file.php:245