public function __construct($type) { parent::__construct(); $type = trim(strtolower($type), '.'); if (!$this->issetType($type)) { throw new SystemException("Could not find type '{$type}' in BlankFile"); } $typeData = $this->getType($type); $this->name = $typeData['newFileName'] . $typeData['ext']; $this->mimeType = TypeFile::getMimeTypeByFilename($this->name); $this->src = $typeData['src']; $this->size = IO\File::isFileExists($typeData['src']) ? filesize($typeData['src']) : 0; }
/** * @param FileData $fileData * @return null|EditSession * @throws \Bitrix\Main\NotImplementedException */ protected function addCreateEditSessionByCurrentUser(FileData $fileData) { return EditSession::add(array('USER_ID' => $this->getUser()->getId(), 'OWNER_ID' => $this->getUser()->getId(), 'IS_EXCLUSIVE' => 1, 'SERVICE' => $this->documentHandler->getCode(), 'SERVICE_FILE_ID' => $fileData->getId(), 'SERVICE_FILE_LINK' => $fileData->getLinkInService()), $this->errorCollection); }
/** * Gets a file's metadata by ID * @param FileData $fileData * @return array|null */ private function getFileMetadataInternal(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } $accessToken = $this->getAccessToken(); $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $http->setHeader('Content-Type', 'application/json; charset=UTF-8'); $http->setHeader('Authorization', "Bearer {$accessToken}"); if ($http->get(self::API_URL_V2 . '/files/' . $fileData->getId()) === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_METADATA))); return null; } if (!$this->checkHttpResponse($http)) { return null; } $file = Json::decode($http->getResult()); if ($file === null) { $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON))); return null; } return $file; }
/** * Download part of file from cloud service by FileData::id, put contents in FileData::src * @param FileData $fileData * @param $startRange * @param $chunkSize * @return FileData|null */ public function downloadPartFile(FileData $fileData, $startRange, $chunkSize) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id', 'src'))) { return null; } $accessToken = $this->getAccessToken(); @set_time_limit(0); $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $http->setHeader('Authorization', "Bearer {$accessToken}"); $endRange = $startRange + $chunkSize - 1; $http->setHeader('Range', "bytes={$startRange}-{$endRange}"); if ($http->download(self::API_URL_V2 . "/files/{$fileData->getId()}/content", $fileData->getSrc()) === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_DOWNLOAD_FILE))); return null; } return $fileData; }
/** * Saves file in destination folder from entry of cloud import. * @param Entry $entry Cloud import entry. * @param Folder $folder Destination folder. * @return \Bitrix\Disk\File|null New file object. */ public function saveFile(Entry $entry, Folder $folder) { if (!$entry->getTmpFile()) { $this->errorCollection->addOne(new Error('Could not find cloud import', self::ERROR_COULD_NOT_FIND_CLOUD_IMPORT)); return null; } if ($entry->getContentSize() != $entry->getDownloadedContentSize()) { $this->errorCollection->addOne(new Error('Content size != downloaded content size')); return null; } $fileData = new Document\FileData(); $fileData->setId($entry->getServiceObjectId()); $fileMetadata = $this->documentHandler->getFileMetadata($fileData); if (!$fileMetadata || empty($fileMetadata['name'])) { $this->errorCollection->add($this->documentHandler->getErrors()); return null; } $name = $fileMetadata['name']; if (!getFileExtension($name)) { $name = $this->recoverExtensionInName($name, $fileMetadata['mimeType']); } $tmpFile = $entry->getTmpFile(); $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); $file = $folder->uploadFile($fileArray, array('NAME' => $name, 'CREATED_BY' => $this->documentHandler->getUserId(), 'CONTENT_PROVIDER' => $this->documentHandler->getCode()), array(), true); if (!$file) { $tmpFile->delete(); $this->errorCollection->add($folder->getErrors()); return null; } $entry->linkObject($file); return $file; }
/** * Check success view file in service. * @param FileData $fileData * @return bool */ public function checkViewFile(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } if (!ExternalLink::isValidValueForField('HASH', $fileData->getId(), $this->errorCollection)) { return false; } /** @var ExternalLink $extLinkModel */ $extLinkModel = ExternalLink::load(array('=HASH' => $fileData->getId())); if (!$extLinkModel) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_GOOGLE_VIEWER_HANDLER_ERROR_COULD_NOT_FIND_EXT_LINK'), self::ERROR_COULD_NOT_FIND_EXT_LINK))); return null; } return (bool) $extLinkModel->getDownloadCount(); }
protected function getSharedEmbedLink(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } $accessToken = $this->getAccessToken(); $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $accessToken = urlencode($accessToken); if ($http->get("https://apis.live.net/v5.0/{$fileData->getId()}/embed?access_token={$accessToken}") === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_SHARED_EMBED_LINK))); return null; } if (!$this->checkHttpResponse($http)) { return null; } $finalOutput = Json::decode($http->getResult()); if ($finalOutput === null) { $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON))); return null; } if (!preg_match('%src="(.*)"%iuU', $finalOutput['embed_html'], $m)) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_ONE_DRIVE_HANDLER_ERROR_COULD_NOT_FIND_EMBED_LINK'), self::ERROR_COULD_NOT_FIND_EMBED_LINK))); return null; } return $m[1]; }