コード例 #1
1
 protected function processActionUpdate()
 {
     $this->checkRequiredPostParams(array('storageExtra', 'storageId', 'name'));
     if ($this->errorCollection->hasErrors()) {
         $this->sendJsonErrorResponse();
     }
     $tmpFile = $parentFolderId = $targetSectionId = $elementId = null;
     $storage = $this->getStorageObject($this->request->getPost('storageExtra'), $this->request->getPost('storageId'));
     $filename = $this->request->getPost('name');
     $token = $this->request->getPost('token');
     $inRoot = $this->request->getPost('inRoot') == 'true';
     $isUpdate = $this->request->getPost('update') == 'true';
     if ($token) {
         $tmpFileManager = new Bitrix24Disk\UploadFileManager();
         $tmpFileManager->setToken($token)->setUser($this->getUser());
         $tmpFile = $tmpFileManager->findUserSpecificTmpFileByToken();
         if (!$tmpFile) {
             throw new SystemException('Not found file by token');
         }
     }
     if (!$storage->isCorrectName($filename, $msg)) {
         $tmpFile && $tmpFile->delete();
         $this->sendJsonResponse(array('status' => static::STATUS_DENIED, 'message' => $msg));
     }
     if ($inRoot) {
         $storageExtra = $storage->getStorageExtra();
         $targetSectionId = $storageExtra['sectionId'];
         $parentFolderId = $storageExtra['sectionId'];
     } else {
         $this->checkRequiredPostParams(array('parentExtra'));
         if ($this->errorCollection->hasErrors()) {
             $this->sendJsonErrorResponse();
         }
         $parentFolderExtra = $storage->parseElementExtra($this->request->getPost('parentExtra'));
         $targetSectionId = $parentFolderExtra['id'];
         $parentFolderId = $parentFolderExtra['id'];
     }
     if ($isUpdate) {
         $this->checkRequiredPostParams(array('id', 'version'));
         if ($this->errorCollection->hasErrors()) {
             $this->sendJsonErrorResponse();
         }
         $version = $this->request->getPost('version');
         $fileExtra = $storage->parseElementExtra($this->request->getPost('extra'));
         $elementId = $fileExtra['id'];
         $file = $storage->getFile($this->request->getPost('id'), $fileExtra);
         if (empty($file)) {
             $this->sendJsonResponse(array('status' => static::STATUS_NOT_FOUND));
         }
         if ($storage->compareVersion($file['version'], $version) > 0) {
             $file['status'] = static::STATUS_OLD_VERSION;
             $this->sendJsonResponse($file);
         }
         //todo simple check for move/rename
         if ($filename != $file['extra']['name'] || $parentFolderId != $file['extra']['sectionId']) {
             if (!$storage->isUnique($filename, $parentFolderId)) {
                 $file['status'] = static::STATUS_OLD_VERSION;
                 $this->sendJsonResponse($file);
             }
             if ($filename != $file['extra']['name']) {
                 $file = $storage->renameFile($filename, $elementId, $parentFolderId);
                 if (!$file) {
                     $this->errorCollection->addOne(new Error('Error in rename (update) file'));
                     $this->sendJsonErrorResponse();
                 }
             }
             if ($parentFolderId != $file['extra']['sectionId']) {
                 $file = $storage->moveFile($filename, $elementId, $parentFolderId);
             }
             if (!$file) {
                 $this->errorCollection->addOne(new Error('Error in move/rename (update) file'));
                 $this->sendJsonErrorResponse();
             }
             if (!$tmpFile) {
                 $this->sendJsonSuccessResponse($file);
             }
         }
         unset($file);
         if ($tmpFile) {
             $file = $storage->updateFile($filename, $elementId, $tmpFile, array('originalTimestamp' => $this->request->getPost('originalTimestamp')));
             if ($file) {
                 $event = new Event(Driver::INTERNAL_MODULE_ID, self::EVENT_ON_AFTER_DISK_FILE_UPDATE, array($file['extra']['id'], $file));
                 $event->send();
                 $this->sendJsonSuccessResponse($file);
             }
             $this->sendJsonResponse(array('status' => static::STATUS_DENIED, 'message' => 'Error in updateFile'));
         }
     } else {
         if (!$storage->isUnique($filename, $targetSectionId, $opponentId)) {
             $opponentFile = array();
             if ($opponentId) {
                 $opponentFile = $storage->getFile(null, array('id' => $opponentId), true);
             }
             $opponentFile['status'] = static::STATUS_OLD_VERSION;
             $this->sendJsonResponse($opponentFile);
         }
         $newFile = $storage->addFile($filename, $targetSectionId, $tmpFile, array('originalTimestamp' => $this->request->getPost('originalTimestamp')));
         if ($newFile) {
             $event = new Event(Driver::INTERNAL_MODULE_ID, self::EVENT_ON_AFTER_DISK_FILE_ADD, array($newFile['extra']['id'], $newFile));
             $event->send();
             $this->sendJsonSuccessResponse($newFile);
         }
         //else denied
     }
     $this->sendJsonResponse(array('status' => static::STATUS_DENIED, 'message' => 'Error in add/update file'));
 }
コード例 #2
0
ファイル: importmanager.php プロジェクト: DarneoStudio/bitrix
 /**
  * Fix for Google. It does not get in metadata real size of empty file.
  * @param Entry             $entry
  * @param Document\FileData $fileData
  * @return bool
  * @throws IO\FileNotFoundException
  * @throws \Bitrix\Main\SystemException
  */
 protected function uploadEmptyFileFromGoogle(Entry $entry, Document\FileData $fileData)
 {
     $tmpFile = $fileData->getSrc();
     $downloadedContentSize = $entry->getDownloadedContentSize();
     $startRange = $downloadedContentSize;
     //fix for Google. It doesn't get in metadata real size of empty file.
     if (!$this->documentHandler->downloadFile($fileData)) {
         $this->errorCollection->add($this->documentHandler->getErrors());
         return false;
     }
     $realFile = new IO\File($tmpFile);
     $contentSize = $realFile->getSize();
     $entry->setContentSize($contentSize);
     $chunkSize = $contentSize - $downloadedContentSize;
     $uploadFileManager = new Bitrix24Disk\UploadFileManager();
     $uploadFileManager->setTmpFileClass(TmpFile::className())->setUser($this->documentHandler->getUserId())->setFileSize($contentSize)->setContentRange(array($startRange, $startRange + $chunkSize - 1));
     $tmpFileArray = \CFile::makeFileArray($tmpFile);
     if (!$uploadFileManager->upload($fileData->getId(), $tmpFileArray)) {
         $this->errorCollection->add($uploadFileManager->getErrors());
         return false;
     }
     $entry->linkTmpFile(TmpFile::load(array('=TOKEN' => $uploadFileManager->getToken())));
     return $entry->increaseDownloadedContentSize($chunkSize);
 }