Ejemplo n.º 1
0
 public function testGuessTypeFallback()
 {
     $files = array('generic_binary' => 'application/octet-stream', 'generic_text' => 'text/plain');
     foreach ($files as $file => $mimeType) {
         $this->assertEquals($mimeType, Mime_Type::guessType("{$this->_files}/{$file}"), "File `{$file}`.");
     }
 }
Ejemplo n.º 2
0
 protected function _load($handle)
 {
     rewind($handle);
     $this->_object = $handle;
     $this->_objectTemp = $this->_tempFile();
     file_put_contents($this->_objectTemp, $handle);
     $this->_objectType = $this->_type(Mime_Type::guessType($handle));
     $this->_targetType = $this->_objectType;
     $this->_info = $this->_info();
     return true;
 }
Ejemplo n.º 3
0
 public function testConvertToVideo()
 {
     $source = fopen("{$this->_files}/video_theora_comments.ogv", 'rb');
     $target = fopen('php://temp', 'wb');
     $subject = new Media_Process_Adapter_FfmpegShell($source);
     $subject->convert('video/mpeg');
     $result = $subject->store($target);
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
     $this->assertEquals('video/mpeg', Mime_Type::guessType($target));
     fclose($source);
     fclose($target);
 }
Ejemplo n.º 4
0
 public function testConvert()
 {
     $source = fopen("{$this->_files}/audio_vorbis_comments.ogg", 'rb');
     $target = fopen('php://temp', 'wb');
     $subject = new Media_Process_Adapter_SoxShell($source);
     $subject->convert('audio/x-wav');
     $result = $subject->store($target);
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
     $this->assertEquals('audio/x-wav', Mime_Type::guessType($target));
     fclose($source);
     fclose($target);
 }
Ejemplo n.º 5
0
 public function testConvertImageToImage()
 {
     $source = fopen("{$this->_files}/image_png.png", 'rb');
     $target = fopen('php://temp', 'wb');
     $subject = new Media_Process_Adapter_Gd($source);
     $subject->convert('image/jpeg');
     $result = $subject->store($target);
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
     $this->assertEquals('image/jpeg', Mime_Type::guessType($target));
     fclose($source);
     fclose($target);
 }
Ejemplo n.º 6
0
 public function __construct($handle)
 {
     $this->_object = new Imagick();
     // @fixme Workaround for imagick failing to work with handles before module version 3.0.
     // See http://pecl.php.net/bugs/bug.php?id=16932 for more information.
     // $this->_object->readImageFile($handle);
     $this->_object->readImageBlob(stream_get_contents($handle, -1, 0));
     $mimeType = Mime_Type::guessType($handle);
     if (!isset($this->_formatMap[$mimeType])) {
         throw new OutOfBoundsException("MIME type `{$mimeType}` cannot be mapped to a format.");
     }
     // We need to explictly `setFormat()` here, otherwise `getFormat()` returns `null`.
     $this->_object->setFormat($this->_formatMap[$mimeType]);
 }
Ejemplo n.º 7
0
 public function __construct($handle)
 {
     $mimeType = Mime_Type::guessType($handle);
     if (!isset($this->_formatMap[$mimeType])) {
         throw new OutOfBoundsException("Could not map MIME-type `{$mimeType}` to format.");
     }
     $this->_format = $this->_formatMap[$mimeType];
     $this->_object = imageCreateFromString(stream_get_contents($handle));
     if (!$this->_isResource($this->_object)) {
         throw new Exception("Was not able to create image from handle.");
     }
     if (imageIsTrueColor($this->_object)) {
         imageAlphaBlending($this->_object, false);
         imageSaveAlpha($this->_object, true);
     }
 }
