/** * Saves uploaded file to ElggFile with given attributes * * @param array $attributes New file attributes and metadata * @apara string $prefix Filestore prefix * @return \Upload */ public function save(array $attributes = array(), $prefix = self::DEFAULT_FILESTORE_PREFIX) { $this->error_code = $this->error; $this->error = $this->getError(); $this->filesize = $this->size; $this->path = $this->tmp_name; $this->mimetype = $this->detectMimeType(); $this->simpletype = $this->parseSimpleType(); if (!$this->isSuccessful()) { return $this; } $prefix = trim($prefix, '/'); if (!$prefix) { $prefix = self::DEFAULT_FILESTORE_PREFIX; } $id = elgg_strtolower(time() . $this->name); $filename = implode('/', array($prefix, $id)); $type = elgg_extract('type', $attributes, 'object'); $subtype = elgg_extract('subtype', $attributes, 'file'); $class = get_subtype_class($type, $subtype); if (!$class) { $class = '\\ElggFile'; } try { $filehandler = new $class(); foreach ($attributes as $key => $value) { $filehandler->{$key} = $value; } $filehandler->setFilename($filename); $filehandler->title = $this->name; $filehandler->originalfilename = $this->name; $filehandler->filesize = $this->size; $filehandler->mimetype = $this->mimetype; $filehandler->simpletype = $this->simpletype; $filehandler->open("write"); $filehandler->close(); if ($this->simpletype == 'image') { $img = new \hypeJunction\Files\Image($this->tmp_name); $img->save($filehandler->getFilenameOnFilestore(), hypeApps()->config->getSrcCompressionOpts()); } else { move_uploaded_file($this->tmp_name, $filehandler->getFilenameOnFilestore()); } if ($filehandler->save()) { $this->guid = $filehandler->getGUID(); $this->file = $filehandler; } } catch (\Exception $ex) { elgg_log($ex->getMessage(), 'ERROR'); $this->error = elgg_echo('upload:error:unknown'); } return $this; }
/** * Create icons for an entity * * @param \ElggEntity $entity Entity * @param mixed $source ElggFile, or path, or temp storage to be used as a source for icons * @param array $options Additional options * 'icon_sizes' Additional icon sizes to create * 'icon_filestore_prefix' Prefix of cropped/resizes icon sizes on the filestore * 'coords' Cropping coords * @return \ElggFile[]|false Created icons */ public function create(\ElggEntity $entity, $source = null, array $options = array()) { if (!$entity instanceof \ElggEntity) { return false; } if (!$source) { $source = $entity; } if ($source instanceof \ElggFile) { $source = $source->getFilenameOnFilestore(); } if (empty($source) || !file_exists($source)) { return false; } $coords = elgg_extract('coords', $options, false); $dir = $this->getIconDirectory($entity, elgg_extract('icon_filestore_prefix', $options)); $entity->icon_mimetype = \ElggFile::detectMimeType($source, 'image/jpeg'); $entity->icon_directory = $dir; // reset unset($entity->icontime); foreach (array('x1', 'x2', 'y1', 'y2') as $coord) { unset($entity->{$coord}); } $error = false; $icons = array(); $icons_meta = array(); $icon_sizes = $this->getSizes($entity, elgg_extract('icon_sizes', $options, array())); foreach ($icon_sizes as $size => $props) { if (!isset($props['croppable'])) { $props['croppable'] = in_array($size, $this->config->getCroppableSizes()); } if (is_array($coords) && !isset($coords['master_width'])) { $coords['master_width'] = $this->config->get('master_size_length'); $coords['master_height'] = $this->config->get('master_size_length'); } try { $icon = $this->getIconFile($entity, $size); $image = new \hypeJunction\Files\Image($source); $image->resize($props, $coords); $image->save($icon->getFilenameOnFilestore(), $this->config->getIconCompressionOpts()); $icons[$size] = $icon; if (isset($props['metadata_name'])) { $metadata_name = $props['metadata_name']; $icons_meta[$metadata_name] = $icon->getFilename(); } } catch (\Exception $ex) { elgg_log($ex->getMessage(), 'ERROR'); $error = true; } } if ($error) { foreach ($icons as $icon) { $icon->delete(); } return false; } if (!$entity instanceof \ElggFile) { // store the original icon source file $src = $this->getIconFile($entity); $srcimg = new \hypeJunction\Files\Image($source); $srcimg->save($src->getFilenameOnFilestore(), $this->config->getSrcCompressionOpts()); } if (is_array($coords)) { // store cropping coordinates foreach ($coords as $coord => $value) { $entity->{$coord} = $value; } } foreach ($icons_meta as $name => $value) { $entity->{$name} = $value; } $entity->icontime = time(); return $icons; }