Merge pull request #12267 from MarekBenjamin/show_image_upload_limit

Show image size limit in Frio as "usagemessage" for photo_upload
This commit is contained in:
Hypolite Petovan 2022-11-30 12:24:51 -05:00 committed by GitHub
commit 35ca4961d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 402 additions and 319 deletions

View file

@ -654,7 +654,7 @@ function photos_post(App $a)
Logger::info('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes');
$maximagesize = DI::config()->get('system', 'maximagesize');
$maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
if ($maximagesize && ($filesize > $maximagesize)) {
DI::sysmsg()->addNotice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
@ -914,7 +914,20 @@ function photos_content(App $a)
'$submit' => DI::l10n()->t('Submit'),
]);
$usage_message = '';
// Get the relevant size limits for uploads. Abbreviated var names: MaxImageSize -> mis; upload_max_filesize -> umf
$mis_bytes = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
$umf_bytes = Strings::getBytesFromShorthand(ini_get('upload_max_filesize'));
// Per Friendica definition a value of '0' means unlimited:
If ($mis_bytes == 0) {
$mis_bytes = INF;
}
// When PHP is configured with upload_max_filesize less than maximagesize provide this lower limit.
$maximagesize_bytes = (is_numeric($mis_bytes) && ($mis_bytes < $umf_bytes) ? $mis_bytes : $umf_bytes);
// @todo We may be want to use appropriate binary prefixed dynamicly
$usage_message = DI::l10n()->t('The maximum accepted image size is %s', Strings::formatBytes($maximagesize_bytes));
$tpl = Renderer::getMarkupTemplate('photos_upload.tpl');

View file

