MediaFileSource.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_Gdata
  17. * @subpackage App
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Gdata_App_MediaData
  23. */
  24. require_once 'Zend/Gdata/App/BaseMediaSource.php';
  25. /**
  26. * Concrete class to use a file handle as an attachment within a MediaEntry.
  27. *
  28. * @category Zend
  29. * @package Zend_Gdata
  30. * @subpackage App
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Gdata_App_MediaFileSource extends Zend_Gdata_App_BaseMediaSource
  35. {
  36. /**
  37. * The filename which is represented
  38. *
  39. * @var string
  40. */
  41. protected $_filename = null;
  42. /**
  43. * The content type for the file attached (example image/png)
  44. *
  45. * @var string
  46. */
  47. protected $_contentType = null;
  48. /**
  49. * Create a new Zend_Gdata_App_MediaFileSource object.
  50. *
  51. * @param string $filename The name of the file to read from.
  52. */
  53. public function __construct($filename)
  54. {
  55. $this->setFilename($filename);
  56. }
  57. /**
  58. * Return the MIME multipart representation of this MediaEntry.
  59. *
  60. * @return string
  61. * @throws Zend_Gdata_App_IOException
  62. */
  63. public function encode()
  64. {
  65. if ($this->getFilename() !== null &&
  66. is_readable($this->getFilename())) {
  67. // Retrieves the file, using the include path
  68. $fileHandle = fopen($this->getFilename(), 'r', true);
  69. $result = fread($fileHandle, filesize($this->getFilename()));
  70. if ($result === false) {
  71. require_once 'Zend/Gdata/App/IOException.php';
  72. throw new Zend_Gdata_App_IOException("Error reading file - " .
  73. $this->getFilename() . '. Read failed.');
  74. }
  75. fclose($fileHandle);
  76. return $result;
  77. } else {
  78. require_once 'Zend/Gdata/App/IOException.php';
  79. throw new Zend_Gdata_App_IOException("Error reading file - " .
  80. $this->getFilename() . '. File is not readable.');
  81. }
  82. }
  83. /**
  84. * Get the filename associated with this reader.
  85. *
  86. * @return string
  87. */
  88. public function getFilename()
  89. {
  90. return $this->_filename;
  91. }
  92. /**
  93. * Set the filename which is to be read.
  94. *
  95. * @param string $value The desired file handle.
  96. * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface.
  97. */
  98. public function setFilename($value)
  99. {
  100. $this->_filename = $value;
  101. return $this;
  102. }
  103. /**
  104. * The content type for the file attached (example image/png)
  105. *
  106. * @return string The content type
  107. */
  108. public function getContentType()
  109. {
  110. return $this->_contentType;
  111. }
  112. /**
  113. * Set the content type for the file attached (example image/png)
  114. *
  115. * @param string $value The content type
  116. * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface
  117. */
  118. public function setContentType($value)
  119. {
  120. $this->_contentType = $value;
  121. return $this;
  122. }
  123. /**
  124. * Alias for getFilename().
  125. *
  126. * @return string
  127. */
  128. public function __toString()
  129. {
  130. return $this->getFilename();
  131. }
  132. }