1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
storage.php
См. документацию.
1
<?php
2
namespace
Bitrix\Main\UI\Uploader;
3
use Bitrix\Main\Error;
4
use Bitrix\Main\Result;
5
use Bitrix\Main\Localization\Loc;
6
use Bitrix\Main\Loader;
7
8
interface
Storable
9
{
14
public
function
copy
(
$path
,
array
$file);
15
}
16
17
class
Storage
implements
Storable
18
{
19
protected
$path
=
""
;
20
21
protected
static
$lastId
=
null
;
22
protected
static
$descriptor
=
null
;
23
24
function
__construct
()
25
{
26
}
27
28
private
static
function
flush()
29
{
30
if
(!is_null(self::$descriptor))
31
{
32
@fflush(self::$descriptor);
33
@flock(self::$descriptor, LOCK_UN);
34
@fclose(self::$descriptor);
35
self::$descriptor =
null
;
36
}
37
}
38
function
__destruct
()
39
{
40
self::flush();
41
}
42
48
public
function
copy
(
$path
,
array
$file)
49
{
50
$result
=
new
Result
();
51
$directory =
\CBXVirtualIo::GetInstance
()->getDirectory(
$path
);
52
53
$newFile = $directory->GetPathWithName().
"/"
.$file[
"code"
];
54
$result
->setData(
array
(
55
"size"
=> $file[
"size"
],
56
"tmp_name"
=> $newFile,
57
"type"
=> $file[
"type"
]
58
));
59
if
(mb_substr($newFile, -mb_strlen($file[
'tmp_name'
])) == $file[
'tmp_name'
])
60
{
61
}
62
elseif
(!$directory->create())
63
{
64
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_TemporaryDirectoryIsNotCreated"
),
"BXU347.1"
));
65
}
66
elseif
(array_key_exists(
'tmp_url'
, $file))
67
{
68
if
(!((!file_exists($newFile) || @unlink($newFile)) &&
File::http
()->download($file[
'tmp_url'
], $newFile) !==
false
))
69
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileIsNotUploaded"
),
"BXU347.2.1"
));
70
}
71
elseif
(!file_exists($file[
'tmp_name'
]))
72
{
73
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileIsNotUploaded"
),
"BXU347.2.2"
));
74
}
75
elseif
(array_key_exists(
'start'
, $file))
76
{
77
$result
= $this->
copyChunk
($newFile, $file);
78
}
79
elseif
(!((!file_exists($newFile) || @unlink($newFile)) && move_uploaded_file($file[
'tmp_name'
], $newFile)))
80
{
81
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileIsNotUploaded"
),
"BXU347.2.4"
));
82
}
83
else
84
{
85
$result
->setData(
array
(
86
"size"
=> filesize($newFile),
87
"tmp_name"
=> $newFile,
88
"type"
=> ($file[
"type"
] ?: \CFile::GetContentType($newFile))
89
));
90
}
91
return
$result
;
92
}
93
98
public
function
copyChunk
(
$path
,
array
$chunk)
99
{
100
$result
=
new
Result
();
101
if
(is_null(self::$descriptor) || self::$lastId !=
$path
)
102
{
103
self::flush();
104
self::$descriptor = $fdst = fopen(
$path
,
'cb'
);
105
@chmod(
$path
, BX_FILE_PERMISSIONS);
106
}
107
else
108
$fdst = self::$descriptor;
109
110
if
(!$fdst)
111
{
112
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_TemporaryFileIsNotCreated"
),
"BXU349.1"
));
113
}
114
else
if
(!flock($fdst, LOCK_EX))
115
{
116
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileIsLocked"
),
"BXU349.100"
));
117
}
118
else
119
{
120
$buff = 4096;
121
if
(($fsrc = fopen($chunk[
'tmp_name'
],
'r'
)))
122
{
123
fseek($fdst, $chunk[
"start"
], SEEK_SET);
124
while
(!feof($fsrc) && (
$data
= fread($fsrc, $buff)))
125
{
126
if
(
$data
!==
''
)
127
{
128
fwrite($fdst,
$data
);
129
}
130
else
131
{
132
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FilePartCanNotBeRead"
),
"BXU349.2"
));
133
break
;
134
}
135
}
136
fclose($fsrc);
137
unlink($chunk[
'tmp_name'
]);
138
}
139
else
140
{
141
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FilePartCanNotBeOpened"
),
"BXU349.3"
));
142
}
143
}
144
if
(!
$result
->isSuccess())
145
{
146
self::flush();
147
}
148
149
$result
->setData(
array
(
150
"size"
=> $chunk[
"~size"
],
151
"tmp_name"
=>
$path
,
152
"type"
=> $chunk[
"type"
]
153
));
154
155
return
$result
;
156
}
157
158
public
function
flushDescriptor
()
159
{
160
self::flush();
161
}
162
}
163
164
class
CloudStorage
extends
Storage
implements
Storable
165
{
166
protected
$moduleId
=
"main"
;
167
function
__construct
(
$params
)
168
{
169
if
(!
Loader::includeModule
(
"clouds"
))
170
throw
new \Bitrix\Main\NotImplementedException();
171
if
(is_array(
$params
))
172
{
173
$params
= array_change_key_case(
$params
, CASE_LOWER);
174
$this->moduleId = (
$params
[
"moduleid"
] ?:
$this->moduleId
);
175
}
176
}
177
182
private
function
findBucket($file)
183
{
184
185
$bucket = \CCloudStorage::findBucketForFile(
array
(
'FILE_SIZE'
=> $file[
'size'
],
'MODULE_ID'
=> $this->moduleId), $file[
"name"
] ??
''
);
186
if
(!$bucket || !$bucket->init())
187
{
188
return
null
;
189
}
190
return
$bucket;
191
}
192
protected
function
moveIntoCloud
(\
CCloudStorageBucket
$bucket,
$path
, $file)
193
{
194
$result
=
new
Result
();
195
$absPath = \CTempFile::getAbsoluteRoot();
196
$relativePath =
$path
;
197
if
(str_starts_with(
$path
, $absPath) && mb_strpos(
$path
,
"/bxu/"
) > 0)
198
$relativePath = mb_substr(
$path
, mb_strpos(
$path
,
"/bxu/"
));
199
$subdir = explode(
"/"
, trim($relativePath,
"/"
));
200
$filename
= array_pop($subdir);
201
if
(!isset(\
Bitrix
\
Main
\
Application::getInstance
()->getSession()[
"upload_tmp"
]))
202
{
203
\Bitrix\Main\Application::getInstance
()->getSession()[
"upload_tmp"
] =
array
();
204
}
205
206
if
(!isset(\
Bitrix
\
Main
\
Application::getInstance
()->getSession()[
"upload_tmp"
][
$path
]))
207
{
208
$relativePath =
\Bitrix\Main\Application::getInstance
()->getSession()[
"upload_tmp"
][
$path
] =
\CCloudTempFile::GetDirectoryName
($bucket, 12).$filename;
209
}
210
else
211
{
212
$relativePath =
\Bitrix\Main\Application::getInstance
()->getSession()[
"upload_tmp"
][
$path
];
213
}
214
215
$upload = new \CCloudStorageUpload($relativePath);
216
$finished =
false
;
217
if
(!$upload->isStarted() && !$upload->start($bucket->ID, $file[
"size"
], $file[
"type"
]))
218
{
219
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileTransferIntoTheCloudIsFailed"
),
"BXU346.2"
));
220
}
221
else
if
(!(
$fileContent
= \
Bitrix
\
Main
\
IO
\File::getFileContents($file[
"tmp_name"
])))
222
{
223
$result
->addError(
new
Error
(Loc::getMessage(
"BXU_FileIsFailedToRead"
),
"BXU346.3"
));
224
}
225
else
226
{
227
$fails = 0;
228
$success
=
false
;
229
while
($upload->hasRetries())
230
{
231
if
(method_exists($upload,
"part"
) && $upload->part(
$fileContent
, ($file[
"number"
] ?? 0)) ||
232
!method_exists($upload,
"part"
) && $upload->next(
$fileContent
))
233
{
234
$success
=
true
;
235
break
;
236
}
237
$fails++;
238
}
239
if
(!
$success
)
240
{
241
$result
->addError(
new
Error
(
"Could not upload file for {$fails} times."
,
"BXU346.4"
));
242
}
243
else
if
(isset($file[
"count"
]) && $upload->GetPartCount() < $file[
"count"
])
244
{
245
}
246
else
if
(!$upload->finish())
247
{
248
$result
->addError(
new
Error
(
"Could not resume file transfer."
,
"BXU346.5"
));
249
}
250
else
251
{
252
$finished =
true
;
253
}
254
}
255
256
$result
->setData(
array
(
257
"tmp_name"
=> $bucket->getFileSRC($relativePath),
258
"size"
=> $file[
"size"
],
259
"type"
=> $file[
"type"
],
260
"finished"
=> $finished
261
));
262
return
$result
;
263
}
264
265
public
function
copy
(
$path
,
array
$file)
266
{
267
$result
= parent::copy(
$path
, $file);
268
if
(
$result
->isSuccess())
269
{
270
if
(!array_key_exists(
'start'
, $file))
271
{
272
$res
=
$result
->getData();
273
$file[
"tmp_name"
] =
$res
[
"tmp_name"
];
274
$file[
"size"
] =
$res
[
"size"
];
275
$file[
"type"
] =
$res
[
"type"
];
276
$info
= (new \Bitrix\Main\File\Image($file[
"tmp_name"
]))->getInfo();
277
if
(
$info
)
278
{
279
$file[
"width"
] =
$info
->getWidth();
280
$file[
"height"
] =
$info
->getHeight();
281
}
282
else
283
{
284
$file[
"width"
] = 0;
285
$file[
"height"
] = 0;
286
}
287
if
($bucket = $this->findBucket($file))
288
{
289
unset($file[
"count"
]);
290
if
(($r = $this->
moveIntoCloud
($bucket, $file[
"tmp_name"
], $file)) && $r->isSuccess())
291
{
292
$res
= $r->getData();
293
$result
->setData(
array
(
294
"size"
=> $file[
"size"
],
295
"file_size"
=> $file[
"size"
],
296
"tmp_name"
=>
$res
[
"tmp_name"
],
297
"type"
=> $file[
"type"
],
298
"width"
=> $file[
"width"
],
299
"height"
=> $file[
"height"
],
300
"bucketId"
=> $bucket->ID
301
));
302
}
303
if
($r->getErrors())
304
{
305
$result
->addErrors($r->getErrors());
306
}
307
@unlink(
$path
);
308
}
309
}
310
else
if
($file[
"start"
] <= 0)
311
{
312
$res
=
$result
->getData();
313
if
((
$info
= (
new
\
Bitrix
\
Main
\
File
\Image($file[
"tmp_name"
]))->getInfo()))
314
{
315
$file[
"width"
] =
$info
->getWidth();
316
$file[
"height"
] =
$info
->getHeight();
317
$result
->setData(array_merge(
$res
, [
"width"
=> $file[
"width"
],
"height"
=> $file[
"height"
]]));
318
}
319
}
320
}
321
return
$result
;
322
}
323
328
public
function
copyChunk
(
$path
,
array
$file)
329
{
330
if
($bucket = $this->findBucket(
array
(
331
"name"
=> $file[
"~name"
],
332
"size"
=> $file[
"~size"
],
333
)))
334
{
335
if
((
$result
= $this->
moveIntoCloud
($bucket,
$path
, array_merge($file,
array
(
"size"
=> $file[
"~size"
])))) &&
336
$result
->isSuccess() && (
$res
=
$result
->getData()) &&
$res
[
"finished"
] ===
true
)
337
{
338
$res
=
$result
->getData();
339
$result
->setData(
array
(
340
"size"
=> $file[
"~size"
],
341
"file_size"
=> $file[
"~size"
],
342
"tmp_name"
=>
$res
[
"tmp_name"
],
343
"type"
=> $file[
"type"
],
344
"bucketId"
=> $bucket->ID
345
));
346
}
347
}
348
else
349
{
350
$result
= parent::copyChunk(
$path
, $file);
351
}
352
return
$result
;
353
}
354
360
public
static
function
checkBucket
($id)
361
{
362
$res
=
false
;
363
if
(
Loader::includeModule
(
"clouds"
))
364
{
365
$r =
\CCloudStorageBucket::GetAllBuckets
();
366
$res
= (is_array($r) && array_key_exists($id, $r));
367
}
368
return
$res
;
369
}
370
}
$path
$path
Определения
access_edit.php:21
Bitrix\Main\Application\getInstance
static getInstance()
Определения
application.php:98
Bitrix\Main\Error
Определения
error.php:15
Bitrix\Main\Loader\includeModule
static includeModule($moduleName)
Определения
loader.php:67
Bitrix\Main\ORM\Data\Result
Определения
result.php:16
Bitrix\Main\UI\Uploader\CloudStorage
Определения
storage.php:165
Bitrix\Main\UI\Uploader\CloudStorage\checkBucket
static checkBucket($id)
Определения
storage.php:360
Bitrix\Main\UI\Uploader\CloudStorage\moveIntoCloud
moveIntoCloud(\CCloudStorageBucket $bucket, $path, $file)
Определения
storage.php:192
Bitrix\Main\UI\Uploader\CloudStorage\copyChunk
copyChunk($path, array $file)
Определения
storage.php:328
Bitrix\Main\UI\Uploader\CloudStorage\__construct
__construct($params)
Определения
storage.php:167
Bitrix\Main\UI\Uploader\CloudStorage\copy
copy($path, array $file)
Определения
storage.php:265
Bitrix\Main\UI\Uploader\CloudStorage\$moduleId
$moduleId
Определения
storage.php:166
Bitrix\Main\UI\Uploader\File\http
static http()
Определения
file.php:576
Bitrix\Main\UI\Uploader\Storage
Определения
storage.php:18
Bitrix\Main\UI\Uploader\Storage\__construct
__construct()
Определения
storage.php:24
Bitrix\Main\UI\Uploader\Storage\$path
$path
Определения
storage.php:19
Bitrix\Main\UI\Uploader\Storage\$descriptor
static $descriptor
Определения
storage.php:22
Bitrix\Main\UI\Uploader\Storage\__destruct
__destruct()
Определения
storage.php:38
Bitrix\Main\UI\Uploader\Storage\$lastId
static $lastId
Определения
storage.php:21
Bitrix\Main\UI\Uploader\Storage\flushDescriptor
flushDescriptor()
Определения
storage.php:158
Bitrix\Main\UI\Uploader\Storage\copyChunk
copyChunk($path, array $chunk)
Определения
storage.php:98
Bitrix\Main\UI\Uploader\Storage\copy
copy($path, array $file)
Определения
storage.php:48
CBXVirtualIo\GetInstance
static GetInstance()
Определения
virtual_io.php:60
CCloudStorageBucket
Определения
storage_bucket.php:21
CCloudStorageBucket\GetAllBuckets
static GetAllBuckets()
Определения
storage_bucket.php:654
CCloudTempFile\GetDirectoryName
static GetDirectoryName($obBucket, $hours_to_keep_files=0, $subdir='')
Определения
temp_file.php:85
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
$filename
$filename
Определения
file_edit.php:47
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$fileContent
$fileContent
Определения
file_property.php:47
$res
$res
Определения
filter_act.php:7
$result
$result
Определения
get_property_values.php:14
Bitrix\Main\UI\Uploader\Storable
Определения
storage.php:9
Bitrix\Main\UI\Uploader\Storable\copy
copy($path, array $file)
$success
$success
Определения
mail_entry.php:69
$info
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения
backup.php:924
Bitrix\Main\File
Определения
Image.php:9
Bitrix\Main\IO
Определения
directory.php:3
Bitrix\Main
Bitrix
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$params
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения
template.php:799
bitrix
modules
main
lib
ui
uploader
storage.php
Создано системой
1.14.0