@ -575,8 +575,9 @@ class Photo
$image->scaleToSquare(300);
$filesize = strlen($image->asString());
$maximagesize = DI::config()->get('system', 'maximagesize');
if (!empty($maximagesize) && ($filesize > $maximagesize)) {
$maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
if ($maximagesize && ($filesize > $maximagesize)) {
Logger::info('Avatar exceeds image limit', ['uid' => $uid, 'cid' => $cid, 'maximagesize' => $maximagesize, 'size' => $filesize, 'type' => $image->getType()]);
if ($image->getType() == 'image/gif') {
$image->toStatic();
@ -966,9 +967,9 @@ class Photo
$width = $image->getWidth();
$height = $image->getHeight();
$maximagesize = DI::config()->get('system', 'maximagesize');
$maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
if (!empty($maximagesize) && ($filesize > $maximagesize)) {
if ($maximagesize && ($filesize > $maximagesize)) {
// Scale down to multiples of 640 until the maximum size isn't exceeded anymore
foreach ([5120, 2560, 1280, 640] as $pixels) {
if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {

View file

@ -34,9 +34,11 @@ use Friendica\Model\User;
use Friendica\Module\BaseAdmin;
use Friendica\Module\Conversation\Community;
use Friendica\Module\Register;
use Friendica\Navigation\SystemMessages;
use Friendica\Protocol\Relay;
use Friendica\Util\BasePath;
use Friendica\Util\EMailer\MailBuilder;
use Friendica\Util\Strings;
class Site extends BaseAdmin
{
@ -67,7 +69,7 @@ class Site extends BaseAdmin
$language = (!empty($_POST['language']) ? trim($_POST['language']) : '');
$theme = (!empty($_POST['theme']) ? trim($_POST['theme']) : '');
$theme_mobile = (!empty($_POST['theme_mobile']) ? trim($_POST['theme_mobile']) : '');
$maximagesize = (!empty($_POST['maximagesize']) ? intval(trim($_POST['maximagesize'])) : 0);
$maximagesize = (!empty($_POST['maximagesize']) ? trim($_POST['maximagesize']) : 0);
$maximagelength = (!empty($_POST['maximagelength']) ? intval(trim($_POST['maximagelength'])) : -1);
$jpegimagequality = (!empty($_POST['jpegimagequality']) ? intval(trim($_POST['jpegimagequality'])) : 100);
@ -239,7 +241,11 @@ class Site extends BaseAdmin
} else {
DI::config()->set('system', 'singleuser', $singleuser);
}
DI::config()->set('system', 'maximagesize' , $maximagesize);
if (preg_match('/\d+(?:\s*[kmg])?/i', $maximagesize)) {
DI::config()->set('system', 'maximagesize', $maximagesize);
} else {
DI::sysmsg()->addNotice(DI::l10n()->t('%s is no valid input for maximum image size', $maximagesize));
}
DI::config()->set('system', 'max_image_length' , $maximagelength);
DI::config()->set('system', 'jpeg_quality' , $jpegimagequality);
@ -467,7 +473,10 @@ class Site extends BaseAdmin
'$show_help' => ['show_help', DI::l10n()->t('Show help entry from navigation menu'), !DI::config()->get('system', 'hide_help'), DI::l10n()->t('Displays the menu entry for the Help pages from the navigation menu. It is always accessible by calling /help directly.')],
'$singleuser' => ['singleuser', DI::l10n()->t('Single user instance'), DI::config()->get('system', 'singleuser', '---'), DI::l10n()->t('Make this instance multi-user or single-user for the named user'), $user_names],
'$maximagesize' => ['maximagesize', DI::l10n()->t('Maximum image size'), DI::config()->get('system', 'maximagesize'), DI::l10n()->t('Maximum size in bytes of uploaded images. Default is 0, which means no limits. Be aware that this setting does not affect server-side upload limits.')],
'$maximagesize' => ['maximagesize', DI::l10n()->t('Maximum image size'), DI::config()->get('system', 'maximagesize'), DI::l10n()->t('Maximum size in bytes of uploaded images. Default is 0, which means no limits. You can put k, m, or g behind the desired value for KiB, MiB, GiB, respectively.
The value of <code>upload_max_filesize</code> in your <code>PHP.ini</code> needs be set to at least the desired limit.
Currently <code>upload_max_filesize</code> is set to %s (%sB)', Strings::getBytesFromShorthand(ini_get('upload_max_filesize')), ini_get('upload_max_filesize')),
'', 'pattern="\d+(?:\s*[kmg])?"'],
'$maximagelength' => ['maximagelength', DI::l10n()->t('Maximum image length'), DI::config()->get('system', 'max_image_length'), DI::l10n()->t('Maximum length in pixels of the longest side of uploaded images. Default is -1, which means no limits.')],
'$jpegimagequality' => ['jpegimagequality', DI::l10n()->t('JPEG image quality'), DI::config()->get('system', 'jpeg_quality'), DI::l10n()->t('Uploaded JPEGS will be saved at this quality setting [0-100]. Default is 100, which is full quality.')],

View file

@ -168,9 +168,9 @@ class Upload extends \Friendica\BaseModule
$width = $image->getWidth();
$height = $image->getHeight();
$maximagesize = $this->config->get('system', 'maximagesize');
$maximagesize = Strings::getBytesFromShorthand($this->config->get('system', 'maximagesize'));
if (!empty($maximagesize) && $filesize > $maximagesize) {
if ($maximagesize && $filesize > $maximagesize) {
// Scale down to multiples of 640 until the maximum size isn't exceeded anymore
foreach ([5120, 2560, 1280, 640] as $pixels) {
if ($filesize > $maximagesize && max($width, $height) > $pixels) {

View file

@ -53,7 +53,7 @@ class Index extends BaseSettings
$filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
$maximagesize = DI::config()->get('system', 'maximagesize', 0);
$maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize', 0));
if ($maximagesize && $filesize > $maximagesize) {
DI::sysmsg()->addNotice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));

View file

@ -220,7 +220,12 @@ class Strings
*/
public static function formatBytes(int $bytes, int $precision = 2): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
// If this method is called for an infinite (== unlimited) amount of bytes:
if ($bytes == INF) {
return INF;
}
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
@ -502,4 +507,35 @@ class Strings
return $text;
}
}
/**
* This function converts a PHP's shorhand notation string for file sizes in to an integer number of total bytes.
* For example: The string for shorthand notation of '2M' (which is 2,097,152 Bytes) is converted to 2097152
* @see https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
* @param string $shorthand
* @return int
*/
public static function getBytesFromShorthand(string $shorthand): int
{
$shorthand = trim($shorthand);
if (is_numeric($shorthand)) {
return $shorthand;
}
$last = strtolower($shorthand[strlen($shorthand)-1]);
$shorthand = substr($shorthand, 0, -1);
switch($last) {
case 'g':
$shorthand *= 1024;
case 'm':
$shorthand *= 1024;
case 'k':
$shorthand *= 1024;
}
return $shorthand;
}
}

File diff suppressed because it is too large Load diff

View file

@ -40,3 +40,11 @@ tr.details th {
.adminpage td {
word-break: break-all;
}
.adminpage input[id=id_maximagesize]:valid {
background-color: palegreen;
}
.adminpage input[id=id_maximagesize]:invalid {
background-color: lightpink;
}