Permalink
Browse files

feature(icons): udpate file plugin to use new icon service

File thumbnails are now handled by the icon service
  • Loading branch information...
hypeJunction authored and mrclay committed Apr 11, 2016
1 parent 36c8b46 commit 2c9f5c0a47bf81b6291384145b5fe01ff7e17b72
Showing with 52 additions and 151 deletions.
  1. +9 −69 mod/file/actions/file/upload.php
  2. +6 −31 mod/file/start.php
  3. +7 −30 mod/file/thumbnail.php
  4. +30 −21 mod/file/views/default/icon/object/file.php
@@ -105,76 +105,16 @@
$guid = $file->save();
$thumb = new ElggFile();
$thumb->owner_guid = $file->owner_guid;
$sizes = [
'small' => [
'w' => 60,
'h' => 60,
'square' => true,
'metadata_name' => 'thumbnail',
'filename_prefix' => 'thumb',
],
'medium' => [
'w' => 153,
'h' => 153,
'square' => true,
'metadata_name' => 'smallthumb',
'filename_prefix' => 'smallthumb',
],
'large' => [
'w' => 600,
'h' => 600,
'square' => false,
'metadata_name' => 'largethumb',
'filename_prefix' => 'largethumb',
],
];
$remove_thumbs = function () use ($file, $sizes, $thumb) {
if (!$file->guid) {
return;
}
unset($file->icontime);
foreach ($sizes as $size => $data) {
$filename = $file->{$data['metadata_name']};
if ($filename !== null) {
$thumb->setFilename($filename);
$thumb->delete();
unset($file->{$data['metadata_name']});
}
}
};
$remove_thumbs();
$jpg_filename = pathinfo($filestorename, PATHINFO_FILENAME) . '.jpg';
if ($guid && $file->simpletype == "image") {
$file->icontime = time();
foreach ($sizes as $size => $data) {
$image_bytes = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), $data['w'], $data['h'], $data['square']);
if (!$image_bytes) {
// bail and remove any thumbs
$remove_thumbs();
break;
}
$filename = "{$prefix}{$data['filename_prefix']}{$jpg_filename}";
$thumb->setFilename($filename);
$thumb->open("write");
$thumb->write($image_bytes);
$thumb->close();
unset($image_bytes);
$file->{$data['metadata_name']} = $filename;
}
if ($guid && $file->saveIconFromElggFile($file)) {
$file->thumbnail = $file->getIcon('small')->getFilename();
$file->smallthumb = $file->getIcon('medium')->getFilename();
$file->largethumb = $file->getIcon('large')->getFilename();
} else {
$file->deleteIcon();
unset($file->thumbnail);
unset($file->smallthumb);
unset($file->largethumb);
}
} else {
// not saving a file but still need to save the entity to push attributes to database
$file->save();
View
@@ -376,30 +376,13 @@ function file_set_url($hook, $type, $url, $params) {
*/
function file_set_icon_url($hook, $type, $url, $params) {
$file = $params['entity'];
$size = $params['size'];
$size = elgg_extract('size', $params, 'large');
if (elgg_instanceof($file, 'object', 'file')) {
// thumbnails get first priority
if ($file->thumbnail) {
switch ($size) {
case "small":
$thumbfile = $file->thumbnail;
break;
case "medium":
$thumbfile = $file->smallthumb;
break;
case "large":
default:
$thumbfile = $file->largethumb;
break;
}
$readfile = new ElggFile();
$readfile->owner_guid = $file->owner_guid;
$readfile->setFilename($thumbfile);
$thumb_url = elgg_get_inline_url($readfile, true);
if ($thumb_url) {
return $thumb_url;
}
$thumbnail = $file->getIcon($size);
$thumb_url = elgg_get_inline_url($thumbnail, true);
if ($thumb_url) {
return $thumb_url;
}
$mapping = array(
@@ -471,15 +454,7 @@ function file_handle_object_delete($event, $type, ElggObject $file) {
return;
}
$thumbnails = array($file->thumbnail, $file->smallthumb, $file->largethumb);
foreach ($thumbnails as $thumbnail) {
if ($thumbnail) {
$delfile = new ElggFile();
$delfile->owner_guid = $file->owner_guid;
$delfile->setFilename($thumbnail);
$delfile->delete();
}
}
$file->deleteIcon();
}
/**
View
@@ -1,10 +1,10 @@
<?php
/**
* Elgg file thumbnail
*
* @package ElggFile
*/
$autoload_root = dirname(dirname(__DIR__));
if (!is_file("$autoload_root/vendor/autoload.php")) {
$autoload_root = dirname(dirname(dirname($autoload_root)));
@@ -23,35 +23,12 @@
$file = get_entity($file_guid);
$thumb_url = false;
// thumbnails get first priority
if ($file && $file->thumbnail) {
switch ($size) {
case "small":
$thumbfile = $file->thumbnail;
break;
case "medium":
$thumbfile = $file->smallthumb;
break;
case "large":
default:
$thumbfile = $file->largethumb;
break;
}
if (!empty($thumbfile)) {
$readfile = new ElggFile();
$readfile->owner_guid = $file->owner_guid;
$readfile->setFilename($thumbfile);
$thumb_url = elgg_get_inline_url($readfile, true);
if ($file) {
$thumb = $file->getIcon($size);
$thumb_url = elgg_get_inline_url($thumb, true);
if ($thumb_url) {
forward($thumb_url);
}
}
if ($thumb_url) {
forward($thumb_url);
}
header('HTTP/1.1 404 Not found');
exit;
forward('', '404');
@@ -8,37 +8,46 @@
* @uses $vars['img_class'] Optional CSS class added to img
* @uses $vars['link_class'] Optional CSS class added to link
*/
$entity = $vars['entity'];
$sizes = array('small', 'medium', 'large', 'tiny', 'master', 'topbar');
// Get size
if (!in_array($vars['size'], $sizes)) {
$vars['size'] = "medium";
$entity = elgg_extract('entity', $vars);
if (!$entity instanceof ElggFile) {
elgg_log('icon/object/file view expects an instance of ElggFile', 'ERROR');
return;
}
$title = $entity->title;
$title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8', false);
$sizes = array_keys(elgg_get_icon_sizes($entity->getType(), $entity->getSubtype()));
$size = elgg_extract('size', $vars, 'medium');
if (!in_array($size, $sizes)) {
// File plugin only capable of handling 3 sizes
// Anything that is an unknown size defaults to large
if ($size == 'topbar' || $size == 'tiny') {
$size = 'small';
} else if ($size == 'master') {
$size = 'large';
} else {
$size = "medium";
}
}
$url = $entity->getURL();
if (isset($vars['href'])) {
$url = $vars['href'];
} else {
$url = $entity->getURL();
}
$class = '';
$class = [];
if (isset($vars['img_class'])) {
$class = $vars['img_class'];
}
if ($entity->thumbnail) {
$class = "class=\"elgg-photo $class\"";
} else if ($class) {
$class = "class=\"$class\"";
$class[] = $vars['img_class'];
}
$img_src = $entity->getIconURL($vars['size']);
$img_src = elgg_format_url($img_src);
$img = "<img $class src=\"$img_src\" alt=\"$title\" />";
if ($entity->hasIcon($size)) {
$class[] = 'elgg-photo';
}
$img = elgg_view('output/img', [
'class' => $class,
'alt' => $entity->getDisplayName(),
'src' => $entity->getIconURL($size),
]);
if ($url) {
$params = array(
'href' => $url,
@@ -51,4 +60,4 @@
echo elgg_view('output/url', $params);
} else {
echo $img;
}
}

0 comments on commit 2c9f5c0

Please sign in to comment.