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; }
/** * Tests MIME type detection for a Flash file. */ function testMimeTypeSWF() { $this->assertEqual(binarypool_mime::getMimeType(realpath(dirname(__FILE__) . '/../res/swiss-kurier.swf')), 'application/x-shockwave-flash'); }
/** * 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; }