Exemple #1
0
 public static function MoveFile($arFile, $obTargetBucket)
 {
     $io = CBXVirtualIo::GetInstance();
     //Try to find suitable bucket for the file
     $bucket = CCloudStorage::FindBucketForFile($arFile, $arFile["FILE_NAME"]);
     if (!is_object($bucket)) {
         return CCloudStorage::FILE_SKIPPED;
     }
     if (!$bucket->Init()) {
         return CCloudStorage::FILE_SKIPPED;
     }
     //Check if this is same bucket as the target
     if ($bucket->ID != $obTargetBucket->ID) {
         return CCloudStorage::FILE_SKIPPED;
     }
     if ($bucket->FileExists($bucket->GetFileSRC($arFile))) {
         //TODO rename file
         return CCloudStorage::FILE_SKIPPED;
     }
     $filePath = "/" . $arFile["SUBDIR"] . "/" . $arFile["FILE_NAME"];
     $filePath = preg_replace("#[\\\\\\/]+#", "/", $filePath);
     if ($arFile["FILE_SIZE"] > $bucket->GetService()->GetMinUploadPartSize()) {
         $obUpload = new CCloudStorageUpload($filePath);
         if (!$obUpload->isStarted()) {
             if ($arFile["HANDLER_ID"]) {
                 $ar = array();
                 if (!CCloudStorage::OnMakeFileArray($arFile, $ar)) {
                     return CCloudStorage::FILE_SKIPPED;
                 }
                 if (!isset($ar["tmp_name"])) {
                     return CCloudStorage::FILE_SKIPPED;
                 }
             } else {
                 $ar = CFile::MakeFileArray($arFile["ID"]);
                 if (!isset($ar["tmp_name"])) {
                     return CCloudStorage::FILE_SKIPPED;
                 }
             }
             $temp_file = CTempFile::GetDirectoryName(2, "clouds") . bx_basename($arFile["FILE_NAME"]);
             $temp_fileX = $io->GetPhysicalName($temp_file);
             CheckDirPath($temp_fileX);
             if (!copy($io->GetPhysicalName($ar["tmp_name"]), $temp_fileX)) {
                 return CCloudStorage::FILE_SKIPPED;
             }
             if ($obUpload->Start($bucket->ID, $arFile["FILE_SIZE"], $arFile["CONTENT_TYPE"], $temp_file)) {
                 return CCloudStorage::FILE_PARTLY_UPLOADED;
             } else {
                 return CCloudStorage::FILE_SKIPPED;
             }
         } else {
             $temp_file = $obUpload->getTempFileName();
             $temp_fileX = $io->GetPhysicalName($temp_file);
             $fp = fopen($temp_fileX, "rb");
             if (!is_resource($fp)) {
                 return CCloudStorage::FILE_SKIPPED;
             }
             $pos = $obUpload->getPos();
             if ($pos > filesize($temp_fileX)) {
                 if ($obUpload->Finish()) {
                     $bucket->IncFileCounter(filesize($temp_fileX));
                     if ($arFile["HANDLER_ID"]) {
                         CCloudStorage::OnFileDelete($arFile);
                     } else {
                         $ar = CFile::MakeFileArray($arFile["ID"]);
                         $fileNameX = $io->GetPhysicalName($ar["tmp_name"]);
                         unlink($fileNameX);
                         @rmdir(substr($fileNameX, 0, -strlen(bx_basename($fileNameX))));
                     }
                     return CCloudStorage::FILE_MOVED;
                 } else {
                     return CCloudStorage::FILE_SKIPPED;
                 }
             }
             fseek($fp, $pos);
             self::$part_count = $obUpload->GetPartCount();
             self::$part_size = $obUpload->getPartSize();
             $part = fread($fp, self::$part_size);
             while ($obUpload->hasRetries()) {
                 if ($obUpload->Next($part)) {
                     return CCloudStorage::FILE_PARTLY_UPLOADED;
                 }
             }
             return CCloudStorage::FILE_SKIPPED;
         }
     } else {
         if ($arFile["HANDLER_ID"]) {
             $ar = array();
             if (!CCloudStorage::OnMakeFileArray($arFile, $ar)) {
                 return CCloudStorage::FILE_SKIPPED;
             }
             if (!isset($ar["tmp_name"])) {
                 return CCloudStorage::FILE_SKIPPED;
             }
         } else {
             $ar = CFile::MakeFileArray($arFile["ID"]);
             if (!isset($ar["tmp_name"])) {
                 return CCloudStorage::FILE_SKIPPED;
             }
         }
         $res = $bucket->SaveFile($filePath, $ar);
         if ($res) {
             $bucket->IncFileCounter(filesize($ar["tmp_name"]));
             if (file_exists($ar["tmp_name"])) {
                 unlink($ar["tmp_name"]);
                 @rmdir(substr($ar["tmp_name"], 0, -strlen(bx_basename($ar["tmp_name"]))));
             }
             if ($arFile["HANDLER_ID"]) {
                 CCloudStorage::OnFileDelete($arFile);
             }
         } else {
             //delete temporary copy
             if ($arFile["HANDLER_ID"]) {
                 unlink($ar["tmp_name"]);
                 @rmdir(substr($ar["tmp_name"], 0, -strlen(bx_basename($ar["tmp_name"]))));
             }
         }
         return $res ? CCloudStorage::FILE_MOVED : CCloudStorage::FILE_SKIPPED;
     }
 }