Ejemplo n.º 1
0
 /**
  * Uploads new version to file.
  * @see \Bitrix\Disk\File::addVersion().
  * @param array $fileArray Structure like $_FILES.
  * @param int $createdBy Id of user.
  * @return Version|null
  */
 public function uploadVersion(array $fileArray, $createdBy)
 {
     $this->errorCollection->clear();
     if (!isset($fileArray['MODULE_ID'])) {
         $fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID;
     }
     if (empty($fileArray['type'])) {
         $fileArray['type'] = '';
     }
     $fileArray['type'] = TypeFile::normalizeMimeType($fileArray['type'], $this->name);
     /** @noinspection PhpDynamicAsStaticMethodCallInspection */
     $fileId = CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID, true, true);
     if (!$fileId) {
         $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_FILE_MODEL_ERROR_COULD_NOT_SAVE_FILE'), self::ERROR_COULD_NOT_SAVE_FILE)));
         return null;
     }
     /** @noinspection PhpDynamicAsStaticMethodCallInspection */
     $fileArray = CFile::getFileArray($fileId);
     $version = $this->addVersion($fileArray, $createdBy);
     if (!$version) {
         CFile::delete($fileId);
     }
     return $version;
 }
Ejemplo n.º 2
0
 /**
  * Uploads new file to folder.
  * @param array $fileArray Structure like $_FILES.
  * @param array $data Contains additional fields (CREATED_BY, NAME, etc).
  * @param array $rights Rights (@see \Bitrix\Disk\RightsManager).
  * @param bool  $generateUniqueName Generates unique name for object in directory.
  * @throws \Bitrix\Main\ArgumentException
  * @return File|null
  */
 public function uploadFile(array $fileArray, array $data, array $rights = array(), $generateUniqueName = false)
 {
     $this->errorCollection->clear();
     $this->checkRequiredInputParams($data, array('NAME', 'CREATED_BY'));
     if (!isset($fileArray['MODULE_ID'])) {
         $fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID;
     }
     if (empty($fileArray['type'])) {
         $fileArray['type'] = '';
     }
     $fileArray['type'] = TypeFile::normalizeMimeType($fileArray['type'], $data['NAME']);
     /** @noinspection PhpDynamicAsStaticMethodCallInspection */
     $fileId = CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID, true, true);
     if (!$fileId) {
         $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_FOLDER_MODEL_ERROR_COULD_NOT_SAVE_FILE'), self::ERROR_COULD_NOT_SAVE_FILE)));
         return null;
     }
     /** @noinspection PhpDynamicAsStaticMethodCallInspection */
     $fileArray = CFile::getFileArray($fileId);
     $data['NAME'] = Ui\Text::correctFilename($data['NAME']);
     $fileModel = $this->addFile(array('NAME' => $data['NAME'], 'FILE_ID' => $fileId, 'CONTENT_PROVIDER' => isset($data['CONTENT_PROVIDER']) ? $data['CONTENT_PROVIDER'] : null, 'SIZE' => !isset($data['SIZE']) ? $fileArray['FILE_SIZE'] : $data['SIZE'], 'CREATED_BY' => $data['CREATED_BY']), $rights, $generateUniqueName);
     if (!$fileModel) {
         CFile::delete($fileId);
         return null;
     }
     return $fileModel;
 }
