PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. namespace Celebrio\FileSystem\Drivers;
  3. use Nette\Object;
  4. use Celebrio\FileSystem\Drivers\IDriver;
  5. use Celebrio\FileSystem\DataFile;
  6. use Celebrio\FileSystem\File;
  7. use Celebrio\WindowsAzure\AzureStorage\AzureStorageBLOB;
  8. use \Microsoft_Http_Client_Adapter_Exception;
  9. use \Microsoft_WindowsAzure_Exception;
  10. /**
  11. * Azure driver
  12. * This is general azure driver for those who doesn't want to use our File
  13. * System but want use Azure Storage for thier own needs.
  14. * Two opnions are posible. The simplier one is to store data to only one
  15. * container. Just set up the container name (by constructor or method
  16. * setContainer) and call load/save/move... without other container
  17. * specifications on files.
  18. * You can also do those operations between multiple containers. For this reason,
  19. * each method has container specification to determinate in which one you
  20. * want to store/move/copy etc.
  21. *
  22. * @author honza
  23. */
  24. class AzureStorage extends AzureStorageDriver implements IDriver {
  25. public function getModuleName() {
  26. return "azure";
  27. }
  28. public function __construct($host = '', $accountName = '', $password = '', $basicContainer = '') {
  29. $this->setContainer($basicContainer);
  30. $this->setConnectionSettings($host, $accountName, $basicContainer);
  31. }
  32. public function load(DataFile $dataFile, $container = '') {
  33. if($dataFile->isDirectory()) {
  34. throw new \InvalidArgumentException("Azure storage does not return directory but empty file.", 0);
  35. }
  36. $path = $this->getPathForFile($dataFile);
  37. if(empty($container))
  38. $container = $this->getContainer();
  39. try {
  40. $data = AzureStorageBLOB::connection()->getBlobData($container, $path);
  41. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  42. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", 0, $e);
  43. } catch(\Microsoft_WindowsAzure_Exception $e) {
  44. throw new \FileNotFoundException("Some problem occur", 0, $e);
  45. }
  46. $dataFile->setData($data);
  47. return $dataFile;
  48. }
  49. public function save(DataFile $dataFile, $container = '') {
  50. if(\strlen($dataFile->getData()) >= self::MAX_BLOB_SIZE) {
  51. $this->saveLarge($dataFile, $container);
  52. return;
  53. }
  54. $path = $this->getPathForFile($dataFile);
  55. if(empty($container))
  56. $container = $this->getContainer();
  57. try {
  58. AzureStorageBLOB::connection()->createContainerIfNotExists($container);
  59. AzureStorageBLOB::connection()->createResourceName($container, $path);
  60. AzureStorageBLOB::connection()->putBlobData($container, $path, $dataFile->getData());
  61. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  62. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  63. } catch(\Microsoft_WindowsAzure_Exception $e) {
  64. throw new \FileNotFoundException("Some problem occur", "0", $e);
  65. }
  66. }
  67. /**
  68. * Writes binary large objects to Azure storage. This method is called when
  69. * the size of uploaded file is greater then 64Mb. It takes incoming string
  70. * (data) and splits them into blocks of data which will be uploaded
  71. * on server separately and then confirmed by putBlockList method.
  72. *
  73. * @param DataFile $dataFile
  74. */
  75. public function saveLargeFile(DataFile $dataFile, $container = '') {
  76. if(\strlen($dataFile->getData()) < self::MAX_BLOB_SIZE) {
  77. $this->save($dataFile);
  78. return;
  79. }
  80. $path = $this->getPathForFile($dataFile);
  81. if(empty($container))
  82. $container = $this->getContainer();
  83. $data = $dataFile->getData();
  84. try {
  85. AzureStorageBLOB::connection()->createContainerIfNotExists($container);
  86. $numOfBlocks = ceil (\strlen($dataFile->getData()) / self::MAX_BLOCK_SIZE);
  87. $indexes = $this->getIndexes($numOfBlocks);
  88. //storing blocks on server
  89. for($i = 0; $i < $numOfBlocks; $i++) {
  90. $blockData = \substr($data, $i * self::MAX_BLOCK_SIZE, self::MAX_BLOCK_SIZE);
  91. AzureStorageBLOB::connection()->putBlock($container, $path, $indexes[$i], $blockData);
  92. $blockData = null;
  93. unset($blockData);
  94. }
  95. //commit blocks
  96. AzureStorageBLOB::connection()->putBlockList($container, $path, $indexes);
  97. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  98. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  99. } catch(\Microsoft_WindowsAzure_Exception $e) {
  100. throw new \FileNotFoundException("Some problem occur", "0", $e);
  101. }
  102. }
  103. public function delete(DataFile $dataFile, $container = '') {
  104. $path = $this->getPathForFile($dataFile);
  105. if(empty($container))
  106. $container = $this->getContainer();
  107. try {
  108. AzureStorageBLOB::connection()->deleteBlob($container, $path);
  109. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  110. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  111. } catch(\Microsoft_WindowsAzure_Exception $e) {
  112. throw new \FileNotFoundException("Some problem occur", "0", $e);
  113. }
  114. }
  115. public function move(DataFile $source, DataFile $target, $container = '') {
  116. $sourcePath = $this->getPathForFile($source);
  117. $targetPath = $this->getPathForFile($target);
  118. if($sourcePath === $targetPath) {
  119. throw new InvalidFilesystemOperation("Move the file on the same file is not allowed");
  120. }
  121. if(empty($container))
  122. $container = $this->getContainer();
  123. try {
  124. AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
  125. AzureStorageBLOB::connection()->deleteBlob($container, $sourcePath);
  126. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  127. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  128. } catch(\Microsoft_WindowsAzure_Exception $e) {
  129. throw new \FileNotFoundException("Some problem occur", "0", $e);
  130. }
  131. }
  132. public function moveBetweenContainers(DataFile $source, $sourceContainer, DataFile $target, $targetContainer) {
  133. if($sourceContainer === $targetContainer) {
  134. $this->move($source, $target, $sourceContainer);
  135. return;
  136. }
  137. $sourcePath = $this->getPathForFile($source);
  138. $targetPath = $this->getPathForFile($target);
  139. try {
  140. AzureStorageBLOB::connection()->createContainerIfNotExists($targetContainer);
  141. AzureStorageBLOB::connection()->copyBlob($sourceContainer, $sourcePath, $targetContainer, $targetPath);
  142. AzureStorageBLOB::connection()->deleteBlob($sourceContainer, $sourcePath);
  143. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  144. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  145. } catch(\Microsoft_WindowsAzure_Exception $e) {
  146. throw new \FileNotFoundException("Some problem occur", "0", $e);
  147. }
  148. }
  149. public function copy(DataFile $source, DataFile $target, $container = '') {
  150. $sourcePath = $this->getPathForFile($source);
  151. $targetPath = $this->getPathForFile($target);
  152. if(empty($container))
  153. $container = $this->getContainer();
  154. try {
  155. AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
  156. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  157. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  158. } catch(\Microsoft_WindowsAzure_Exception $e) {
  159. throw new \FileNotFoundException("Some problem occur", "0", $e);
  160. }
  161. }
  162. public function copyBetweenTwoContainers(DataFile $source, $sourceContainer, DataFile $target, $targetContainer) {
  163. if($sourceContainer === $targetContainer) {
  164. $this->copy($source, $target, $sourceContainer);
  165. return;
  166. }
  167. $sourcePath = $this->getPathForFile($source);
  168. $targetPath = $this->getPathForFile($target);
  169. try {
  170. AzureStorageBLOB::connection()->createContainerIfNotExists($targetContainer);
  171. AzureStorageBLOB::connection()->copyBlob($container, $sourcePath, $container, $targetPath);
  172. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  173. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  174. } catch(\Microsoft_WindowsAzure_Exception $e) {
  175. throw new \FileNotFoundException("Some problem occur", "0", $e);
  176. }
  177. }
  178. public function exists(DataFile $dataFile, $container ='') {
  179. $path = $this->getPathForFile($dataFile);
  180. if(empty($container))
  181. $this->getContainer();
  182. try {
  183. $exists = AzureStorageBLOB::connection()->blobExists($container, $path);
  184. } catch(Microsoft_Http_Client_Adapter_Exception $e) {
  185. throw new \Albireo\FileSystem\IOException("Connection failed. Check internet connection.", "0", $e);
  186. } catch(\Microsoft_WindowsAzure_Exception $e) {
  187. throw new \FileNotFoundException("Some problem occur", "0", $e);
  188. }
  189. return $exists;
  190. }
  191. public function onChangeOwner(DataFile $dataFile) {
  192. //Do nothing in this moment
  193. }
  194. public function getBlobsInContainer($containerName, $prefix = '') {
  195. return parent::getBlobsInContainer($containerName, $prefix);
  196. }
  197. public function getContainers($prefix = null) {
  198. return AzureStorageBLOB::connection()->listContainers($prefix);
  199. }
  200. public function setContainer($containerName) {
  201. parent::setContainer($containerName);
  202. }
  203. }