PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/zend-1.11.2/library/Zend/Service/WindowsAzure/Storage/Blob/Stream.php

https://github.com/rakesh-sankar/PHP-Framework-Benchmark
PHP | 565 lines | 301 code | 58 blank | 206 comment | 33 complexity | ba168eee39265ddf5cc99877bd4433a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_WindowsAzure_Storage
  17. * @subpackage Blob
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://todo name_todo
  20. * @version $Id: Stream.php 23485 2010-12-10 04:03:18Z mjh_ca $
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Storage_Blob
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Exception
  28. */
  29. require_once 'Zend/Service/WindowsAzure/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure_Storage
  33. * @subpackage Blob
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_WindowsAzure_Storage_Blob_Stream
  38. {
  39. /**
  40. * Current file name
  41. *
  42. * @var string
  43. */
  44. private $_fileName = null;
  45. /**
  46. * Temporary file name
  47. *
  48. * @var string
  49. */
  50. private $_temporaryFileName = null;
  51. /**
  52. * Temporary file handle
  53. *
  54. * @var resource
  55. */
  56. private $_temporaryFileHandle = null;
  57. /**
  58. * Blob storage client
  59. *
  60. * @var Zend_Service_WindowsAzure_Storage_Blob
  61. */
  62. private $_storageClient = null;
  63. /**
  64. * Write mode?
  65. *
  66. * @var boolean
  67. */
  68. private $_writeMode = false;
  69. /**
  70. * List of blobs
  71. *
  72. * @var array
  73. */
  74. private $_blobs = null;
  75. /**
  76. * Retrieve storage client for this stream type
  77. *
  78. * @param string $path
  79. * @return Zend_Service_WindowsAzure_Storage_Blob
  80. */
  81. protected function _getStorageClient($path = '')
  82. {
  83. if ($this->_storageClient === null) {
  84. $url = explode(':', $path);
  85. if (!$url) {
  86. throw new Zend_Service_WindowsAzure_Exception('Could not parse path "' . $path . '".');
  87. }
  88. $this->_storageClient = Zend_Service_WindowsAzure_Storage_Blob::getWrapperClient($url[0]);
  89. if (!$this->_storageClient) {
  90. throw new Zend_Service_WindowsAzure_Exception('No storage client registered for stream type "' . $url[0] . '://".');
  91. }
  92. }
  93. return $this->_storageClient;
  94. }
  95. /**
  96. * Extract container name
  97. *
  98. * @param string $path
  99. * @return string
  100. */
  101. protected function _getContainerName($path)
  102. {
  103. $url = parse_url($path);
  104. if ($url['host']) {
  105. return $url['host'];
  106. }
  107. return '';
  108. }
  109. /**
  110. * Extract file name
  111. *
  112. * @param string $path
  113. * @return string
  114. */
  115. protected function _getFileName($path)
  116. {
  117. $url = parse_url($path);
  118. if ($url['host']) {
  119. $fileName = isset($url['path']) ? $url['path'] : $url['host'];
  120. if (strpos($fileName, '/') === 0) {
  121. $fileName = substr($fileName, 1);
  122. }
  123. return $fileName;
  124. }
  125. return '';
  126. }
  127. /**
  128. * Open the stream
  129. *
  130. * @param string $path
  131. * @param string $mode
  132. * @param integer $options
  133. * @param string $opened_path
  134. * @return boolean
  135. */
  136. public function stream_open($path, $mode, $options, $opened_path)
  137. {
  138. $this->_fileName = $path;
  139. $this->_temporaryFileName = tempnam(sys_get_temp_dir(), 'azure');
  140. // Check the file can be opened
  141. $fh = @fopen($this->_temporaryFileName, $mode);
  142. if ($fh === false) {
  143. return false;
  144. }
  145. fclose($fh);
  146. // Write mode?
  147. if (strpbrk($mode, 'wax+')) {
  148. $this->_writeMode = true;
  149. } else {
  150. $this->_writeMode = false;
  151. }
  152. // If read/append, fetch the file
  153. if (!$this->_writeMode || strpbrk($mode, 'ra+')) {
  154. $this->_getStorageClient($this->_fileName)->getBlob(
  155. $this->_getContainerName($this->_fileName),
  156. $this->_getFileName($this->_fileName),
  157. $this->_temporaryFileName
  158. );
  159. }
  160. // Open temporary file handle
  161. $this->_temporaryFileHandle = fopen($this->_temporaryFileName, $mode);
  162. // Ok!
  163. return true;
  164. }
  165. /**
  166. * Close the stream
  167. *
  168. * @return void
  169. */
  170. public function stream_close()
  171. {
  172. @fclose($this->_temporaryFileHandle);
  173. // Upload the file?
  174. if ($this->_writeMode) {
  175. // Make sure the container exists
  176. $containerExists = $this->_getStorageClient($this->_fileName)->containerExists(
  177. $this->_getContainerName($this->_fileName)
  178. );
  179. if (!$containerExists) {
  180. $this->_getStorageClient($this->_fileName)->createContainer(
  181. $this->_getContainerName($this->_fileName)
  182. );
  183. }
  184. // Upload the file
  185. try {
  186. $this->_getStorageClient($this->_fileName)->putBlob(
  187. $this->_getContainerName($this->_fileName),
  188. $this->_getFileName($this->_fileName),
  189. $this->_temporaryFileName
  190. );
  191. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  192. @unlink($this->_temporaryFileName);
  193. unset($this->_storageClient);
  194. throw $ex;
  195. }
  196. }
  197. @unlink($this->_temporaryFileName);
  198. unset($this->_storageClient);
  199. }
  200. /**
  201. * Read from the stream
  202. *
  203. * @param integer $count
  204. * @return string
  205. */
  206. public function stream_read($count)
  207. {
  208. if (!$this->_temporaryFileHandle) {
  209. return false;
  210. }
  211. return fread($this->_temporaryFileHandle, $count);
  212. }
  213. /**
  214. * Write to the stream
  215. *
  216. * @param string $data
  217. * @return integer
  218. */
  219. public function stream_write($data)
  220. {
  221. if (!$this->_temporaryFileHandle) {
  222. return 0;
  223. }
  224. $len = strlen($data);
  225. fwrite($this->_temporaryFileHandle, $data, $len);
  226. return $len;
  227. }
  228. /**
  229. * End of the stream?
  230. *
  231. * @return boolean
  232. */
  233. public function stream_eof()
  234. {
  235. if (!$this->_temporaryFileHandle) {
  236. return true;
  237. }
  238. return feof($this->_temporaryFileHandle);
  239. }
  240. /**
  241. * What is the current read/write position of the stream?
  242. *
  243. * @return integer
  244. */
  245. public function stream_tell()
  246. {
  247. return ftell($this->_temporaryFileHandle);
  248. }
  249. /**
  250. * Update the read/write position of the stream
  251. *
  252. * @param integer $offset
  253. * @param integer $whence
  254. * @return boolean
  255. */
  256. public function stream_seek($offset, $whence)
  257. {
  258. if (!$this->_temporaryFileHandle) {
  259. return false;
  260. }
  261. return (fseek($this->_temporaryFileHandle, $offset, $whence) === 0);
  262. }
  263. /**
  264. * Flush current cached stream data to storage
  265. *
  266. * @return boolean
  267. */
  268. public function stream_flush()
  269. {
  270. $result = fflush($this->_temporaryFileHandle);
  271. // Upload the file?
  272. if ($this->_writeMode) {
  273. // Make sure the container exists
  274. $containerExists = $this->_getStorageClient($this->_fileName)->containerExists(
  275. $this->_getContainerName($this->_fileName)
  276. );
  277. if (!$containerExists) {
  278. $this->_getStorageClient($this->_fileName)->createContainer(
  279. $this->_getContainerName($this->_fileName)
  280. );
  281. }
  282. // Upload the file
  283. try {
  284. $this->_getStorageClient($this->_fileName)->putBlob(
  285. $this->_getContainerName($this->_fileName),
  286. $this->_getFileName($this->_fileName),
  287. $this->_temporaryFileName
  288. );
  289. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  290. @unlink($this->_temporaryFileName);
  291. unset($this->_storageClient);
  292. throw $ex;
  293. }
  294. }
  295. return $result;
  296. }
  297. /**
  298. * Returns data array of stream variables
  299. *
  300. * @return array
  301. */
  302. public function stream_stat()
  303. {
  304. if (!$this->_temporaryFileHandle) {
  305. return false;
  306. }
  307. $stat = array();
  308. $stat['dev'] = 0;
  309. $stat['ino'] = 0;
  310. $stat['mode'] = 0;
  311. $stat['nlink'] = 0;
  312. $stat['uid'] = 0;
  313. $stat['gid'] = 0;
  314. $stat['rdev'] = 0;
  315. $stat['size'] = 0;
  316. $stat['atime'] = 0;
  317. $stat['mtime'] = 0;
  318. $stat['ctime'] = 0;
  319. $stat['blksize'] = 0;
  320. $stat['blocks'] = 0;
  321. $info = null;
  322. try {
  323. $info = $this->_getStorageClient($this->_fileName)->getBlobInstance(
  324. $this->_getContainerName($this->_fileName),
  325. $this->_getFileName($this->_fileName)
  326. );
  327. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  328. // Unexisting file...
  329. }
  330. if ($info !== null) {
  331. $stat['size'] = $info->Size;
  332. $stat['atime'] = time();
  333. }
  334. return $stat;
  335. }
  336. /**
  337. * Attempt to delete the item
  338. *
  339. * @param string $path
  340. * @return boolean
  341. */
  342. public function unlink($path)
  343. {
  344. $this->_getStorageClient($path)->deleteBlob(
  345. $this->_getContainerName($path),
  346. $this->_getFileName($path)
  347. );
  348. }
  349. /**
  350. * Attempt to rename the item
  351. *
  352. * @param string $path_from
  353. * @param string $path_to
  354. * @return boolean False
  355. */
  356. public function rename($path_from, $path_to)
  357. {
  358. if ($this->_getContainerName($path_from) != $this->_getContainerName($path_to)) {
  359. throw new Zend_Service_WindowsAzure_Exception('Container name can not be changed.');
  360. }
  361. if ($this->_getFileName($path_from) == $this->_getContainerName($path_to)) {
  362. return true;
  363. }
  364. $this->_getStorageClient($path_from)->copyBlob(
  365. $this->_getContainerName($path_from),
  366. $this->_getFileName($path_from),
  367. $this->_getContainerName($path_to),
  368. $this->_getFileName($path_to)
  369. );
  370. $this->_getStorageClient($path_from)->deleteBlob(
  371. $this->_getContainerName($path_from),
  372. $this->_getFileName($path_from)
  373. );
  374. return true;
  375. }
  376. /**
  377. * Return array of URL variables
  378. *
  379. * @param string $path
  380. * @param integer $flags
  381. * @return array
  382. */
  383. public function url_stat($path, $flags)
  384. {
  385. $stat = array();
  386. $stat['dev'] = 0;
  387. $stat['ino'] = 0;
  388. $stat['mode'] = 0;
  389. $stat['nlink'] = 0;
  390. $stat['uid'] = 0;
  391. $stat['gid'] = 0;
  392. $stat['rdev'] = 0;
  393. $stat['size'] = 0;
  394. $stat['atime'] = 0;
  395. $stat['mtime'] = 0;
  396. $stat['ctime'] = 0;
  397. $stat['blksize'] = 0;
  398. $stat['blocks'] = 0;
  399. $info = null;
  400. try {
  401. $info = $this->_getStorageClient($path)->getBlobInstance(
  402. $this->_getContainerName($path),
  403. $this->_getFileName($path)
  404. );
  405. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  406. // Unexisting file...
  407. }
  408. if ($info !== null) {
  409. $stat['size'] = $info->Size;
  410. $stat['atime'] = time();
  411. }
  412. return $stat;
  413. }
  414. /**
  415. * Create a new directory
  416. *
  417. * @param string $path
  418. * @param integer $mode
  419. * @param integer $options
  420. * @return boolean
  421. */
  422. public function mkdir($path, $mode, $options)
  423. {
  424. if ($this->_getContainerName($path) == $this->_getFileName($path)) {
  425. // Create container
  426. try {
  427. $this->_getStorageClient($path)->createContainer(
  428. $this->_getContainerName($path)
  429. );
  430. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  431. return false;
  432. }
  433. } else {
  434. throw new Zend_Service_WindowsAzure_Exception('mkdir() with multiple levels is not supported on Windows Azure Blob Storage.');
  435. }
  436. }
  437. /**
  438. * Remove a directory
  439. *
  440. * @param string $path
  441. * @param integer $options
  442. * @return boolean
  443. */
  444. public function rmdir($path, $options)
  445. {
  446. if ($this->_getContainerName($path) == $this->_getFileName($path)) {
  447. // Delete container
  448. try {
  449. $this->_getStorageClient($path)->deleteContainer(
  450. $this->_getContainerName($path)
  451. );
  452. } catch (Zend_Service_WindowsAzure_Exception $ex) {
  453. return false;
  454. }
  455. } else {
  456. throw new Zend_Service_WindowsAzure_Exception('rmdir() with multiple levels is not supported on Windows Azure Blob Storage.');
  457. }
  458. }
  459. /**
  460. * Attempt to open a directory
  461. *
  462. * @param string $path
  463. * @param integer $options
  464. * @return boolean
  465. */
  466. public function dir_opendir($path, $options)
  467. {
  468. $this->_blobs = $this->_getStorageClient($path)->listBlobs(
  469. $this->_getContainerName($path)
  470. );
  471. return is_array($this->_blobs);
  472. }
  473. /**
  474. * Return the next filename in the directory
  475. *
  476. * @return string
  477. */
  478. public function dir_readdir()
  479. {
  480. $object = current($this->_blobs);
  481. if ($object !== false) {
  482. next($this->_blobs);
  483. return $object->Name;
  484. }
  485. return false;
  486. }
  487. /**
  488. * Reset the directory pointer
  489. *
  490. * @return boolean True
  491. */
  492. public function dir_rewinddir()
  493. {
  494. reset($this->_blobs);
  495. return true;
  496. }
  497. /**
  498. * Close a directory
  499. *
  500. * @return boolean True
  501. */
  502. public function dir_closedir()
  503. {
  504. $this->_blobs = null;
  505. return true;
  506. }
  507. }