Ejemplo n.º 8
0
 /**
  * Takes an array of paths and generates and array of source items.
  *
  * @param array $paths An array of  relative or absolute paths to files.
  * @param boolean $full When `true` will generate absolute URLs.
  * @return array An array of sources each one with the keys `name`, `mimeType`, `url` and `file`.
  */
 function _sources($paths, $full = false)
 {
     $sources = array();
     foreach ($paths as $path) {
         if (!($url = $this->url($path, $full))) {
             return;
         }
         if (strpos('://', $path) !== false) {
             $file = parse_url($url, PHP_URL_PATH);
         } else {
             $file = $this->file($path);
         }
         $mimeType = Mime_Type::guessType($file);
         $name = Mime_Type::guessName($mimeType);
         $sources[] = compact('name', 'mimeType', 'url', 'file');
     }
     return $sources;
 }
Ejemplo n.º 9
0
 /**
  * Generates markup to link to file
  *
  * @param string $path Absolute or partial path to a file
  * @param array $options
  * @return mixed
  * @deprecated
  */
 function link($path, $options = array())
 {
     $message = "MediaHelper::link - ";
     $message .= "All functionality related to assets has been deprecated.";
     trigger_error($message, E_USER_NOTICE);
     $default = array('inline' => true, 'restrict' => array());
     $defaultRss = array('title' => 'RSS Feed');
     if (is_bool($options)) {
         $options = array('inline' => $options);
     }
     $options = array_merge($default, $options);
     if (is_array($path) && !array_key_exists('controller', $path)) {
         $out = null;
         foreach ($path as $i) {
             $out .= $this->link($i, $options);
         }
         if (empty($out)) {
             return;
         }
         return $out;
     }
     $inline = $options['inline'];
     unset($options['inline']);
     if (!($url = $this->url($path))) {
         return;
     }
     if (strpos('://', $path) !== false) {
         $file = parse_url($url, PHP_URL_PATH);
     } else {
         $file = $this->file($path);
     }
     $mimeType = Mime_Type::guessType($file);
     $name = Mime_Type::guessName($mimeType);
     if ($options['restrict'] && !in_array($name, (array) $options['restrict'])) {
         return;
     }
     unset($options['restrict']);
     switch ($mimeType) {
         case 'text/css':
             $out = sprintf($this->tags['csslink'], $url, $this->_parseAttributes($options, null, '', ' '));
             return $this->output($out, $inline);
         case 'application/javascript':
         case 'application/x-javascript':
             $out = sprintf($this->tags['javascriptlink'], $url);
             return $this->output($out, $inline);
         case 'application/rss+xml':
             $options = array_merge($defaultRss, $options);
             $out = sprintf($this->tags['rsslink'], $url, $options['title']);
             return $this->output($out, $inline);
         default:
             return $this->Html->link(basename($file), $url);
     }
 }
Ejemplo n.º 10
0
 public function testMakeVersionUnknownMethodsArePassedThrough()
 {
     $config = Media_Process::config();
     $message = 'Need imagick media processing adapters configured for both image.';
     $skipped = $this->skipIf(!isset($config['image']) || $config['image'] != 'Imagick', $message);
     if ($skipped) {
         return;
     }
     $Model = ClassRegistry::init('Unicorn', 'Model');
     $Model->Behaviors->load('Media.Generator', $this->behaviorSettings);
     $directory = $this->Data->settings['filter'] . 's' . DS;
     mkdir($directory);
     $file = $this->Data->getFile(array('image-jpg.jpg' => 'image.jpg'));
     $result = $Model->Behaviors->Generator->makeVersion($Model, $file, array('version' => 's', 'directory' => $directory, 'instructions' => array('setFormat' => 'png')));
     $this->assertTrue($result);
     $mimeType = Mime_Type::guessType($directory . 'image.jpg', array('paranoid' => true));
     $this->assertEqual($mimeType, 'image/png');
 }
Ejemplo n.º 11
0
 public function testGuessTypePreferredTypes()
 {
     $result = Mime_Type::guessType('test.ogg');
     $this->assertEquals('audio/ogg', $result);
 }
