12 private ?\CCloudStorageBucket $bucket =
null;
18 if (!$file->isExists())
23 if (mb_strpos($file->getPhysicalPath(), \CTempFile::getAbsoluteRoot()) !== 0)
28 if (!copy($chunk->
getFile()->getPhysicalPath(), $tempFilePath))
33 $newFile =
new IO\File($tempFilePath);
34 if (!$newFile->isExists())
51 $bucket = self::findBucketForFile($chunk, $controller);
56 $appendResult = $tempFile->appendToCloud($chunk);
57 if (!$appendResult->isSuccess())
62 return $result->addErrors($appendResult->getErrors());
69 if (!$file->rename($localTempDir))
78 $result->setData([
'tempFile' => $tempFile]);
86 $tempFile->setFilename($chunk->
getName());
87 $tempFile->setMimetype($chunk->
getType());
89 $tempFile->setReceivedSize($chunk->
getSize());
90 $tempFile->setWidth($chunk->
getWidth());
91 $tempFile->setHeight($chunk->
getHeight());
93 $tempFile->setController($controller->
getName());
101 $path = $chunk->
getFile()->getPhysicalPath();
102 $tempRoot = \CTempFile::getAbsoluteRoot();
103 $path = mb_substr($path, mb_strlen($tempRoot));
106 $tempFile->setPath($path);
110 $tempFile->setCloud(
true);
111 $tempFile->setBucketId($bucket->ID);
123 if ($chunk->
getEndRange() < $this->getReceivedSize())
134 if ($this->getReceivedSize() + $chunk->
getSize() > $this->getSize())
139 $result = $this->
isCloud() ? $this->appendToCloud($chunk) : $this->appendToFile($chunk);
140 if ($result->isSuccess())
142 $this->increaseReceivedSize($chunk->
getSize());
153 $fileAbsolutePath = $this->getAbsolutePath();
155 $fileId = \CFile::saveFile(
157 'name' => $this->getFilename(),
158 'tmp_name' => $fileAbsolutePath,
159 'type' => $this->getMimetype(),
161 'width' => $this->getWidth(),
162 'height' => $this->getHeight(),
163 'size' => $this->getSize(),
179 $this->setFileId($fileId);
180 $this->setUploaded(
true);
184 $this->removeActualTempFile();
191 return $this->getCloud() && $this->getBucketId() > 0;
196 $this->customData->set(
'deleteBFile',
false);
202 $this->removeActualTempFile();
205 \CFile::delete($this->getFileId());
209 private function removeActualTempFile(): bool
211 if ($this->getDeleted())
219 $bucket = $this->getBucket();
222 $success = $bucket->deleteFile($this->getPath());
227 $success = IO\File::deleteFile($this->getAbsolutePath());
232 $this->setDeleted(
true);
239 private function getAbsoluteCloudPath(): ?string
241 $bucket = $this->getBucket();
247 return $bucket->getFileSRC($this->getPath());
250 private function getAbsoluteLocalPath(): string
252 return \CTempFile::getAbsoluteRoot() . $this->getPath();
255 private function getAbsolutePath(): ?string
259 return $this->getAbsoluteCloudPath();
262 return $this->getAbsoluteLocalPath();
265 private function appendToFile(Chunk $chunk): Result
267 $result =
new Result();
268 $file =
new IO\File($this->getAbsoluteLocalPath());
270 if ($chunk->isFirst() || !$file->isExists())
275 if ($chunk->getEndRange() < $file->getSize())
281 if (!$chunk->getFile()->isExists())
286 if ($file->putContents($chunk->getFile()->getContents(),
IO\File::APPEND) ===
false)
294 private function appendToCloud(Chunk $chunk): Result
296 $result =
new Result();
297 $bucket = $this->getBucket();
303 $minUploadSize = $bucket->getService()->getMinUploadPartSize();
304 if ($chunk->getSize() < $minUploadSize && !$chunk->isLast())
306 $postMaxSize = \CUtil::unformat(ini_get(
'post_max_size'));
307 $uploadMaxFileSize = \CUtil::unformat(ini_get(
'upload_max_filesize'));
309 return $result->addError(
313 'chunkSize' => $chunk->getSize(),
314 'minUploadSize' => $minUploadSize,
315 'postMaxSize' => $postMaxSize,
316 'uploadMaxFileSize' => $uploadMaxFileSize,
322 $cloudUpload = new \CCloudStorageUpload($this->getPath());
323 if (!$cloudUpload->isStarted() && !$cloudUpload->start($bucket->ID, $chunk->getFileSize(), $chunk->getType()))
328 if ($cloudUpload->getPos() === doubleval($chunk->getEndRange() + 1))
331 if ($chunk->isLast() && !$cloudUpload->finish())
339 $fileContent = $chunk->getFile()->isExists() ? $chunk->getFile()->getContents() :
false;
340 if ($fileContent ===
false)
347 while ($cloudUpload->hasRetries())
349 if ($cloudUpload->next($fileContent))
364 if ($chunk->isLast() && !$cloudUpload->finish())
373 private function increaseReceivedSize(
int $bytes): void
375 $receivedSize = $this->getReceivedSize();
376 $this->setReceivedSize($receivedSize + $bytes);
380 private static function findBucketForFile(Chunk $chunk, UploaderController $controller): ?\CCloudStorageBucket
382 if (!Loader::includeModule(
'clouds'))
387 $bucket = \CCloudStorage::findBucketForFile(
389 'FILE_SIZE' => $chunk->getFileSize(),
390 'MODULE_ID' => $controller->getCommitOptions()->getModuleId(),
395 if (!$bucket || !$bucket->init())
405 $directory = \CTempFile::getDirectoryName(
409 Security\Random::getString(32),
415 IO\Directory::createDirectory($directory);
418 $tempName = md5(mt_rand() . mt_rand());
420 return $directory . $tempName;
425 $tmpFilePath = \CTempFile::getFileName(
'file-uploader' . uniqid(md5(mt_rand() . mt_rand()),
true));
426 $directory = IO\Path::getDirectory($tmpFilePath);
429 IO\Directory::createDirectory($directory);
437 $directory = \CCloudTempFile::getDirectoryName(
442 Security\Random::getString(32),
446 $tempName = md5(mt_rand() . mt_rand());
448 return $directory . $tempName;
451 private function getBucket(): ?\CCloudStorageBucket
453 if ($this->bucket !==
null)
455 return $this->bucket;
458 if (!$this->getBucketId() || !Loader::includeModule(
'clouds'))
463 $bucket = new \CCloudStorageBucket($this->getBucketId());
466 $this->bucket = $bucket;
469 return $this->bucket;
static isDirectoryExists($path)