MediaFileSource.php 3.9 KB

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