Ejemplo n.º 3
0
 protected function migrateVersion()
 {
     if (!$this->runWorkWithBizproc) {
         return;
     }
     if ($this->isStepFinished(__METHOD__)) {
         return;
     }
     $uploadDir = COption::getOptionString("main", "upload_dir", "upload");
     $isCloud = CModule::IncludeModule("clouds");
     $useGZipCompressionOption = \Bitrix\Main\Config\Option::get("bizproc", "use_gzip_compression", "");
     $isBitrix24 = IsModuleInstalled('bitrix24');
     $bucket = null;
     if ($isBitrix24 && $isCloud) {
         $bucket = new CCloudStorageBucket(1);
         $bucket->init();
     }
     if ($useGZipCompressionOption === "Y") {
         $this->useGZipCompression = true;
     } elseif ($useGZipCompressionOption === "N") {
         $this->useGZipCompression = false;
     } else {
         $this->useGZipCompression = function_exists("gzcompress") && ($GLOBALS["DB"]->type != "ORACLE" || !defined('BX_UTF'));
     }
     $sqlHelper = $this->connection->getSqlHelper();
     $lastId = $this->getStorageId();
     $versionQuery = $this->connection->query("\n\t\t\tSELECT\n\t\t\t\tobj.*,\n\t\t\t\th.ID VERSION_ID,\n\t\t\t\th.NAME VERSION_NAME,\n\t\t\t\th.DOCUMENT VERSION_DOC,\n\t\t\t\th.USER_ID VERSION_USER_ID,\n\t\t\t\th.MODIFIED VERSION_MODIFIED\n\t\t\tFROM b_disk_object obj\n\t\t\t\tINNER JOIN b_bp_history h ON h.DOCUMENT_ID = obj.WEBDAV_ELEMENT_ID AND h.MODULE_ID = 'webdav'\n\n\t\t\tWHERE obj.TYPE = 3 AND h.ID > {$lastId} ORDER BY h.ID\n\t\t");
     while ($version = $versionQuery->fetch()) {
         $this->abortIfNeeded();
         if (strlen($version['VERSION_DOC']) > 0) {
             if ($this->useGZipCompression) {
                 $version['VERSION_DOC'] = gzuncompress($version['VERSION_DOC']);
             }
             $version['VERSION_DOC'] = unserialize($version['VERSION_DOC']);
             if (!is_array($version['VERSION_DOC'])) {
                 $version['VERSION_DOC'] = array();
             }
         } else {
             $version['VERSION_DOC'] = array();
         }
         if (empty($version['VERSION_DOC']) || empty($version['VERSION_DOC']['PROPERTIES']['WEBDAV_VERSION']['VALUE']) || empty($version['VERSION_DOC']['PROPERTIES']['FILE']['VALUE'])) {
             $this->storeStorageId($version['VERSION_ID']);
             continue;
         }
         $version['VERSION_NAME'] = $sqlHelper->forSql($version['VERSION_NAME']);
         $version['VERSION_MODIFIED'] = $sqlHelper->getCharToDateFunction($version['VERSION_MODIFIED']->format("Y-m-d H:i:s"));
         $version['UPDATE_TIME'] = $sqlHelper->getCharToDateFunction($version['UPDATE_TIME']->format("Y-m-d H:i:s"));
         $fullPath = $version['VERSION_DOC']['PROPERTIES']['FILE']['VALUE'];
         $handlerId = '';
         $filename = bx_basename($fullPath);
         if (substr($fullPath, 0, 4) == "http") {
             if (!$isCloud) {
                 $this->storeStorageId($version['VERSION_ID']);
                 continue;
             }
             if (!$isBitrix24) {
                 $bucket = CCloudStorage::findBucketByFile($fullPath);
                 if (!$bucket) {
                     $this->storeStorageId($version['VERSION_ID']);
                     continue;
                 }
             }
             $handlerId = $bucket->ID;
             $subDir = trim(substr(getDirPath($fullPath), strlen($bucket->getFileSRC('/'))), '/');
             $contentType = \Bitrix\Disk\TypeFile::getMimeTypeByFilename($filename);
         } else {
             $subDir = trim(substr(getDirPath($fullPath), strlen('/' . $uploadDir)), '/');
             $contentType = CFile::getContentType($_SERVER["DOCUMENT_ROOT"] . $fullPath);
             $contentType = \Bitrix\Disk\TypeFile::normalizeMimeType($contentType, $filename);
         }
         $webdavSize = $version['VERSION_DOC']['PROPERTIES']['WEBDAV_SIZE']['VALUE'];
         if (empty($webdavSize)) {
             $webdavSize = 0;
         }
         $fileId = CFile::doInsert(array('HEIGHT' => 0, 'WIDTH' => 0, 'FILE_SIZE' => $webdavSize, 'CONTENT_TYPE' => $contentType, 'SUBDIR' => $subDir, 'FILE_NAME' => $filename, 'MODULE_ID' => Driver::INTERNAL_MODULE_ID, 'ORIGINAL_NAME' => $filename, 'DESCRIPTION' => '', 'HANDLER_ID' => $handlerId, 'EXTERNAL_ID' => md5(mt_rand())));
         if (!$fileId) {
             $this->storeStorageId($version['VERSION_ID']);
             continue;
         }
         $this->connection->queryExecute("\n\t\t\t\tINSERT INTO b_disk_version (OBJECT_ID, FILE_ID, " . $this->sqlHelper->quote('SIZE') . ", NAME, CREATE_TIME, CREATED_BY, MISC_DATA, OBJECT_CREATE_TIME, OBJECT_CREATED_BY, OBJECT_UPDATE_TIME, OBJECT_UPDATED_BY, GLOBAL_CONTENT_VERSION, BP_VERSION_ID)\n\t\t\t\tVALUES ({$version['ID']}, {$fileId}, {$webdavSize}, '{$version['VERSION_NAME']}', {$version['VERSION_MODIFIED']},  {$version['VERSION_USER_ID']}, null, {$version['VERSION_MODIFIED']}, {$version['CREATED_BY']}, {$version['UPDATE_TIME']}, {$version['UPDATED_BY']}, {$version['VERSION_DOC']['PROPERTIES']['WEBDAV_VERSION']['VALUE']}, {$version['VERSION_ID']})\n\t\t\t");
         $this->storeStorageId($version['VERSION_ID']);
     }
     $this->abortIfNeeded();
     $this->storeStorageId(0);
     $this->setStepFinished(__METHOD__);
 }