Ejemplo n.º 12
0
 public function __construct($handle)
 {
     $this->_object = $handle;
     $this->_command = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? 'ffmpeg.exe' : 'ffmpeg';
     $this->_source = $this->_target = $this->_type(Mime_Type::guessType($this->_object));
 }
Ejemplo n.º 13
0
 public function testConvertMultipageDocumentToImage()
 {
     if (!$this->_hasGhostscript()) {
         $this->markTestSkipped('The `imagick` extension lacks ghostscript support.');
     }
     $source = fopen("{$this->_files}/application_pdf_multipage.pdf", 'rb');
     $target = fopen('php://temp', 'wb');
     $subject = new Media_Process_Adapter_Imagick($source);
     $subject->convert('image/jpeg');
     $result = $subject->store($target);
     $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
     $this->assertEquals('image/jpeg', Mime_Type::guessType($target));
     fclose($source);
     fclose($target);
 }
Ejemplo n.º 14
0
 /**
  * Downloads an attachment
  *
  * @param integer $id The id of the attachment
  */
 public function download($id)
 {
     $this->view = 'Media';
     $this->{$this->modelClass}->recursive = -1;
     $attachment = $this->{$this->modelClass}->read(null, $id);
     $ext = array_pop(explode('.', $attachment[$this->modelClass]['basename']));
     $params = array('id' => $attachment[$this->modelClass]['basename'], 'name' => $attachment[$this->modelClass]['alternative'], 'download' => true, 'extension' => $ext, 'mimeType' => array($ext => Mime_Type::guessType(MEDIA . $attachment[$this->modelClass]['dirname'] . DS . $attachment[$this->modelClass]['basename'])), 'path' => MEDIA_TRANSFER . $attachment[$this->modelClass]['dirname'] . DS);
     $this->set($params);
 }
Ejemplo n.º 15
0
 /**
  * Takes an array of paths and generates and array of source items.
  *
  * @param array $paths An array of  relative or absolute paths to files.
  * @param boolean $full When `true` will generate absolute URLs.
  * @return array|boolean An array of sources each one with the keys `name`, `mimeType`, `url` and `file`.
  */
 protected function _sources($paths, $full = false)
 {
     $sources = array();
     foreach ($paths as $path) {
         if (!($url = $this->url($path, $full))) {
             return false;
         }
         $file = $this->file($path);
         $mimeType = Mime_Type::guessType($file);
         $name = Mime_Type::guessName($mimeType);
         $sources[] = compact('name', 'mimeType', 'url', 'file');
     }
     return $sources;
 }
