/** * Test the size detection for PDF files. */ function testSizePDF() { $info = binarypool_mime::getImageSize(realpath(dirname(__FILE__) . '/../res/emil_frey_logo_2.pdf')); $this->assertEqual($info['width'], '203'); $this->assertEqual($info['height'], '40'); $this->assertEqual($info['unit'], 'mm'); }
public function save($local_file, $remote_file) { $url = $this->absolutize($remote_file); // Cache the fileinfo binarypool_fileinfo::setCache($url, binarypool_fileinfo::getFileinfo($local_file)); $this->removeCache($remote_file); $this->flushCache($remote_file); $retval = $this->client->putObjectFile($local_file, $this->cfg['bucket'], $remote_file, S3::ACL_PUBLIC_READ, array(), binarypool_mime::getMimeType($local_file)); if ($retval === false) { throw new binarypool_exception(105, 500, "Could not copy file to its final destination on S3: {$remote_file}"); } return $retval; }
/** * Validates image files. * $config must be an associative array with the following keys: * - mime: Array of MIME types to allow (whitelist). */ public static function validate($source, $config = array()) { if (is_array($config)) { if (isset($config['mime']) && is_array($config['mime']) && sizeof($config['mime']) > 0) { $mime = binarypool_mime::getMimeType($source); if (!in_array($mime, $config['mime'])) { throw new binarypool_exception(119, 400, "Invalid MIME type for image. Allowed types: " . implode(', ', $config['mime'])); return false; } } } return true; }
/** * Returns information about a file. Handles URLs as well as local files. * * @param $file: A file path or URL. */ public static function getFileinfo($file) { if (isset(self::$fileinfoCache[$file])) { return self::$fileinfoCache[$file]; } $mime = null; $size = null; $sha1 = null; $fproxy = new binarypool_fileobject($file); if ($fproxy->exists()) { $mime = binarypool_mime::getMimeType($fproxy->file); $size = intval(filesize($fproxy->file)); $sha1 = sha1_file($fproxy->file); } $info = array('mime' => $mime, 'size' => $size, 'hash' => $sha1); self::$fileinfoCache[$file] = $info; return $info; }
protected function execute() { $storage = new binarypool_storage($this->bucket); $path = $this->getPath(); if (preg_match('#index\\.xml$#', $path) && $this->clientxsl) { ob_start(); $storage->sendFile($path); $content = ob_get_contents(); ob_end_clean(); $pi = '<?xml version="1.0" encoding="UTF-8"?>'; $pi .= '<?xml-stylesheet type="text/xsl" href="/static/xsl/index.xsl"?>'; $content = preg_replace('#<\\?xml version="1.0" encoding="UTF-8"\\?>#', $pi, $content); $this->sendHeaders('text/xml; charset=utf-8'); print $content; } else { $mime = binarypool_mime::getMimeType($storage->absolutize($path)); $this->sendHeaders($mime); $storage->sendFile($path); } $this->ignoreView = true; }
/** * Returns the XML representation of a single item. * * @param $file: Absolute file name on the disk for this item. * @param $rendition: Rendition name. null if this is the original. */ private function getItem($file, $rendition) { $fproxy = new binarypool_fileobject($file); if (!$fproxy->exists()) { throw new binarypool_exception(102, 404, "Referenced file in asset does not exist: {$file}"); } $fileinfo = binarypool_fileinfo::getFileinfo($file); $mime = $fileinfo['mime']; $size = $fileinfo['size']; $hash = $fileinfo['hash']; $type = is_null($this->type) ? binarypool_render::getType($mime) : $this->type; $info = binarypool_mime::getImageSize($file, $mime, $type); $isRendition = is_null($rendition) ? 'false' : 'true'; $isLandscape = $info['width'] > $info['height'] ? 'true' : 'false'; $xml = '<item type="' . $type . '" isRendition="' . $isRendition . '">'; $xml .= '<webobject isVisual="true" isAudioOnly="false" unit="' . htmlspecialchars($info['unit']) . '">'; $xml .= '<objectWidth>' . $info['width'] . '</objectWidth>'; $xml .= '<objectHeight>' . $info['height'] . '</objectHeight>'; $xml .= '</webobject>'; $xml .= '<imageinfo isLandscape="' . $isLandscape . '" unit="' . htmlspecialchars($info['unit']) . '">'; $xml .= '<width>' . $info['width'] . '</width>'; $xml .= '<height>' . $info['height'] . '</height>'; $xml .= '</imageinfo>'; if ($isRendition) { $xml .= '<rendition>' . htmlspecialchars($rendition) . '</rendition>'; } if ($this->locationAbsolute) { $xml .= '<location absolute="true">' . $file . '</location>'; } else { $xml .= '<location>' . htmlspecialchars($this->basepath . basename($file)) . '</location>'; } $xml .= '<importsource />'; $xml .= '<mimetype>' . htmlspecialchars($mime) . '</mimetype>'; $xml .= '<size>' . $size . '</size>'; $xml .= '<hash>' . htmlspecialchars($hash) . '</hash>'; $xml .= '</item>'; return $xml; }
/** * Calculates the renditions for the given file. * * @param $type: Type - the renderer is chosen based on this type. * @param $bucket: Bucket and type define the renditions which have * to be generated. * @param $original: Absolute path to the original file. * @param $outputPath: Directory where the renditions are written to. * @param $exclude: Renditions which don't have to be calculated. * @param $assetFile: Path to the asset file. Needed for asynchronous * rendition processes. * @return Associative array of hashes. The key is the rendition name, * the path is the relative path of the file. */ public static function render($type, $bucket, $original, $outputPath, $exclude = array(), $assetFile = null) { // Get configured renditions for this type $buckets = binarypool_config::getBuckets(); $renditions = array(); if (!isset($buckets[$bucket]['renditions'][$type])) { return $renditions; } // Information about the original file $mime = binarypool_mime::getMimeType($original); // Loops through the renditions configuration and generates the // renditions as specified. // // The following keys of each rendition config are considered: // - _sources: Ordered array of rendition names that should be // used as input for this rendition. If not present // of empty elements mean that the original // binary is used. // - _class: The render_base subclass to use. The class is // prefixed with 'binarypool_render_' to get the // definitive PHP class name. // - _mimes: MIME type of the original binary for which this // rendition is calculated. // // This three keys are removed from the array and the modified // array is then passed to the render class as configuration. // // The list of renditions needs to be in the correct dependency // order. If rendition `B' uses rendition `A' as a source, then // rendition `A' must come before `B'. foreach ($buckets[$bucket]['renditions'][$type] as $rendition => $renditionConfig) { // Renditions that the user uploaded are not calculated if (in_array($rendition, $exclude)) { continue; } // Rendition to be calculated? if (isset($renditionConfig['_mimes'])) { $mimesCfg = $renditionConfig['_mimes']; if (!is_array($mimesCfg)) { $mimesCfg = array($mimesCfg); } if (!in_array($mime, $mimesCfg)) { continue; } unset($renditionConfig['_mimes']); } // Get correct source file name $sourceFile = null; if (isset($renditionConfig['_sources'])) { foreach ($renditionConfig['_sources'] as $sourceRendition) { if (isset($renditions[$sourceRendition])) { $sourceFile = $renditions[$sourceRendition]; break; } else { if ($sourceRendition === '') { $sourceFile = $original; break; } } } unset($renditionConfig['_sources']); } else { $sourceFile = $original; } if (is_null($sourceFile)) { throw new binarypool_exception(106, 500, "Missing source rendition for rendition {$rendition}"); } $renditionConfig['_bucket'] = $bucket; // Get correct class name $renditionType = strtolower($type); if (isset($renditionConfig['_class'])) { $renditionType = $renditionConfig['_class']; unset($renditionConfig['_class']); } $renderingClass = "binarypool_render_" . $renditionType; // Filenames $renditionFile = $rendition; $absoluteRendition = $outputPath . $renditionFile; // Render image. Return value of rendering is the absolute path to the // rendition including file extensions. $absoluteRendition = call_user_func($renderingClass . '::render', $sourceFile, $absoluteRendition, $assetFile, $renditionConfig); if (!is_null($absoluteRendition)) { if (!file_exists($absoluteRendition)) { throw new binarypool_exception(106, 500, "Could not create rendition: {$rendition}"); } $renditions[$rendition] = $absoluteRendition; } } return $renditions; }
/** * Saves the file as original. */ private function saveOriginalFile($dir, $file, $basename) { $filename = binarypool_mime::fixExtension($file, $basename); if (empty($filename)) { $filename = 'original'; } if ($filename == 'index.xml') { $filename = 'index-document.xml'; } $filename = preg_replace('/[^-a-zA-Z0-9.]+/', '_', $filename); $this->storage->save($file, $dir . $filename); return $dir . $filename; }