Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
file.php
1
<?php
2
3
namespace
Bitrix\Main\Web\WebPacker\Output
;
4
5
use
Bitrix\Main\Config\Option
;
6
use
Bitrix\Main\Error
;
7
use
Bitrix\Main\SystemException
;
8
use
Bitrix\Main\Web\WebPacker
;
9
use
Bitrix\Main\Web\MimeType
;
10
16
class
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));
248
$list =
MimeType::getMimeTypeList
();
249
if
(isset($list[$extension]))
250
{
251
return
$list[$extension];
252
}
253
254
return
'text/plain'
;
255
}
256
}
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\IO\File
Definition
file.php:7
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Web\MimeType
Definition
mimetype.php:7
Bitrix\Main\Web\MimeType\getMimeTypeList
static getMimeTypeList()
Definition
mimetype.php:233
Bitrix\Main\Web\WebPacker\Builder
Definition
builder.php:16
Bitrix\Main\Web\WebPacker\Output\Base
Definition
base.php:13
Bitrix\Main\Web\WebPacker\Output\File\setContent
setContent($content)
Definition
file.php:72
Bitrix\Main\Web\WebPacker\Output\File\getId
getId()
Definition
file.php:95
Bitrix\Main\Web\WebPacker\Output\File\$dir
$dir
Definition
file.php:21
Bitrix\Main\Web\WebPacker\Output\File\saveFile
saveFile($content)
Definition
file.php:213
Bitrix\Main\Web\WebPacker\Output\File\output
output(WebPacker\Builder $builder)
Definition
file.php:43
Bitrix\Main\Web\WebPacker\Output\File\getUri
getUri()
Definition
file.php:180
Bitrix\Main\Web\WebPacker\Output\File\setUploadDir
setUploadDir($uploadDir)
Definition
file.php:118
Bitrix\Main\Web\WebPacker\Output\File\setName
setName($name)
Definition
file.php:142
Bitrix\Main\Web\WebPacker\Output\File\setDir
setDir($dir)
Definition
file.php:130
Bitrix\Main\Web\WebPacker\Output\File\$content
$content
Definition
file.php:24
Bitrix\Main\Web\WebPacker\Output\File\getMimeTypeByFileName
static getMimeTypeByFileName($fileName)
Definition
file.php:245
Bitrix\Main\Web\WebPacker\Output\File\setModuleId
setModuleId($moduleId)
Definition
file.php:106
Bitrix\Main\Web\WebPacker\Output\File\$uploadDir
$uploadDir
Definition
file.php:20
Bitrix\Main\Web\WebPacker\Output\File\setId
setId($id)
Definition
file.php:84
Bitrix\Main\Web\WebPacker\Output\File\$type
$type
Definition
file.php:23
Bitrix\Main\Web\WebPacker\Output\File\$name
$name
Definition
file.php:22
Bitrix\Main\Web\WebPacker\Output\File\removeById
static removeById($id)
Definition
file.php:32
Bitrix\Main\Web\WebPacker\Output\File\$moduleId
$moduleId
Definition
file.php:19
Bitrix\Main\Web\WebPacker\Output\File\setType
setType($type)
Definition
file.php:154
Bitrix\Main\Web\WebPacker\Output\File\$id
$id
Definition
file.php:18
Bitrix\Main\Web\WebPacker\Output
Definition
base.php:3
Bitrix\Main\Web\WebPacker
Definition
builder.php:3
modules
main
lib
web
webpacker
output
file.php
Создано системой
1.10.0