13 use Symfony\Component\HttpFoundation\BinaryFileResponse;
14 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 use Symfony\Component\HttpFoundation\Response;
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");
131 copy(
$filename, $tmp->getFilenameOnFilestore());
133 $tmp->mimetype = (
new MimeTypeDetector())->getType($tmp->getFilenameOnFilestore());
154 if (!
$file->exists()) {
158 $tmp_filename =
time() . pathinfo(
$file->getFilenameOnFilestore(), PATHINFO_BASENAME);
160 $tmp->owner_guid =
$entity->guid;
161 $tmp->setFilename(
"tmp/$tmp_filename");
164 copy(
$file->getFilenameOnFilestore(), $tmp->getFilenameOnFilestore());
166 $tmp->mimetype = (
new MimeTypeDetector())->getType($tmp->getFilenameOnFilestore(),
$file->getMimeType());
187 $entity_type =
$entity->getType();
188 $entity_subtype =
$entity->getSubtype();
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) {
210 $success =
function() use (
$entity,
$type, $x1, $y1, $x2, $y2) {
211 if (
$type ==
'icon') {
213 if ($x1 || $y1 || $x2 || $y2) {
220 $this->hooks->trigger(
"entity:$type:saved",
$entity->getType(), [
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)) {
273 copy(
$file->getFilenameOnFilestore(),
$icon->getFilenameOnFilestore());
277 $source =
$file->getFilenameOnFilestore();
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) {
350 if (
$type ==
'icon') {
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();
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');
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()
if(! $owner||!($owner instanceof ElggUser)||! $owner->canEdit()) $coords
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.
saveIcon(ElggEntity $entity, ElggFile $file, $type='icon', array $coords=array())
Saves icons using a created temporary file.
handleServeIconRequest($allow_removing_headers=true)
Handle request to /serve-icon handler.
saveIconFromLocalFile(ElggEntity $entity, $filename, $type='icon', array $coords=array())
Saves icons using a local file as the source.
saveIconFromUploadedFile(ElggEntity $entity, $input_name, $type='icon', array $coords=array())
Saves icons using an uploaded file as the source.
getIconURL(ElggEntity $entity, $params=array())
Get the URL for this entity's icon.
getSizes($entity_type=null, $entity_subtype=null, $type='icon')
Returns a configuration array of icon sizes.
getIconLastChange(ElggEntity $entity, $size, $type='icon')
Returns the timestamp of when the icon was changed.
getIcon(ElggEntity $entity, $size, $type='icon')
Returns entity icon as an ElggIcon object The icon file may or may not exist on filestore.
saveIconFromElggFile(ElggEntity $entity, ElggFile $file, $type='icon', array $coords=array())
Saves icons using a file located in the data store as the source.
deleteIcon(ElggEntity $entity, $type='icon')
Removes all icon files and metadata for the passed type of icon.
__construct(Config $config, PluginHooksService $hooks, Request $request, Logger $logger, EntityTable $entities)
Constructor.
hasIcon(\ElggEntity $entity, $size, $type='icon')
Returns if the entity has an icon of the passed type.
Detect the MIME type of a file.
$guid
Removes an admin notice.
foreach($resources as $id=> $href) if(!empty($resources_html)) $files
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
if(! $display_name) $type
elgg_get_file_simple_type($mime_type)
Returns the category of a file from its MIME type.
elgg_get_inline_url(\ElggFile $file, $use_cookie=false, $expires='')
Returns file's URL for inline display Suitable for displaying cacheable resources,...
elgg_strtolower()
Wrapper function for mb_strtolower().
getCurrentTime($modifier='')
Get the (cloned) time.
trait TimeUsing
Adds methods for setting the current time (for testing)
if(!array_key_exists($filename, $text_files)) $file