Ejemplo n.º 1
0
/**
 * Maps a content type alias back to its mime-type(s)
 *
 * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
 * @return string Null on an undefined alias. String value of the mapped alias type. If an
 *   alias maps to more than one content type, the first one will be returned.
 */
	public function mapAlias($alias) {
		if (is_array($alias)) {
			return array_map(array($this, 'mapAlias'), $alias);
		}
		$type = $this->response->getMimeType($alias);
		if ($type) {
			if (is_array($type)) {
				return $type[0];
			}
			return $type;
		}
		return null;
	}
Ejemplo n.º 2
0
 /**
  * Returns an audio/video element
  *
  * ### Usage
  *
  * Using an audio file:
  *
  * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
  *
  * Outputs:
  *
  * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  *
  * Using a video file:
  *
  * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
  *
  * Outputs:
  *
  * `<video src="/files/video.mp4">Fallback text</video>`
  *
  * Using multiple video files:
  *
  * ```
  * echo $this->Html->media(
  * 		array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  * 		array('tag' => 'video', 'autoplay')
  * );
  * ```
  *
  * Outputs:
  *
  * ```
  * <video autoplay="autoplay">
  * 		<source src="/files/video.mp4" type="video/mp4"/>
  * 		<source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  * </video>
  * ```
  *
  * ### Options
  *
  * - `tag` Type of media element to generate, either "audio" or "video".
  * 	If tag is not provided it's guessed based on file's mime type.
  * - `text` Text to include inside the audio/video tag
  * - `pathPrefix` Path prefix to use for relative URLs, defaults to 'files/'
  * - `fullBase` If provided the src attribute will get a full address including domain name
  *
  * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  *  Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  * @param array $options Array of HTML attributes, and special options above.
  * @return string Generated media element
  */
 public function media($path, $options = array())
 {
     $options += array('tag' => null, 'pathPrefix' => 'files/', 'text' => '');
     if (!empty($options['tag'])) {
         $tag = $options['tag'];
     } else {
         $tag = null;
     }
     if (is_array($path)) {
         $sourceTags = '';
         foreach ($path as &$source) {
             if (is_string($source)) {
                 $source = array('src' => $source);
             }
             if (!isset($source['type'])) {
                 $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
                 $source['type'] = $this->response->getMimeType($ext);
             }
             $source['src'] = $this->assetUrl($source['src'], $options);
             $sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
         }
         unset($source);
         $options['text'] = $sourceTags . $options['text'];
         unset($options['fullBase']);
     } else {
         if (empty($path) && !empty($options['src'])) {
             $path = $options['src'];
         }
         $options['src'] = $this->assetUrl($path, $options);
     }
     if ($tag === null) {
         if (is_array($path)) {
             $mimeType = $path[0]['type'];
         } else {
             $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
         }
         if (preg_match('#^video/#', $mimeType)) {
             $tag = 'video';
         } else {
             $tag = 'audio';
         }
     }
     if (isset($options['poster'])) {
         $options['poster'] = $this->assetUrl($options['poster'], array('pathPrefix' => Configure::read('App.imageBaseUrl')) + $options);
     }
     $text = $options['text'];
     $options = array_diff_key($options, array('tag' => null, 'fullBase' => null, 'pathPrefix' => null, 'text' => null));
     return $this->tag($tag, $text, $options);
 }
 /**
  * https://developers.google.com/drive/v2/reference/files/insert
  **/
 public function insertFile($file, $driveFile, $options = array())
 {
     // setting default options
     $options = array_merge(array('convert' => 'true'), $options);
     // seting path and request
     $path = sprintf('/%s', $driveFile['id']);
     $request = array();
     $request['uri']['query'] = $options;
     $request['body'] = file_get_contents($file['tmp_name']);
     // using CakeReponse to guess mime type
     $ext = array_pop(explode('.', $file['name']));
     $CR = new CakeResponse();
     $request['header']['Content-Type'] = $CR->getMimeType($ext);
     return $this->_request($path, $request);
 }
Ejemplo n.º 4
0
 /**
  * Tests the request object constructor
  *
  * @return void
  */
 public function testConstruct()
 {
     $response = new CakeResponse();
     $this->assertNull($response->body());
     $this->assertEquals('UTF-8', $response->charset());
     $this->assertEquals('text/html', $response->type());
     $this->assertEquals(200, $response->statusCode());
     $options = array('body' => 'This is the body', 'charset' => 'my-custom-charset', 'type' => 'mp3', 'status' => '203');
     $response = new CakeResponse($options);
     $this->assertEquals('This is the body', $response->body());
     $this->assertEquals('my-custom-charset', $response->charset());
     $this->assertEquals('audio/mpeg', $response->type());
     $this->assertEquals(203, $response->statusCode());
     $options = array('body' => 'This is the body', 'charset' => 'my-custom-charset', 'type' => 'mp3', 'status' => '422', 'statusCodes' => array(422 => 'Unprocessable Entity'));
     $response = new CakeResponse($options);
     $this->assertEquals($options['body'], $response->body());
     $this->assertEquals($options['charset'], $response->charset());
     $this->assertEquals($response->getMimeType($options['type']), $response->type());
     $this->assertEquals($options['status'], $response->statusCode());
 }