public function convert($mimeType) { $sourceType = Mime_Type::guessExtension($this->_object); $targetType = Mime_Type::guessExtension($mimeType); $map = array('ogv' => 'ogg'); if (isset($map[$sourceType])) { $sourceType = $map[$sourceType]; } if (isset($map[$targetType])) { $targetType = $map[$targetType]; } $command = "{$this->_command} -f {$sourceType} -i - "; switch (Mime_Type::guessName($mimeType)) { case 'image': $command .= "-vcodec {$targetType} -vframes 1 -an -f rawvideo -"; break; case 'video': $command .= "-f {$targetType} -"; break; default: return true; } rewind($this->_object); $temporary = fopen('php://temp', 'w+b'); $descr = array(0 => $this->_object, 1 => $temporary, 2 => array('pipe', 'a')); $process = proc_open($command, $descr, $pipes); fclose($pipes[2]); $return = proc_close($process); if ($return != 0) { // throw new RuntimeException("Command `{$command}` returned `{$return}`."); return false; } $this->_object = $temporary; return true; }
public function convert($mimeType) { if (Mime_Type::guessName($mimeType) != 'audio') { return true; // others care about inter media type conversions } $sourceType = Mime_Type::guessExtension($this->_object); $targetType = Mime_Type::guessExtension($mimeType); if ($this->_compress) { // do stuff... } $command = "{$this->_command} -t {$sourceType} - -t {$targetType} -"; rewind($this->_object); $temporary = fopen('php://temp', 'w'); $descr = array(0 => $this->_object, 1 => $temporary, 2 => array('pipe', 'a')); $process = proc_open($command, $descr, $pipes); fclose($pipes[2]); $return = proc_close($process); if ($return != 0) { //var_dump(stream_get_contents($temporary, -1, 0)); // throw new RuntimeException("Command `{$command}` returned `{$return}`."); return false; } $this->_object = $temporary; return true; }
public function convert($mimeType) { if (Mime_Type::guessName($mimeType) != 'image') { return true; } if (!isset($this->_formatMap[$mimeType])) { throw new OutOfBoundsException("MIME type `{$mimeType}` cannot be mapped to a format."); } return $this->_object->setFormat($this->_formatMap[$mimeType]); }
public function convert($mimeType) { if (Mime_Type::guessName($mimeType) != 'image') { return true; } if (isset($this->_formatMap[$mimeType])) { return $this->_format = $this->_formatMap[$mimeType]; } return false; }
public function convert($mimeType) { switch (Mime_Type::guessName($mimeType)) { case 'image': $this->_options = array('vcodec' => '-vcodec ' . $this->_type($mimeType), 'vframes' => '-vframes 1', 'seek' => '-ss ' . intval($this->duration() / 4), 'noAudio' => '-an') + $this->_options; $this->_targetType = 'rawvideo'; break; case 'video': $this->_targetType = $this->_type($mimeType); break; } return true; }
public function convert($mimeType) { if (Mime_Type::guessName($mimeType) != 'audio') { return true; // others care about inter media type conversions } $sourceType = Mime_Type::guessExtension($this->_object); $targetType = Mime_Type::guessExtension($mimeType); $map = array('ogv' => 'ogg', 'oga' => 'ogg'); if (isset($map[$sourceType])) { $sourceType = $map[$sourceType]; } if (isset($map[$targetType])) { $targetType = $map[$targetType]; } $modify = null; if ($this->_sampleRate) { $modify .= " --rate {$this->_sampleRate}"; } if ($this->_channels) { $modify .= " --channels {$this->_channels}"; } rewind($this->_object); $error = fopen('php://temp', 'wrb+'); $targetFile = tempnam(sys_get_temp_dir(), 'mm_'); // Since SoX 14.3.0 multi threading is enabled which // paradoxically can cause huge slowdowns. $command = "{$this->_command} -q --single-threaded"; $command .= " -t {$sourceType} -{$modify} -t {$targetType} {$targetFile}"; $descr = array(0 => $this->_object, 1 => array('pipe', 'a'), 2 => array('pipe', 'a')); $process = proc_open($command, $descr, $pipes); fclose($pipes[1]); fclose($pipes[2]); $return = proc_close($process); // Workaround for header based formats which require the output stream to be seekable. $target = fopen($targetFile, 'rb'); $temporary = fopen('php://temp', 'wb+'); stream_copy_to_stream($target, $temporary); fclose($target); unlink($targetFile); if ($return != 0) { rewind($error); //var_dump(stream_get_contents($temporary, -1, 0)); // throw new RuntimeException("Command `{$command}` returned `{$return}`."); return false; } fclose($error); $this->_object = $temporary; return true; }
function transferTo($via, $from) { extract($from); $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } $path = $short . DS; $path .= uniqid(); // <--- This is the important part. $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
/** * Returns the relative path to the destination file * * @param array $via Information about the temporary file * @param array $from Information about the source file * @return string The path to the destination file or false */ public function transferTo($via, $from) { extract($from); $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (empty($extension)) { $extension = Mime_Type::guessExtension($mimeType ? $mimeType : $file); } if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } $path = $short . DS; $path .= strtolower(Inflector::underscore($this->model)) . DS; $path .= String::uuid(); $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
/** * This factory method takes a source or an instance of an adapter, * guesses the type of media maps it to a media information class * and instantiates it. * * @param array $config Valid values are: * - `'source'`: An absolute path to a file. * - `'adapters'`: Names or instances of media adapters (i.e. `array('Gd')`). * @return Media_Process_Generic An instance of a subclass of `Media_Process_Generic` or * if type could not be mapped an instance of the that class * itself. */ public static function &factory(array $config = array()) { $default = array('source' => null, 'adapters' => array()); extract($config + $default); if (!$source) { throw new BadMethodCallException("No source given."); } $name = Mime_Type::guessName($source); if (!$adapters) { if (!isset(self::$_config[$name])) { throw new Exception("No adapters configured for media name `{$name}`."); } $adapters = self::$_config[$name]; } $name = ucfirst($name); $class = "Media_Info_{$name}"; require_once "Media/Info/{$name}.php"; $media = new $class(compact('source', 'adapters')); return $media; }
/** * This factory method takes a source or an instance of an adapter, * guesses the type of media maps it to a media processing class * and instantiates it. * * @param array $config Valid values are: * - `'source'`: An absolute path, a file or an open handle or * a MIME type if `'adapter'` is an instance. * - `'adapter'`: A name or instance of a media adapter (i.e. `'Gd'`). * @return Media_Process_Generic An instance of a subclass of `Media_Process_Generic` or * if type could not be mapped an instance of the that class * itself. */ public static function &factory(array $config = array()) { $default = array('source' => null, 'adapter' => null); extract($config + $default); if (!$source) { throw new BadMethodCallException("No source given."); } $name = Mime_Type::guessName($source); if (!$adapter) { if (!isset(self::$_config[$name])) { throw new Exception("No adapter configured for media name `{$name}`."); } $adapter = self::$_config[$name]; } $name = ucfirst($name); $class = "Media_Process_{$name}"; if (!class_exists($class)) { // Allows for injecting arbitrary classes. require_once "Media/Process/{$name}.php"; } $media = new $class(compact('source', 'adapter')); return $media; }
/** * Returns a relative path to the destination file. * * Automatically uses 3 levels of subfolders to allow storing large numbers of files without * hitting problems with the number of files or folders in a parent folder. * * @param array $source Information about the source * @return string */ function transferTo(&$Model, $via, $from) { extract($from); $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } $unique = sha1(microtime()); $unique = $unique[0] . DS . $unique[1] . DS . substr($unique, 2) . DS; $path = $short . DS; $path .= $unique . strtolower(Inflector::slug($filename)); $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
/** * 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; }
/** * Returns a relative path to the destination file in order to determine * the final destination file name in combination with the transferDirectory * setting. * * Since there are many requirements to file name generation the behavior * implements these method which is given information about the temporary and * destination resource of the current transfer. You can reimplement this method * in your model which then will take precedence over the one provided by the * behavior. The method must return a relative path. * * The default implementation generates destination paths according to the pattern * <shortened media name>/<slugged filename>.<original extension>. However it is * also possible to do much more here, like correcting the extension using * Mime_Type::guessExtension() or multiple levels of subdirectories. * * Please note that the destination directory part of the returned path must be * existent (or creatable if createDirectory is enabled) and writable by the * server. * * @see _prepare() * @param Model $Model * @param array $via Information about the temporary resource (if used) * @param array $from Information about the source * @return string */ public function transferTo(Model $Model, $via, $from) { extract($from); /* @var $scheme string */ /* @var $host string */ /* @var $port integer */ /* @var $file string */ /* @var $mimeType string */ /* @var $size integer */ /* @var $pixels integer */ /* @var $permission string */ /* @var $dirname string */ /* @var $basename string */ /* @var $filename string */ /* @var $extension string */ /* @var $error array */ $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } $path = $short . DS; $path .= strtolower(Inflector::slug($filename)); $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
/** * Returns the configured filter array * * @param Model $Model * @param string $file * @return array */ public function filter(Model $Model, $file) { $name = Mime_Type::guessName($file); $filter = $this->settings[$Model->alias]['filter']; $filters = (array) Configure::read('Media.filter'); $default = false; if (!is_array($filter)) { if (array_key_exists($filter, $filters)) { $filter = $filters[$filter]; } else { $filter = $filters; $default = true; } } if ($default !== true && $this->settings[$Model->alias]['mergeFilter'] === true) { $filter = array_merge($filters, $filter); } // TODO Maybe trigger a notice in case no filter is defined for the given MIME-Type. if (!isset($filter[$name])) { return array(); } return $filter[$name]; }
/** * Parses instruction sets and invokes `makeVersion()` for each version on a file. * Also creates the destination directory if enabled by settings. * * If the `makeVersion()` method is implemented in the current model it'll be used * for generating a specifc version of the file (i.e. `s`, `m` or `l`) otherwise * the method within this behavior is going to be used. * * If you already have generated versions of files and change the filter * configuration afterwards you may want to recreate those files with the new * settings. * * You can achieve that by removing already generated files first (optional), than * invoking the task from the shell: * $ cake media make * * For more information on options and arguments for the task call: * $ cake media help * * @param Model $Model * @param string $file Path to a file relative to `baseDirectory` or an absolute path to a file * @return boolean */ function make(&$Model, $file) { extract($this->settings[$Model->alias]); list($file, $relativeFile) = $this->_file($Model, $file); $relativeDirectory = DS . rtrim(dirname($relativeFile), '.'); $filter = Configure::read('Media.filter.' . Mime_Type::guessName($file)); $result = true; foreach ($filter as $version => $instructions) { $directory = 'files/'; $Folder = 'files/'; try { $result = $Model->makeVersion($file, compact('version', 'directory', 'instructions')); } catch (Exception $E) { $message = "GeneratorBehavior::make - While making version `{$version}` "; $message .= "of file `{$file}` an exception was thrown, the message provided "; $message .= 'was `' . $E->getMessage() . '`. Skipping version.'; trigger_error($message, E_USER_WARNING); $result = false; } if (!$result) { $message = "GeneratorBehavior::make - The method responsible for making version "; $message .= "`{$version}` of file `{$file}` returned `false`. Skipping version."; trigger_error($message, E_USER_WARNING); $result = false; } } return $result; }
/** * Converts the media to given MIME type. * * @param string $mimeType * @return boolean|object false on error or a Media object on success */ public function convert($mimeType) { if (!$this->_adapter->convert($mimeType)) { return false; } if ($this->name() != Mime_Type::guessName($mimeType)) { // i.e. document -> image $config = Media_Process::config(); if ($config[$this->name()] == $config[Mime_Type::guessName($mimeType)]) { $media = Media_Process::factory(array('source' => $mimeType, 'adapter' => $this->_adapter)); } else { $handle = fopen('php://temp', 'w+'); if (!$this->_adapter->store($handle)) { // err } $media = Media_Process::factory(array('source' => $handle)); fclose($handle); } return $media; } return $this; }
public function testGuessNameFile() { $map = array('video_flash_snippet.flv' => 'video', 'audio_ogg_snippet.ogg' => 'audio', 'xml_snippet.xml' => 'generic', 'image_png.png' => 'image'); foreach ($map as $file => $name) { $this->assertEquals($name, Mime_Type::guessName($this->_files . '/' . $file), "File `{$file}`."); } }
/** * 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); } }
/** * Parses instruction sets and invokes `makeVersion()` for each version on a file. * Also creates the destination directory if enabled by settings. * * If the `makeVersion()` method is implemented in the current model it'll be used * for generating a specifc version of the file (i.e. `s`, `m` or `l`) otherwise * the method within this behavior is going to be used. * * If you already have generated versions of files and change the filter * configuration afterwards you may want to recreate those files with the new * settings. * * You can achieve that by removing already generated files first (optional), than * invoking the task from the shell: * $ cake media make * * For more information on options and arguments for the task call: * $ cake media help * * @param Model $Model * @param string $file Path to a file relative to `baseDirectory` or an absolute path to a file * @return boolean */ function make(&$Model, $file) { extract($this->settings[$Model->alias]); list($file, $relativeFile) = $this->_file($Model, $file); $relativeDirectory = DS . rtrim(dirname($relativeFile), '.'); $filter = Configure::read('Media.filter.' . Mime_Type::guessName($file)); $result = true; foreach ($filter as $version => $instructions) { $directory = Folder::slashTerm($filterDirectory . $version . $relativeDirectory); $Folder = new Folder($directory, $createDirectory, $createDirectoryMode); if (!$Folder->pwd()) { $message = "GeneratorBehavior::make - Directory `{$directory}` "; $message .= "could not be created or is not writable. "; $message .= "Please check the permissions."; trigger_error($message, E_USER_WARNING); $result = false; continue; } try { $result = $Model->makeVersion($file, compact('version', 'directory', 'instructions')); } catch (Exception $E) { $message = "GeneratorBehavior::make - While making version `{$version}` "; $message .= "of file `{$file}` an exception was thrown, the message provided "; $message .= 'was `' . $E->getMessage() . '`. Skipping version.'; trigger_error($message, E_USER_WARNING); $result = false; } if (!$result) { $message = "GeneratorBehavior::make - The method responsible for making version "; $message .= "`{$version}` of file `{$file}` returned `false`. Skipping version."; trigger_error($message, E_USER_WARNING); $result = false; } } return $result; }
/** * Reimplements the TransferBehavior::transferTo() method from Media plugin * * @access public * @return array */ function transferTo($via, $from) { extract($from); $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } $extension = !empty($extension) ? '.' . strtolower($extension) : null; $newFilename = uniqid('', true) . $extension; $this->data[$this->alias]['original_filename'] = $filename . $extension; return $short . DS . $newFilename; }
/** * 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; }
/** * Parses instruction sets and invokes `makeVersion()` for each version on a file. * Also creates the destination directory if enabled by settings. * * If the `makeVersion()` method is implemented in the current model it'll be used * for generating a specifc version of the file (i.e. `s`, `m` or `l`) otherwise * the method within this behavior is going to be used. * * @param Model $Model * @param string $file Path to a file relative to `baseDirectory` or an absolute path to a file * @return boolean */ function make(&$Model, $file) { extract($this->settings[$Model->alias]); list($file, $relativeFile) = $this->_file($Model, $file); $relativeDirectory = DS . rtrim(dirname($relativeFile), '.'); $filter = Configure::read('Media.filter.' . Mime_Type::guessName($file)); $result = true; foreach ($filter as $version => $instructions) { $directory = Folder::slashTerm($filterDirectory . $version . $relativeDirectory); $Folder = new Folder($directory, $createDirectory, $createDirectoryMode); if (!$Folder->pwd()) { $message = "GeneratorBehavior::generateVersion - Directory `{$directory}` "; $message .= "could not be created or is not writable. "; $message .= "Please check the permissions."; trigger_error($message, E_USER_WARNING); $result = false; continue; } if (!$Model->makeVersion($file, compact('version', 'directory', 'instructions'))) { $message = "GeneratorBehavior::make - Failed to make version `{$version}` "; $message .= "of file `{$file}`. "; trigger_error($message, E_USER_WARNING); $result = false; } } return $result; }
/** * Returns a relative path to the destination file in order to determine * the final destination file name in combination with the transferDirectory * setting. * * Since there are many requirements to file name generation the behavior * implements thise method which is given information about the temporary and * destination resource of the current transfer. You can reimplment this method * in your model which then will take precendence over the one provided by the * behavior. The method must return a relative path. * * The default implementation generates destination paths according to the pattern * <shortend media name>/<slugged filename>.<original extension>. However it is * also possible to do much more here, like correcting the extension using * Mime_Type::guessExtension() or multiple levels of subdirectories. * * Please note that the destination directory part of the returned path must be * existent (or creatable if createDirectory is enabled) and writable by the * server. * * @see _prepare() * @param Model $Model * @param array $via Information about the temporary resource (if used) * @param array $source Information about the source * @return string */ function transferTo(&$Model, $via, $from) { extract($from); $irregular = array('image' => 'img', 'text' => 'txt'); $name = Mime_Type::guessName($mimeType ? $mimeType : $file); if (isset($irregular[$name])) { $short = $irregular[$name]; } else { $short = substr($name, 0, 3); } // build a semi random path $path = $short . DS . $this->__randomPath($from['basename']); // NEVER trust an uploaded filename! //$path .= strtolower(Inflector::slug($filename)); $path .= uniqid(); $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
public function testGuessNameResource() { $handleA = fopen("{$this->_files}/application_pdf.pdf", 'rb'); $handleB = fopen('php://temp', 'rb+'); stream_copy_to_stream($handleA, $handleB); $this->assertEquals('document', Mime_Type::guessName($handleA)); $this->assertEquals('document', Mime_Type::guessName($handleB)); fclose($handleA); fclose($handleB); }