[44] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Zend Framework
|
---|
| 5 | *
|
---|
| 6 | * LICENSE
|
---|
| 7 | *
|
---|
| 8 | * This source file is subject to the new BSD license that is bundled
|
---|
| 9 | * with this package in the file LICENSE.txt.
|
---|
| 10 | * It is also available through the world-wide-web at this URL:
|
---|
| 11 | * http://framework.zend.com/license/new-bsd
|
---|
| 12 | * If you did not receive a copy of the license and are unable to
|
---|
| 13 | * obtain it through the world-wide-web, please send an email
|
---|
| 14 | * to license@zend.com so we can send you a copy immediately.
|
---|
| 15 | *
|
---|
| 16 | * @category Zend
|
---|
| 17 | * @package Zend_Gdata
|
---|
| 18 | * @subpackage App
|
---|
| 19 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
| 20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @see Zend_Gdata_App_MediaData
|
---|
| 25 | */
|
---|
| 26 | require_once 'Zend/Gdata/App/BaseMediaSource.php';
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Concrete class to use a file handle as an attachment within a MediaEntry.
|
---|
| 30 | *
|
---|
| 31 | * @category Zend
|
---|
| 32 | * @package Zend_Gdata
|
---|
| 33 | * @subpackage App
|
---|
| 34 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
| 35 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
| 36 | */
|
---|
| 37 | class Zend_Gdata_App_MediaFileSource extends Zend_Gdata_App_BaseMediaSource
|
---|
| 38 | {
|
---|
| 39 | /**
|
---|
| 40 | * The filename which is represented
|
---|
| 41 | *
|
---|
| 42 | * @var string
|
---|
| 43 | */
|
---|
| 44 | protected $_filename = null;
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * The content type for the file attached (example image/png)
|
---|
| 48 | *
|
---|
| 49 | * @var string
|
---|
| 50 | */
|
---|
| 51 | protected $_contentType = null;
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Create a new Zend_Gdata_App_MediaFileSource object.
|
---|
| 55 | *
|
---|
| 56 | * @param string $filename The name of the file to read from.
|
---|
| 57 | */
|
---|
| 58 | public function __construct($filename)
|
---|
| 59 | {
|
---|
| 60 | $this->setFilename($filename);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Return the MIME multipart representation of this MediaEntry.
|
---|
| 65 | *
|
---|
| 66 | * @return string
|
---|
| 67 | * @throws Zend_Gdata_App_IOException
|
---|
| 68 | */
|
---|
| 69 | public function encode()
|
---|
| 70 | {
|
---|
| 71 | if ($this->getFilename() !== null &&
|
---|
| 72 | is_readable($this->getFilename())) {
|
---|
| 73 |
|
---|
| 74 | // Retrieves the file, using the include path
|
---|
| 75 | $fileHandle = fopen($this->getFilename(), 'r', true);
|
---|
| 76 | $result = fread($fileHandle, filesize($this->getFilename()));
|
---|
| 77 | if ($result === false) {
|
---|
| 78 | require_once 'Zend/Gdata/App/IOException.php';
|
---|
| 79 | throw new Zend_Gdata_App_IOException("Error reading file - " .
|
---|
| 80 | $this->getFilename() . '. Read failed.');
|
---|
| 81 | }
|
---|
| 82 | fclose($fileHandle);
|
---|
| 83 | return $result;
|
---|
| 84 | } else {
|
---|
| 85 | require_once 'Zend/Gdata/App/IOException.php';
|
---|
| 86 | throw new Zend_Gdata_App_IOException("Error reading file - " .
|
---|
| 87 | $this->getFilename() . '. File is not readable.');
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Get the filename associated with this reader.
|
---|
| 93 | *
|
---|
| 94 | * @return string
|
---|
| 95 | */
|
---|
| 96 | public function getFilename()
|
---|
| 97 | {
|
---|
| 98 | return $this->_filename;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Set the filename which is to be read.
|
---|
| 103 | *
|
---|
| 104 | * @param string $value The desired file handle.
|
---|
| 105 | * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface.
|
---|
| 106 | */
|
---|
| 107 | public function setFilename($value)
|
---|
| 108 | {
|
---|
| 109 | $this->_filename = $value;
|
---|
| 110 | return $this;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * The content type for the file attached (example image/png)
|
---|
| 115 | *
|
---|
| 116 | * @return string The content type
|
---|
| 117 | */
|
---|
| 118 | public function getContentType()
|
---|
| 119 | {
|
---|
| 120 | return $this->_contentType;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Set the content type for the file attached (example image/png)
|
---|
| 125 | *
|
---|
| 126 | * @param string $value The content type
|
---|
| 127 | * @return Zend_Gdata_App_MediaFileSource Provides a fluent interface
|
---|
| 128 | */
|
---|
| 129 | public function setContentType($value)
|
---|
| 130 | {
|
---|
| 131 | $this->_contentType = $value;
|
---|
| 132 | return $this;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Alias for getFilename().
|
---|
| 137 | *
|
---|
| 138 | * @return string
|
---|
| 139 | */
|
---|
| 140 | public function __toString()
|
---|
| 141 | {
|
---|
| 142 | return $this->getFilename();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | }
|
---|