/** * Constructor * * @param mixed $source * @param array $params */ public function __construct($source, $params = array()) { $this->source = $this->result = is_a($source, 'Media') ? $source : new Media($source); $this->options = array_merge(static::$defaults, $this->params($params)); $this->destination = new Obj(); $this->destination->filename = str::template($this->options['filename'], array('extension' => $this->source->extension(), 'name' => $this->source->name(), 'filename' => $this->source->filename(), 'safeName' => f::safeName($this->source->name()), 'safeFilename' => f::safeName($this->source->name()) . '.' . $this->extension(), 'width' => $this->options['width'], 'height' => $this->options['height'], 'hash' => md5($this->source->root() . $this->settingsIdentifier()))); $this->destination->url = $this->options['url'] . '/' . $this->destination->filename; $this->destination->root = $this->options['root'] . DS . $this->destination->filename; // don't create the thumbnail if it's not necessary if ($this->isObsolete()) { return; } // don't create the thumbnail if it exists if (!$this->isThere()) { // check for a valid image if (!$this->source->exists() or $this->source->type() != 'image') { throw new Error('The given image is invalid', static::ERROR_INVALID_IMAGE); } // check for a valid driver if (!array_key_exists($this->options['driver'], static::$drivers)) { throw new Error('Invalid thumbnail driver', static::ERROR_INVALID_DRIVER); } // create the thumbnail $this->create(); // check if creating the thumbnail failed if (!file_exists($this->destination->root)) { return; } } // create the result object $this->result = new Media($this->destination->root, $this->destination->url); }
/** * Renames the file and also its meta info txt * * @param string $filename */ public function rename($name) { $filename = f::safeName($name) . '.' . $this->extension(); $root = $this->dir() . DS . $filename; if (empty($name)) { throw new Exception('The filename is missing'); } if ($root == $this->root()) { return $filename; } if (file_exists($root)) { throw new Exception('A file with that name already exists'); } if (!f::move($this->root(), $root)) { throw new Exception('The file could not be renamed'); } foreach ($this->site->languages() as $lang) { // rename all meta files $meta = $this->textfile($lang->code()); if (file_exists($meta)) { f::move($meta, $this->page->textfile($filename, $lang->code())); } } return $filename; }
public function parseFileAttributes() { $source = $this->data[self::ARRAY_ATTR][self::PARA_IMG_SOURCE]; $source = is_object($source) ? $source : $this->imageExt->getPage()->file($source); if ($source) { $this->data[self::ARRAY_ATTR][self::PARA_IMG_SOURCE] = $source->filename(); $this->data[self::ARRAY_ATTR][self::PARA_IMG_SOURCE_MODIFIED] = $source->modified(); $this->data[self::ARRAY_ATTR][self::PARA_IMG_EXTENSION] = $source->extension(); $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_FILENAME] = \str::template($this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_FILENAME], array('extension' => $source->extension(), 'name' => $source->name(), 'filename' => $source->filename(), 'safeName' => \f::safeName($source->name()), 'safeFilename' => \f::safeName($source->name()) . '.' . $this->data[self::ARRAY_ATTR][self::PARA_IMG_EXTENSION], 'width' => $this->data[self::ARRAY_ATTR][self::PARA_IMG_WIDTH], 'height' => $this->data[self::ARRAY_ATTR][self::PARA_IMG_HEIGHT], 'hash' => md5($source->root() . $this->settingsIdentifier()))); $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_URL] = $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_URL] . '/' . $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_FILENAME]; $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_ROOT] = $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_ROOT] . DS . $this->data[self::ARRAY_ATTR][self::PARA_IMG_OUTPUT_FILENAME]; if ($this->data[self::ARRAY_ATTR][self::PARA_GALLERY_ID] !== false && !empty($this->data[self::ARRAY_ATTR][self::PARA_GALLERY_ID])) { $this->data[self::ARRAY_ATTR][self::PARA_LINK_URL] = $source->url(); } if (is_string($this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT]) && WebHelper::startsWith($this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT], '{file-') && WebHelper::endsWith($this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT], '}')) { $field = substr($this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT], 6, strlen($this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT]) - 7); $this->data[self::ARRAY_ATTR][self::PARA_IMG_ALT] = $source->{$field}()->toString(); } if (is_string($this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE]) && WebHelper::startsWith($this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE], '{file-') && WebHelper::endsWith($this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE], '}')) { $field = substr($this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE], 6, strlen($this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE]) - 7); $this->data[self::ARRAY_ATTR][self::PARA_IMG_TITLE] = $source->{$field}()->toString(); } if (is_string($this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE]) && WebHelper::startsWith($this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE], '{file-') && WebHelper::endsWith($this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE], '}')) { $field = substr($this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE], 6, strlen($this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE]) - 7); $this->data[self::ARRAY_ATTR][self::PARA_LINK_TITLE] = $source->{$field}()->toString(); } } }
public function to() { $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); return str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . '.' . $safeExtension, 'extension' => $extension, 'safeExtension' => $safeExtension)); }
/** * Build the destination object * * @return Obj */ public function destination() { if (is_callable($this->options['destination'])) { return call($this->options['destination'], $this); } else { $destination = new Obj(); $destination->filename = str::template($this->options['filename'], array('extension' => $this->source->extension(), 'name' => $this->source->name(), 'filename' => $this->source->filename(), 'safeName' => f::safeName($this->source->name()), 'safeFilename' => f::safeName($this->source->name()) . '.' . $this->extension(), 'width' => $this->options['width'], 'height' => $this->options['height'], 'hash' => md5($this->source->root() . $this->settingsIdentifier()))); $destination->url = $this->options['url'] . '/' . $destination->filename; $destination->root = $this->options['root'] . DS . $destination->filename; return $destination; } }
public function to() { if (!is_null($this->to)) { return $this->to; } $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); if (empty($safeExtension)) { $safeExtension = f::mimeToExtension(f::mime($source['tmp_name'])); } return $this->to = str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . r(!empty($safeExtension), '.' . $safeExtension), 'extension' => $extension, 'safeExtension' => $safeExtension)); }
/** * Constructor * * @param mixed $source * @param array $params */ public function __construct($source, $params = array()) { $this->source = $this->result = is_a($source, 'Media') ? $source : new Media($source); // set source root as default static::$defaults['url'] = $this->source->url(); static::$defaults['root'] = $this->source->dir(); $this->options = array_merge(static::$defaults, $params); $this->destination = new Obj(); $this->destination->filename = str::template($this->options['filename'], array('extension' => $this->source->extension(), 'name' => $this->source->name(), 'filename' => $this->source->filename(), 'safeName' => f::safeName($this->source->name()), 'safeFilename' => f::safeName($this->source->name()) . '.' . $this->extension(), 'width' => $this->options['width'], 'height' => $this->options['height'])); $this->destination->url = $this->options['url'] . '/' . $this->destination->filename; $this->destination->root = $this->options['root'] . DS . $this->destination->filename; // check for a valid image if (!$this->source->exists() or $this->source->type() != 'image') { throw new Exception('The given image is invalid', static::ERROR_INVALID_IMAGE); } }
/** * Returns the filename as safe name * * @return string */ public function safeName() { return f::safeName($this->filename()); }
/** * Renames the file and also its meta info txt * * @param string $filename */ public function rename($name) { $filename = f::safeName($name) . '.' . $this->extension(); $root = $this->dir() . DS . $filename; if ($root == $this->root()) { return $filename; } if (file_exists($root)) { throw new Exception('A file with that name already exists'); } if (!f::move($this->root(), $root)) { throw new Exception('The file could not be renamed'); } $meta = $this->textfile(); if (file_exists($meta)) { f::move($meta, $this->page->textfile($filename)); } cache::flush(); return $filename; }
/** * Generates a new filename for a given name * and makes sure to handle badly given extensions correctly * * @param string $name * @return string */ public function createNewFilename($name) { $name = f::safeName($name); $ext = $this->extension(); // remove possible extensions if (preg_match('!\\.[a-z0-9]{2,4}$!i', $name)) { $name = f::name($name); } return trim($name . '.' . $ext, '.'); }
/** * Generates a new filename for a given name * and makes sure to handle badly given extensions correctly * * @param string $name * @return string */ public function createNewFilename($name, $safeName = true) { $name = basename($safeName ? f::safeName($name) : $name); $ext = f::extension($name); // remove possible extensions if (in_array($ext, f::extensions())) { $name = f::name($name); } return trim($name . '.' . $this->extension(), '.'); }
public function testSafeName() { $name = 'Süper -invølid_fileßnamé!!!@2x.jpg'; $expected = '*****@*****.**'; $this->assertEquals($expected, f::safeName($name)); }
public function testAttachFromFile() { $item = $this->item(); $item->store(); $item->attach(__FILE__); $filename = f::safeName(basename(__FILE__)); $this->assertTrue(file_exists($item->root() . DS . $filename)); $this->assertEquals($filename, $item->files()->first()->filename()); }
/** * Returns the filename for a thumb including the * identifying option hash * * @param Generator $thumb * @return string */ protected function filename(Generator $thumb) { $dimensions = $this->dimensions($thumb); $wh = $dimensions->width() . 'x' . $dimensions->height(); $safeName = f::safeName($thumb->source->name()); $options = $this->options($thumb); $extension = $thumb->source->extension(); if ($thumb->options['filename'] === false) { return $safeName . '-' . $wh . r($options, '-' . $options) . '.' . $extension; } else { return str::template($thumb->options['filename'], ['extension' => $extension, 'name' => $thumb->source->name(), 'filename' => $thumb->source->filename(), 'safeName' => $safeName, 'safeFilename' => $safeName . '.' . $extension, 'width' => $dimensions->width(), 'height' => $dimensions->height(), 'dimensions' => $wh, 'options' => $options, 'hash' => md5($thumb->source->root() . $thumb->settingsIdentifier())]); } }
public function detach($filename) { $filename = f::safeName($filename); if ($filename == 'item.yaml') { throw new Exception('The item.yaml file cannot be removed'); } if (!f::remove($this->root() . DS . $filename)) { throw new Exception('The file cannot be removed'); } }