Ejemplo n.º 16
0
 /**
  * Retrieves metadata of any transferrable resource
  *
  * @param Model $Model
  * @param array|string $resource Transfer resource
  * @return array|void
  */
 function transferMeta(&$Model, $resource)
 {
     extract($this->settings[$Model->alias]);
     $defaultResource = array('scheme' => null, 'host' => null, 'port' => null, 'file' => null, 'mimeType' => null, 'size' => null, 'pixels' => null, 'permission' => null, 'dirname' => null, 'basename' => null, 'filename' => null, 'extension' => null, 'type' => null);
     /* Currently HTTP is supported only */
     if (TransferValidation::url($resource, array('scheme' => 'http'))) {
         $resource = array_merge($defaultResource, pathinfo(parse_url($resource, PHP_URL_PATH)), array('scheme' => parse_url($resource, PHP_URL_SCHEME), 'host' => parse_url($resource, PHP_URL_HOST), 'port' => parse_url($resource, PHP_URL_PORT), 'file' => $resource, 'type' => 'http-url-remote'));
         if (!class_exists('HttpSocket')) {
             App::import('Core', 'HttpSocket');
         }
         $Socket = new HttpSocket(array('timeout' => 5));
         $Socket->request(array('method' => 'HEAD', 'uri' => $resource['file']));
         if (empty($Socket->error) && $Socket->response['status']['code'] == 200) {
             $resource = array_merge($resource, array('size' => $Socket->response['header']['Content-Length'], 'mimeType' => $trustClient ? $Socket->response['header']['Content-Type'] : null, 'permission' => '0004'));
         }
     } elseif (MediaValidation::file($resource, false)) {
         $resource = array_merge($defaultResource, pathinfo($resource), array('file' => $resource, 'host' => 'localhost', 'mimeType' => Mime_Type::guessType($resource, array('paranoid' => !$trustClient))));
         if (TransferValidation::uploadedFile($resource['file'])) {
             $resource['type'] = 'uploaded-file-local';
         } else {
             $resource['type'] = 'file-local';
         }
         if (is_readable($resource['file'])) {
             $resource = array_merge($resource, array('size' => filesize($resource['file']), 'permission' => substr(sprintf('%o', fileperms($resource['file'])), -4)));
             /*
              * Because there is not better way to determine if resource is an image
              * first, we suppress a warning that would be thrown here otherwise.
              */
             if (function_exists('getimagesize')) {
                 list($width, $height) = @getimagesize($resource['file']);
                 $resource['pixels'] = $width * $height;
             }
         }
     } elseif (TransferValidation::fileUpload($resource)) {
         $resource = array_merge($defaultResource, pathinfo($resource['name']), array('file' => $resource['name'], 'host' => env('REMOTE_ADDR'), 'size' => $resource['size'], 'mimeType' => $trustClient ? $resource['type'] : null, 'permission' => '0004', 'type' => 'file-upload-remote'));
     } else {
         return null;
     }
     if (!isset($resource['filename'])) {
         /* PHP < 5.2.0 */
         $length = isset($resource['extension']) ? strlen($resource['extension']) + 1 : 0;
         $resource['filename'] = substr($resource['basename'], 0, -$length);
     }
     return $resource;
 }
Ejemplo n.º 17
0
 /**
  * Retrieve (cached) metadata of a file
  *
  * @param Model $Model
  * @param string $file An absolute path to a file
  * @param integer $level level of amount of info to add, `0` disable, `1` for basic, `2` for detailed info
  * @return mixed Array with results or false if file is not readable
  */
 function metadata(&$Model, $file, $level = 1)
 {
     if ($level < 1) {
         return array();
     }
     extract($this->settings[$Model->alias]);
     $File = new File($file);
     if (!$File->readable()) {
         return false;
     }
     $checksum = $File->md5(true);
     if (isset($this->__cached[$Model->alias][$checksum])) {
         $data = $this->__cached[$Model->alias][$checksum];
     }
     if ($level > 0 && !isset($data[1])) {
         $data[1] = array('size' => $File->size(), 'mime_type' => Mime_Type::guessType($File->pwd()), 'checksum' => $checksum);
     }
     if ($level > 1 && !isset($data[2])) {
         $data[2] = array();
         try {
             $Info = Media_Info::factory(array('source' => $File->pwd()));
             foreach ($Info->all() as $key => $value) {
                 $data[2][Inflector::underscore($key)] = $value;
             }
         } catch (Exception $E) {
         }
     }
     for ($i = $level, $result = array(); $i > 0; $i--) {
         $result = array_merge($result, $data[$i]);
     }
     $this->__cached[$Model->alias][$checksum] = $data;
     return $result;
 }
Ejemplo n.º 18
0
 public function testPassthru()
 {
     $source = fopen("{$this->_files}/image_png.png", 'rb');
     $target = fopen('php://temp', 'wb');
     $subject = new Media_Process_Adapter_Imagick($source);
     $subject->passthru('setFormat', 'jpeg');
     $result = $subject->store($target);
     $this->assertInternalType('integer', $result);
     $this->assertEquals('image/jpeg', Mime_Type::guessType($target));
     fclose($source);
     fclose($target);
 }