/libs/Albireo/FileSystem/Drivers/Azure/AzureGeneral/AzureStorage.php
https://code.google.com/ · PHP · 251 lines · 178 code · 48 blank · 25 comment · 14 complexity · 116f9023baf54175a353eeb7833110e8 MD5 · raw file
- <?php
- namespace Celebrio\FileSystem\Drivers;
- use Nette\Object;
- use Celebrio\FileSystem\Drivers\IDriver;
- use Celebrio\FileSystem\DataFile;
- use Celebrio\FileSystem\File;
- use Celebrio\WindowsAzure\AzureStorage\AzureStorageBLOB;
- use \Microsoft_Http_Client_Adapter_Exception;
- use \Microsoft_WindowsAzure_Exception;
- /**
- * Azure driver
- * This is general azure driver for those who doesn't want to use our File
- * System but want use Azure Storage for thier own needs.
- * Two opnions are posible. The simplier one is to store data to only one
- * container. Just set up the container name (by constructor or method
- * setContainer) and call load/save/move... without other container
- * specifications on files.
- * You can also do those operations between multiple containers. For this reason,
- * each method has container specification to determinate in which one you
- * want to store/move/copy etc.
- *
- * @author honza
- */
- class AzureStorage extends AzureStorageDriver implements IDriver {
-
-
- public function getModuleName() {
- return "azure";
- }
-
- public function __construct($host = '', $accountName = '', $password = '', $basicContainer = '') {
- $this->setContainer($basicContainer);
- $this->setConnectionSettings($host, $accountName, $basicContainer);
- }
- public function load(DataFile $dataFile, $container = '') {
-
- if($dataFile->isDirectory()) {
- throw new \InvalidArgumentException("Azure storage does not return directory but empty file.", 0);
- }
- $path = $this->getPathForFile($dataFile);
-
- if(empty($container))
- $container = $this->getContainer();
-
- try {
- $data = AzureStorageBLOB::connection()->getBlobData($container, $path);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", 0, $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", 0, $e);
- }
-
- $dataFile->setData($data);
-
- return $dataFile;
- }
- public function save(DataFile $dataFile, $container = '') {
- if(\strlen($dataFile->getData()) >= self::MAX_BLOB_SIZE) {
- $this->saveLarge($dataFile, $container);
- return;
- }
- $path = $this->getPathForFile($dataFile);
- if(empty($container))
- $container = $this->getContainer();
-
- try {
- AzureStorageBLOB::connection()->createContainerIfNotExists($container);
- AzureStorageBLOB::connection()->createResourceName($container, $path);
-
- AzureStorageBLOB::connection()->putBlobData($container, $path, $dataFile->getData());
-
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- /**
- * Writes binary large objects to Azure storage. This method is called when
- * the size of uploaded file is greater then 64Mb. It takes incoming string
- * (data) and splits them into blocks of data which will be uploaded
- * on server separately and then confirmed by putBlockList method.
- *
- * @param DataFile $dataFile
- */
- public function saveLargeFile(DataFile $dataFile, $container = '') {
- if(\strlen($dataFile->getData()) < self::MAX_BLOB_SIZE) {
- $this->save($dataFile);
- return;
- }
- $path = $this->getPathForFile($dataFile);
- if(empty($container))
- $container = $this->getContainer();
- $data = $dataFile->getData();
- try {
- AzureStorageBLOB::connection()->createContainerIfNotExists($container);
- $numOfBlocks = ceil (\strlen($dataFile->getData()) / self::MAX_BLOCK_SIZE);
- $indexes = $this->getIndexes($numOfBlocks);
-
- //storing blocks on server
- for($i = 0; $i < $numOfBlocks; $i++) {
- $blockData = \substr($data, $i * self::MAX_BLOCK_SIZE, self::MAX_BLOCK_SIZE);
- AzureStorageBLOB::connection()->putBlock($container, $path, $indexes[$i], $blockData);
- $blockData = null;
- unset($blockData);
- }
- //commit blocks
- AzureStorageBLOB::connection()->putBlockList($container, $path, $indexes);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- public function delete(DataFile $dataFile, $container = '') {
- $path = $this->getPathForFile($dataFile);
- if(empty($container))
- $container = $this->getContainer();
-
- try {
- AzureStorageBLOB::connection()->deleteBlob($container, $path);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
-
- }
-
- public function move(DataFile $source, DataFile $target, $container = '') {
- $sourcePath = $this->getPathForFile($source);
- $targetPath = $this->getPathForFile($target);
-
- if($sourcePath === $targetPath) {
- throw new InvalidFilesystemOperation("Move the file on the same file is not allowed");
- }
-
- if(empty($container))
- $container = $this->getContainer();
-
- try {
- AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
- AzureStorageBLOB::connection()->deleteBlob($container, $sourcePath);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- public function moveBetweenContainers(DataFile $source, $sourceContainer, DataFile $target, $targetContainer) {
- if($sourceContainer === $targetContainer) {
- $this->move($source, $target, $sourceContainer);
- return;
- }
-
- $sourcePath = $this->getPathForFile($source);
- $targetPath = $this->getPathForFile($target);
-
- try {
- AzureStorageBLOB::connection()->createContainerIfNotExists($targetContainer);
- AzureStorageBLOB::connection()->copyBlob($sourceContainer, $sourcePath, $targetContainer, $targetPath);
- AzureStorageBLOB::connection()->deleteBlob($sourceContainer, $sourcePath);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- public function copy(DataFile $source, DataFile $target, $container = '') {
- $sourcePath = $this->getPathForFile($source);
- $targetPath = $this->getPathForFile($target);
- if(empty($container))
- $container = $this->getContainer();
-
- try {
- AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- public function copyBetweenTwoContainers(DataFile $source, $sourceContainer, DataFile $target, $targetContainer) {
- if($sourceContainer === $targetContainer) {
- $this->copy($source, $target, $sourceContainer);
- return;
- }
-
- $sourcePath = $this->getPathForFile($source);
- $targetPath = $this->getPathForFile($target);
-
- try {
- AzureStorageBLOB::connection()->createContainerIfNotExists($targetContainer);
- AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
- }
-
- public function exists(DataFile $dataFile, $container ='') {
- $path = $this->getPathForFile($dataFile);
-
- if(empty($container))
- $this->getContainer();
-
- try {
- $exists = AzureStorageBLOB::connection()->blobExists($container, $path);
- } catch(Microsoft_Http_Client_Adapter_Exception $e) {
- throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
- } catch(\Microsoft_WindowsAzure_Exception $e) {
- throw new \FileNotFoundException("Some problem occur", "0", $e);
- }
-
- return $exists;
-
- }
-
- public function onChangeOwner(DataFile $dataFile) {
- //Do nothing in this moment
- }
-
- public function getBlobsInContainer($containerName, $prefix = '') {
- return parent::getBlobsInContainer($containerName, $prefix);
- }
- public function getContainers($prefix = null) {
- return AzureStorageBLOB::connection()->listContainers($prefix);
- }
- public function setContainer($containerName) {
- parent::setContainer($containerName);
- }
- }