63 $this->config = $config;
64 $this->hooks = $hooks;
65 $this->request = $request;
66 $this->logger = $logger;
67 $this->entities = $entities;
80 $files = $this->request->files;
86 if (!
$input instanceof UploadedFile || !
$input->isValid()) {
90 $tmp_filename =
time() .
$input->getClientOriginalName();
92 $tmp->owner_guid = $entity->guid;
93 $tmp->setFilename(
"tmp/$tmp_filename");
97 copy(
$input->getPathname(), $tmp->getFilenameOnFilestore());
104 unlink(
$input->getPathname());
125 $tmp_filename =
time() . pathinfo(
$filename, PATHINFO_BASENAME);
127 $tmp->owner_guid = $entity->guid;
128 $tmp->setFilename(
"tmp/$tmp_filename");
133 $tmp->mimetype = (
new MimeTypeDetector())->getType($tmp->getFilenameOnFilestore());
160 $tmp->owner_guid = $entity->guid;
161 $tmp->setFilename(
"tmp/$tmp_filename");
187 $entity_type = $entity->
getType();
195 $file = $this->hooks->trigger(
"entity:$type:prepare", $entity_type, [
201 $this->logger->error(
'Source file passed to ' . __METHOD__ .
' can not be resolved to a valid image');
205 $cropping_mode = ($x2 > $x1) && ($y2 > $y1);
206 if (!$cropping_mode) {
207 $this->deleteIcon($entity,
$type);
210 $success =
function()
use ($entity,
$type, $x1, $y1, $x2, $y2) {
211 if (
$type ==
'icon') {
212 $entity->icontime =
time();
213 if ($x1 || $y1 || $x2 || $y2) {
220 $this->hooks->trigger(
"entity:$type:saved", $entity->
getType(), [
230 $fail =
function()
use ($entity,
$type) {
231 $this->deleteIcon($entity,
$type);
235 $created = $this->hooks->trigger(
"entity:$type:save", $entity_type, [
244 if ($created ===
true) {
248 $sizes = $this->getSizes($entity_type, $entity_subtype,
$type);
250 foreach ($sizes as
$size => $opts) {
254 if (
$type ===
'icon' && $cropping_mode) {
256 $cropping_ratio = ($x2 - $x1) / ($y2 - $y1);
257 if ($cropping_ratio == 1 && $square ===
false) {
267 $icon->open(
'write');
272 if (is_array($opts) && empty($opts)) {
278 $destination =
$icon->getFilenameOnFilestore();
280 $resize_params = array_merge($opts,
$coords);
282 if (!
_elgg_services()->imageService->resize($source, $destination, $resize_params)) {
283 $this->logger->error(
"Failed to create {$size} icon from 284 {$file->getFilenameOnFilestore()} with coords [{$x1}, {$y1}],[{$x2}, {$y2}]");
314 $entity_type = $entity->
getType();
317 $default_icon->owner_guid = $entity->guid;
318 $default_icon->setFilename(
"icons/$type/$size.jpg");
320 $icon = $this->hooks->trigger(
"entity:$type:file", $entity_type,
$params, $default_icon);
336 $delete = $this->hooks->trigger(
"entity:$type:delete", $entity->
getType(), [
340 if ($delete ===
false) {
345 foreach ($sizes as
$size) {
346 $icon = $this->getIcon($entity, $size,
$type);
350 if (
$type ==
'icon') {
351 unset($entity->icontime);
383 $entity_type = $entity->
getType();
385 $url = $this->hooks->trigger(
"entity:$type:url", $entity_type,
$params, null);
408 if (
$icon->exists()) {
409 return $icon->getModifiedTime();
422 return $this->getIcon($entity,
$size,
$type)->exists();
434 public function getSizes($entity_type = null, $entity_subtype = null,
$type =
'icon') {
439 if (
$type ==
'icon') {
440 $sizes = $this->config->get(
'icon_sizes');
444 'entity_type' => $entity_type,
445 'entity_subtype' => $entity_subtype,
448 $sizes = $this->hooks->trigger(
"entity:$type:sizes", $entity_type,
$params, $sizes);
451 if (!is_array($sizes)) {
453 "must be an associative array of image size names and their properties");
457 $this->logger->error(
"Failed to find size configuration for image of type '$type' for entity type " .
458 "'$entity_type'. Use the 'entity:$type:sizes, $entity_type' hook to define the icon sizes");
473 $response =
new Response();
475 $response->prepare($this->request);
477 if ($allow_removing_headers) {
479 header_remove(
'Cache-Control');
480 header_remove(
'Pragma');
481 header_remove(
'Expires');
484 $path = implode(
'/', $this->request->getUrlSegments());
485 if (!preg_match(
'~serve-icon/(\d+)/(.*+)$~',
$path,
$m)) {
486 return $response->setStatusCode(400)->setContent(
'Malformatted request URL');
491 $entity = $this->entities->
get(
$guid);
493 return $response->setStatusCode(404)->setContent(
'Item does not exist');
497 if (!$thumbnail->exists()) {
498 return $response->setStatusCode(404)->setContent(
'Icon does not exist');
501 $if_none_match = $this->request->headers->get(
'if_none_match');
502 if (!empty($if_none_match)) {
504 $this->request->headers->set(
'if_none_match', str_replace(
'-gzip',
'', $if_none_match));
507 $filenameonfilestore = $thumbnail->getFilenameOnFilestore();
508 $last_updated = filemtime($filenameonfilestore);
509 $etag =
'"' . $last_updated .
'"';
511 $response->setPrivate()
516 if ($response->isNotModified($this->request)) {
523 $response =
new BinaryFileResponse($filenameonfilestore, 200,
$headers,
false,
'inline');
524 $response->prepare($this->request);
526 $response->setPrivate()
getSubtype()
Get the entity subtype.
get($name)
Return the value of an attribute or metadata.
if(!array_key_exists($filename, $text_files)) $file
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $input
elgg_get_simplecache_url($view, $subview= '')
Get the URL for the cached view.
Access to configuration values.
exists()
Returns if the file exists.
getSizes($entity_type=null, $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
getSimpleType()
Get the simple type of the file.
getCurrentTime($modifier= '')
Get the (cloned) time.
hasIcon(\ElggEntity $entity, $size, $type= 'icon')
Returns if the entity has an icon of the passed type.
$guid
Removes an admin notice.
getIconURL(ElggEntity $entity, $params=array())
Get the URL for this entity's icon.
elgg_strtolower()
Wrapper function for mb_strtolower().
saveIconFromLocalFile(ElggEntity $entity, $filename, $type= 'icon', array $coords=array())
Saves icons using a local file as the source.
getIcon($size, $type= 'icon')
Returns entity icon as an ElggIcon object The icon file may or may not exist on filestore.
getFilenameOnFilestore()
Return the filename of this file as it is/will be stored on the filestore, which may be different to ...
handleServeIconRequest($allow_removing_headers=true)
Handle request to /serve-icon handler.
elgg_get_inline_url(\ElggFile $file, $use_cookie=false, $expires= '')
Returns file's URL for inline display Suitable for displaying cacheable resources, such as user avatars.
saveIconFromUploadedFile(ElggEntity $entity, $input_name, $type= 'icon', array $coords=array())
Saves icons using an uploaded file as the source.
__construct(Config $config, PluginHooksService $hooks, Request $request, Logger $logger, EntityTable $entities)
Constructor.
getMimeType()
Get the mime type of the file.
deleteIcon(ElggEntity $entity, $type= 'icon')
Removes all icon files and metadata for the passed type of icon.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
getIconLastChange(ElggEntity $entity, $size, $type= 'icon')
Returns the timestamp of when the icon was changed.
saveIconFromElggFile(ElggEntity $entity, ElggFile $file, $type= 'icon', array $coords=array())
Saves icons using a file located in the data store as the source.
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy
Detect the MIME type of a file.
getType()
Returns the entity type.
saveIcon(ElggEntity $entity, ElggFile $file, $type= 'icon', array $coords=array())
Saves icons using a created temporary file.
trait TimeUsing
Adds methods for setting the current time (for testing)
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $coords
foreach($resources as $id=> $href) if(!empty($resources_html)) $files
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
getIcon(ElggEntity $entity, $size, $type= 'icon')
Returns entity icon as an ElggIcon object The icon file may or may not exist on filestore.
elgg_get_file_simple_type($mime_type)
Returns the category of a file from its MIME type.