Skip to content

Commit

Permalink
feat(file): default icon sizes are now available for new image thumbs
Browse files Browse the repository at this point in the history
fixes #6181

ref #3907
  • Loading branch information
jdalsem committed Nov 3, 2017
1 parent caac710 commit cb19aff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 69 deletions.
47 changes: 15 additions & 32 deletions mod/file/actions/file/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
if (!$file instanceof ElggFile) {
return elgg_error_response(elgg_echo('file:cannotload'));
}
/* @var ElggFile $file */

// user must be able to edit file
if (!$file->canEdit()) {
Expand All @@ -51,46 +50,30 @@
$file->container_guid = $container_guid;
$file->tags = string_to_tag_array($tags);

$file->save();

if ($uploaded_file && $uploaded_file->isValid()) {
if ($file->acceptUploadedFile($uploaded_file)) {
$guid = $file->save();
// save master file
if (!$file->acceptUploadedFile($uploaded_file)) {
return elgg_error_response(elgg_echo('file:uploadfailed'));
}

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 if ($file->exists()) {
$file->save();

if (isset($reset_icon_urls)) {
// we touch the thumbs because we want new URLs from \Elgg\FileService\File::getURL
$thumbnails = [$file->thumbnail, $file->smallthumb, $file->largethumb];
foreach ($thumbnails as $thumbnail) {
$thumbfile = new ElggFile();
$thumbfile->owner_guid = $file->owner_guid;
$thumbfile->setFilename($thumbnail);
if ($thumbfile->exists()) {
$thumb_filename = $thumbfile->getFilenameOnFilestore();
touch($thumb_filename);
}
}
if (!$file->save()) {
return elgg_error_response(elgg_echo('file:uploadfailed'));
}

// update icons
$file->saveIconFromElggFile($file);

// remove legacy metadata
unset($file->thumbnail);
unset($file->smallthumb);
unset($file->largethumb);
}

// file saved so clear sticky form
elgg_clear_sticky_form('file');

if (empty($guid)) {
return elgg_error_response(elgg_echo('file:uploadfailed'));
}

$forward = $file->getURL();

// handle results differently for new files and file updates
Expand Down
44 changes: 20 additions & 24 deletions mod/file/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,25 @@ function file_set_custom_icon_sizes($hook, $type, $return, $params) {
return;
}

return [
'small' => [
'w' => 60,
'h' => 60,
'square' => true,
'upscale' => true,
],
'medium' => [
'w' => 153,
'h' => 153,
'square' => true,
'upscale' => true,
],
'large' => [
'w' => 600,
'h' => 600,
'upscale' => false,
],
$return['small'] = [
'w' => 60,
'h' => 60,
'square' => true,
'upscale' => true,
];
$return['medium'] = [
'w' => 153,
'h' => 153,
'square' => true,
'upscale' => true,
];
$return['large'] = [
'w' => 600,
'h' => 600,
'upscale' => false,
];

return $return;
}

/**
Expand All @@ -464,7 +464,7 @@ function file_set_icon_file($hook, $type, $icon, $params) {
$entity = elgg_extract('entity', $params);
$size = elgg_extract('size', $params, 'large');

if (!elgg_instanceof($entity, 'object', 'file')) {
if (!($entity instanceof \ElggFile)) {
return;
}

Expand All @@ -479,11 +479,6 @@ function file_set_icon_file($hook, $type, $icon, $params) {
$metadata_name = 'smallthumb';
break;

case 'large' :
$filename_prefix = 'largethumb';
$metadata_name = 'largethumb';
break;

default :
$filename_prefix = "{$size}thumb";
$metadata_name = $filename_prefix;
Expand All @@ -498,6 +493,7 @@ function file_set_icon_file($hook, $type, $icon, $params) {
$filename = "file/{$filename_prefix}{$filename}.jpg";
$icon->setFilename($filename);
}

return $icon;
}

Expand Down
13 changes: 0 additions & 13 deletions mod/file/views/default/icon/object/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,10 @@
*/
$entity = elgg_extract('entity', $vars);
if (!$entity instanceof ElggFile) {
elgg_log('icon/object/file view expects an instance of ElggFile', 'ERROR');
return;
}

$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 = elgg_extract('href', $vars, $entity->getURL());

Expand Down

0 comments on commit cb19aff

Please sign in to comment.