33 $hash = self::initHash(array(
"id" => $file[
"id"],
"name" => $file[
"name"]));
37 "uploadStatus" =>
null,
38 "executeStatus" =>
null,
39 "name" => $file[
"name"],
40 "type" => $file[
"type"],
41 "size" => $file[
"size"],
47 $this->
package = $package;
52 $eventName =
"onFileIsContinued";
56 $eventName =
"onFileIsStarted";
64 if (!ExecuteModuleEventEx($event, array($this->
getHash(), &$this->data, &$error)))
151 $code = $file[
"code"];
153 if ($code !==
"default" && !array_key_exists($code, $copies))
155 $result->addError(
new Error(
"The copy name is not in the list."));
161 else if (isset($file[
"chunkId"]))
168 "type" => $file[
"type"],
169 "uploadStatus" =>
"inprogress",
170 "count" => $file[
"count"],
171 "chunks" => array());
172 $file[
"chunks"] = $info[
"chunks"];
173 $r = $storage->
copy($this->package->getPath().$this->getHash(), $file);
174 if (!$r->isSuccess())
176 $result->addError($r->getErrorCollection()->current());
180 $info[
"chunks"][$file[
"chunkId"]] = array(
181 "size" => $file[
"size"],
182 "number" => $file[
"number"],
183 "start" => $file[
"start"],
184 "error" => $file[
"error"]
186 $file[
"uploadStatus"] =
"uploaded";
187 $data = $r->getData();
188 if (count($info[
"chunks"]) == $info[
"count"])
191 $data[
"code"] = $info[
"code"];
192 $data[
"uploadStatus"] =
"uploaded";
193 $info =
$data + array_intersect_key($info, [
"width" =>
"",
"height" =>
""]);
197 $info += array_intersect_key(
$data, [
"width" =>
"",
"height" =>
""]);
200 $storage->flushDescriptor();
205 $r = $storage->
copy($this->package->getPath().$this->getHash(), $file);
208 $data = $r->getData();
210 $data[
"code"] = $code;
211 $data[
"uploadStatus"] =
"uploaded";
216 $result->addError($r->getErrorCollection()->current());
219 if ($result->isSuccess())
222 if ($info[
"uploadStatus"] ==
"uploaded")
224 $info[
"url"] = $this->getUrl(
"view", $code);
225 $info[
"~url"] = $this->getUrl(
"view", $code, \COption::GetOptionString(
"main.fileinput",
"entryPointUrl",
"/bitrix/tools/upload.php"));
226 $info[
"sizeFormatted"] = \CFile::FormatSize($info[
"size"]);
227 foreach ($this->data[
"files"] as $k => $f)
229 if ($f[
"uploadStatus"] ==
"uploaded")
471 public static function viewFile($cid, $hash, $path)
475 if (mb_strpos($hash,
"_") > 0)
477 $copy = explode(
"_", $hash);
478 $hash = $copy[0]; $copy = $copy[1];
480 $copy = ($copy ?:
"default");
481 if (FileInputUtility::instance()->checkFile($cid, $hash))
483 $file = self::getFromCache($hash, $path);
484 $file = $file[
"files"][$copy];
490 if (mb_strpos(\CTempFile::GetAbsoluteRoot(), $docRoot) === 0)
491 \CFile::ViewByUser($file, array(
"content_type" => $file[
"type"]));
493 self::view($file, array(
"content_type" => $file[
"type"]));
522 $io = \CBXVirtualIo::GetInstance();
523 if (($tempRoot = \CTempFile::GetAbsoluteRoot()) && ($filePath = $tempRoot.$tmpName) && $io->FileExists($filePath))
525 $f = $io->GetFile($filePath);
526 $directory = $io->GetDirectory($f->GetPath());
527 $hash = $directory->GetName();
528 if (($cache = self::getFromCache($hash, $directory->GetPath().
"/")) && is_array($cache) &&
529 array_key_exists(
"files", $cache) && array_key_exists($f->getName(), $cache[
"files"]))
531 return $cache[
"files"][$f->getName()][
"~url"];
595 if ($file[
"error"] > 0)
596 $result->addError(
new Error(File::getUploadErrorMessage($file[
"error"]),
"BXU347.2.9".$file[
"error"]));
597 else if (array_key_exists(
"tmp_url", $file))
599 $url =
new Uri($file[
"tmp_url"]);
600 if ($url->getHost() ==
'' && ($tmp = \CFile::MakeFileArray($url->getPath())) && is_array($tmp))
602 $file = array_merge($tmp, $file);
604 else if ($url->getHost() <>
'' &&
605 self::http()->query(
"HEAD", $file[
"tmp_url"]) &&
606 self::http()->getStatus() ==
"200")
608 $file = array_merge($file, array(
609 "size" => self::http()->getHeaders()->
get(
"content-length"),
610 "type" => self::http()->getHeaders()->
get(
"content-type")
618 else if (isset($file[
'bucketId']) && !CloudStorage::checkBucket($file[
'bucketId']))
622 else if (!isset($file[
'bucketId']) && (!file_exists($file[
'tmp_name']) || (
623 (mb_substr($file[
"tmp_name"], 0, mb_strlen($params[
"path"])) !== $params[
"path"]) &&
624 !is_uploaded_file($file[
'tmp_name'])
631 if ($result->isSuccess())
633 $params[
"uploadMaxFilesize"] = $params[
"uploadMaxFilesize"] ?? 0;
634 $params[
"allowUploadExt"] = $params[
"allowUploadExt"] ??
false;
635 $params[
"allowUpload"] = $params[
"allowUpload"] ??
null;
637 if ($params[
"uploadMaxFilesize"] > 0 && $f->
getSize() > $params[
"uploadMaxFilesize"])
639 $error = GetMessage(
"FILE_BAD_SIZE").
" (".\CFile::FormatSize($f->
getSize()).
").";
644 $ff = array_merge($file, array(
"name" => $name));
645 if ($params[
"allowUpload"] ===
"I")
647 $error = \CFile::CheckFile($ff, $params[
"uploadMaxFilesize"],
"image/", \CFile::GetImageExtensions());
649 elseif ($params[
"allowUpload"] ===
"F" && $params[
"allowUploadExt"])
651 $error = \CFile::CheckFile($ff, $params[
"uploadMaxFilesize"],
false, $params[
"allowUploadExt"]);
655 $error = \CFile::CheckFile($ff, $params[
"uploadMaxFilesize"]);
660 $result->addError(
new Error($error,
"BXU347.3"));
662 if (preg_match(
"/^(.+?)\\.ch(\\d+)\\.(\\d+)\\.chs(\\d+)$/", $file[
"code"], $matches))
664 $file[
"code"] = $matches[1];
665 $file[
"number"] = $matches[2];
666 $file[
"start"] = $matches[3];
667 $file[
"count"] = $matches[4];
668 $file[
"chunkId"] = self::getChunkKey($file[
"count"], $file[
"number"]);
670 $file[
"~size"] = $f->
getSize();
671 $file[
"~name"] = $f->
getName();
672 $file[
"~type"] = $f->getType();
695 public static function createCanvas($source, $dest, $canvasParams = array(), $watermarkParams = array())
697 $watermark = (array_key_exists(
"watermark", $source) ? array() : $watermarkParams);
698 if (\CFile::ResizeImageFile(
702 BX_RESIZE_IMAGE_PROPORTIONAL,
704 $canvasParams[
"quality"],
708 $dest = array_merge($source, $dest);
709 if (array_key_exists(
"watermark", $source) || !empty($watermarkParams))
710 $dest[
"watermark"] =
true;
713 $dest[
"error"] = 348;
714 $dest[
"size"] = filesize($dest[
"tmp_name"]);
715 $dest[
"type"] = $dest[
"type"] ?: \CFile::GetContentType($dest[
"tmp_name"]);
716 $dest[
"sizeFormatted"] = \CFile::FormatSize($dest[
"size"]);
726 public static function view(array $fileData, $options = array())
728 if (!array_key_exists(
"tmp_name", $fileData) || empty($fileData[
"tmp_name"]))
734 $fastDownload = (\COption::GetOptionString(
'main',
'bx_fast_download',
'N') ==
'Y');
736 $attachment_name =
"";
737 $content_type = (array_key_exists(
"type", $fileData) && !empty($fileData[
"type"]) ? $fileData[
"type"] :
"");
742 if(is_array($options))
744 if(isset($options[
"content_type"]))
745 $content_type = $options[
"content_type"];
746 if(isset($options[
"specialchars"]))
747 $specialchars = $options[
"specialchars"];
748 if(isset($options[
"force_download"]))
749 $force_download = $options[
"force_download"];
750 if(isset($options[
"cache_time"]))
751 $cache_time = intval($options[
"cache_time"]);
752 if(isset($options[
"attachment_name"]))
753 $attachment_name = $options[
"attachment_name"];
759 $name = str_replace(array(
"\n",
"\r"),
'', $fileData[
"name"]);
761 if ($attachment_name)
762 $attachment_name = str_replace(array(
"\n",
"\r"),
'', $attachment_name);
764 $attachment_name = $name;
766 $content_type = Web\MimeType::normalize($content_type);
770 if (mb_strpos($fileData[
"tmp_name"], \CTempFile::GetAbsoluteRoot()) === 0)
772 $file = new \Bitrix\Main\IO\File($fileData[
"tmp_name"]);
781 $filetime = $file->getModificationTime();
788 $APPLICATION->RestartBuffer();
791 $filesize = $fileData[
"size"];
794 $p = $server->get(
"HTTP_RANGE") && mb_strpos($server->get(
"HTTP_RANGE"),
"=");
797 $bytes = mb_substr($server->get(
"HTTP_RANGE"), $p + 1);
798 $p = mb_strpos($bytes,
"-");
801 $cur_pos = floatval(mb_substr($bytes, 0, $p));
802 $size = floatval(mb_substr($bytes, $p + 1));
805 $size = $filesize - 1;
807 if ($cur_pos > $size)
810 $size = $filesize - 1;
815 if ($server->getRequestMethod() ==
"HEAD")
817 \CHTTP::SetStatus(
"200 OK");
818 header(
"Accept-Ranges: bytes");
819 header(
"Content-Type: ".$content_type);
820 header(
"Content-Length: ".($size-$cur_pos+1));
823 header(
"Last-Modified: ".date(
"r", $filetime));
831 $ETag = md5($fileData[
"tmp_name"].$filesize.$filetime);
832 if ($server->get(
"HTTP_IF_NONE_MATCH") === $ETag)
834 \CHTTP::SetStatus(
"304 Not Modified");
835 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
838 header(
"ETag: ".$ETag);
843 $lastModified = gmdate(
'D, d M Y H:i:s', $filetime).
' GMT';
844 if ($server->get(
"HTTP_IF_NONE_MATCH") === $lastModified)
846 \CHTTP::SetStatus(
"304 Not Modified");
847 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
853 $utfName = Uri::urnEncode($attachment_name,
"UTF-8");
854 $translitName = \CUtil::translit($attachment_name, LANGUAGE_ID, array(
857 "replace_space" =>
'-',
858 "change_case" =>
false,
863 if(ini_get(
'zlib.output_compression'))
864 ini_set(
'zlib.output_compression',
'Off');
867 \CHTTP::SetStatus(
"206 Partial Content");
869 \CHTTP::SetStatus(
"200 OK");
871 header(
"Content-Type: ".$content_type);
872 header(
"Content-Disposition: attachment; filename=\"".$translitName.
"\"; filename*=utf-8''".$utfName);
873 header(
"Content-Transfer-Encoding: binary");
874 header(
"Content-Length: ".($size-$cur_pos+1));
875 if(is_resource($src))
877 header(
"Accept-Ranges: bytes");
878 header(
"Content-Range: bytes ".$cur_pos.
"-".$size.
"/".$filesize);
883 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
885 header(
'Last-Modified: '.$lastModified);
889 header(
"Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
892 header(
"Expires: 0");
893 header(
"Pragma: public");
896 if($fastDownload && ($fromClouds || mb_strpos($fileData[
"tmp_name"],
Application::getInstance()->getContext()->getServer()->getDocumentRoot()) === 0))
900 $filename = preg_replace(
'~^(http[s]?)(\://)~i',
'\\1.' , $fileData[
"tmp_name"]);
901 $cloudUploadPath = \COption::GetOptionString(
'main',
'bx_cloud_upload',
'/upload/bx_cloud_upload/');
902 header(
'X-Accel-Redirect: '.$cloudUploadPath.$filename);
906 header(
'X-Accel-Redirect: '.\
Bitrix\Main\Text\Encoding::convertEncoding($fileData[
"tmp_name"], SITE_CHARSET,
"UTF-8"));
911 session_write_close();
912 $file->seek($cur_pos);
913 while(!feof($src) && ($cur_pos <= $size))
916 if($cur_pos + $bufsize > $size)
917 $bufsize = $size - $cur_pos + 1;
918 $cur_pos += $bufsize;
919 echo fread($src, $bufsize);
925 $src = new \Bitrix\Main\Web\HttpClient();
926 $fp = fopen(
"php://output",
"wb");
927 $src->setOutputStream($fp);
928 $src->get($fileData[
"tmp_name"]);
931 \CMain::FinalActions();