Switched to Index byte formatter and adjusted max file sizes.

This commit is contained in:
flash 2023-07-05 23:09:28 +00:00
parent 191ef06ef3
commit 3507ae61ff
7 changed files with 14 additions and 24 deletions

@ -1 +1 @@
Subproject commit 8f908d7f688bd95fdedd868c5bf8d25a3b500b99 Subproject commit 1cd1695429b5a313ac357f3f3faba365e425f504

View file

@ -2,6 +2,7 @@
namespace Misuzu; namespace Misuzu;
use InvalidArgumentException; use InvalidArgumentException;
use Index\ByteFormat;
use Misuzu\Parsers\Parser; use Misuzu\Parsers\Parser;
use Misuzu\Users\User; use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException; use Misuzu\Users\UserNotFoundException;
@ -183,7 +184,7 @@ if($isEditing) {
break; break;
case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_FORM_SIZE:
$notices[] = sprintf('Your avatar is not allowed to be larger in file size than %s!', byte_symbol($avatarInfo->getMaxBytes(), true)); $notices[] = sprintf('Your avatar is not allowed to be larger in file size than %s!', ByteFormat::format($avatarInfo->getMaxBytes()));
break; break;
default: default:
$notices[] = 'Unable to save your avatar, contact an administator!'; $notices[] = 'Unable to save your avatar, contact an administator!';
@ -199,7 +200,7 @@ if($isEditing) {
} catch(UserImageAssetInvalidDimensionsException $ex) { } catch(UserImageAssetInvalidDimensionsException $ex) {
$notices[] = sprintf('Your avatar can\'t be larger than %dx%d!', $avatarInfo->getMaxWidth(), $avatarInfo->getMaxHeight()); $notices[] = sprintf('Your avatar can\'t be larger than %dx%d!', $avatarInfo->getMaxWidth(), $avatarInfo->getMaxHeight());
} catch(UserImageAssetFileTooLargeException $ex) { } catch(UserImageAssetFileTooLargeException $ex) {
$notices[] = sprintf('Your avatar is not allowed to be larger in file size than %s!', byte_symbol($avatarInfo->getMaxBytes(), true)); $notices[] = sprintf('Your avatar is not allowed to be larger in file size than %s!', ByteFormat::format($avatarInfo->getMaxBytes()));
} catch(UserImageAssetException $ex) { } catch(UserImageAssetException $ex) {
$notices[] = 'Unable to save your avatar, contact an administator!'; $notices[] = 'Unable to save your avatar, contact an administator!';
} }
@ -228,7 +229,7 @@ if($isEditing) {
break; break;
case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_FORM_SIZE:
$notices[] = sprintf('Your background is not allowed to be larger in file size than %s!', byte_symbol($backgroundProps['max_size'], true)); $notices[] = sprintf('Your background is not allowed to be larger in file size than %s!', ByteFormat::format($backgroundProps['max_size']));
break; break;
default: default:
$notices[] = 'Unable to save your background, contact an administator!'; $notices[] = 'Unable to save your background, contact an administator!';
@ -244,7 +245,7 @@ if($isEditing) {
} catch(UserImageAssetInvalidDimensionsException $ex) { } catch(UserImageAssetInvalidDimensionsException $ex) {
$notices[] = sprintf('Your background can\'t be larger than %dx%d!', $backgroundInfo->getMaxWidth(), $backgroundInfo->getMaxHeight()); $notices[] = sprintf('Your background can\'t be larger than %dx%d!', $backgroundInfo->getMaxWidth(), $backgroundInfo->getMaxHeight());
} catch(UserImageAssetFileTooLargeException $ex) { } catch(UserImageAssetFileTooLargeException $ex) {
$notices[] = sprintf('Your background is not allowed to be larger in file size than %2$s!', byte_symbol($backgroundInfo->getMaxBytes(), true)); $notices[] = sprintf('Your background is not allowed to be larger in file size than %2$s!', ByteFormat::format($backgroundInfo->getMaxBytes()));
} catch(UserImageAssetException $ex) { } catch(UserImageAssetException $ex) {
$notices[] = 'Unable to save your background, contact an administator!'; $notices[] = 'Unable to save your background, contact an administator!';
} }

View file

@ -7,6 +7,7 @@ use Twig\TwigFunction;
use Twig\Environment as TwigEnvironment; use Twig\Environment as TwigEnvironment;
use Misuzu\Parsers\Parser; use Misuzu\Parsers\Parser;
use Misuzu\MisuzuContext; use Misuzu\MisuzuContext;
use Index\ByteFormat;
use Index\Environment; use Index\Environment;
final class TwigMisuzu extends AbstractExtension { final class TwigMisuzu extends AbstractExtension {
@ -20,7 +21,6 @@ final class TwigMisuzu extends AbstractExtension {
return [ return [
new TwigFilter('html_colour', 'html_colour'), new TwigFilter('html_colour', 'html_colour'),
new TwigFilter('country_name', 'get_country_name'), new TwigFilter('country_name', 'get_country_name'),
new TwigFilter('byte_symbol', 'byte_symbol'),
new TwigFilter('parse_text', fn(string $text, int $parser): string => Parser::instance($parser)->parseText($text)), new TwigFilter('parse_text', fn(string $text, int $parser): string => Parser::instance($parser)->parseText($text)),
new TwigFilter('perms_check', 'perms_check'), new TwigFilter('perms_check', 'perms_check'),
new TwigFilter('time_diff', [$this, 'timeDiff'], ['needs_environment' => true]), new TwigFilter('time_diff', [$this, 'timeDiff'], ['needs_environment' => true]),
@ -40,6 +40,7 @@ final class TwigMisuzu extends AbstractExtension {
new TwigFunction('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time), new TwigFunction('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time),
new TwigFunction('sql_query_count', fn() => DB::queries() + $this->ctx->getDbQueryCount()), new TwigFunction('sql_query_count', fn() => DB::queries() + $this->ctx->getDbQueryCount()),
new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()), new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()),
new TwigFunction('byte_symbol', fn($bytes, $decimal = ByteFormat::DECIMAL_DEFAULT) => ByteFormat::format($bytes, $decimal)),
]; ];
} }

View file

@ -17,7 +17,7 @@ class UserAvatarAsset extends UserImageAsset implements UserAssetScalableInterfa
]; ];
private const MAX_RES = 2000; private const MAX_RES = 2000;
private const MAX_BYTES = 1048576; private const MAX_BYTES = 1000000;
public function getMaxWidth(): int { public function getMaxWidth(): int {
return Config::get('avatar.max_res', IConfig::T_INT, self::MAX_RES); return Config::get('avatar.max_res', IConfig::T_INT, self::MAX_RES);

View file

@ -16,7 +16,7 @@ class UserBackgroundAsset extends UserImageAsset {
private const MAX_WIDTH = 3840; private const MAX_WIDTH = 3840;
private const MAX_HEIGHT = 2160; private const MAX_HEIGHT = 2160;
private const MAX_BYTES = 1048576; private const MAX_BYTES = 1500000;
public const ATTACH_NONE = 0x00; public const ATTACH_NONE = 0x00;
public const ATTACH_COVER = 0x01; public const ATTACH_COVER = 0x01;

View file

@ -48,19 +48,19 @@
{% if perms.edit_avatar %} {% if perms.edit_avatar %}
<ul class="profile__guidelines__section"> <ul class="profile__guidelines__section">
<li class="profile__guidelines__line profile__guidelines__line--header">Avatar</li> <li class="profile__guidelines__line profile__guidelines__line--header">Avatar</li>
<li class="profile__guidelines__line">May not exceed the <span class="profile__guidelines__emphasis">{{ profile_user.avatarInfo.maxBytes|byte_symbol() }}</span> file size limit.</li> <li class="profile__guidelines__line">May not exceed the <span class="profile__guidelines__emphasis">{{ byte_symbol(profile_user.avatarInfo.maxBytes) }}</span> file size limit.</li>
<li class="profile__guidelines__line">May not be larger than <span class="profile__guidelines__emphasis">{{ profile_user.avatarInfo.maxWidth }}x{{ profile_user.avatarInfo.maxHeight }}</span>.</li> <li class="profile__guidelines__line">May not be larger than <span class="profile__guidelines__emphasis">{{ profile_user.avatarInfo.maxWidth }}x{{ profile_user.avatarInfo.maxHeight }}</span>.</li>
<li class="profile__guidelines__line">Will be centre cropped and scaled to at most <span class="profile__guidelines__emphasis">240x240</span>.</li> <li class="profile__guidelines__line">Will be centre cropped and scaled to at most <span class="profile__guidelines__emphasis">240x240</span>.</li>
<li class="profile__guidelines__line">Animated gif images are allowed.</li> <li class="profile__guidelines__line">Animated GIF images are allowed.</li>
</ul> </ul>
{% endif %} {% endif %}
{% if perms.edit_background %} {% if perms.edit_background %}
<ul class="profile__guidelines__section"> <ul class="profile__guidelines__section">
<li class="profile__guidelines__line profile__guidelines__line--header">Background</li> <li class="profile__guidelines__line profile__guidelines__line--header">Background</li>
<li class="profile__guidelines__line">May not exceed the <span class="profile__guidelines__emphasis">{{ profile_user.backgroundInfo.maxBytes|byte_symbol() }}</span> file size limit.</li> <li class="profile__guidelines__line">May not exceed the <span class="profile__guidelines__emphasis">{{ byte_symbol(profile_user.backgroundInfo.maxBytes) }}</span> file size limit.</li>
<li class="profile__guidelines__line">May not be larger than <span class="profile__guidelines__emphasis">{{ profile_user.backgroundInfo.maxWidth }}x{{ profile_user.backgroundInfo.maxHeight }}</span>.</li> <li class="profile__guidelines__line">May not be larger than <span class="profile__guidelines__emphasis">{{ profile_user.backgroundInfo.maxWidth }}x{{ profile_user.backgroundInfo.maxHeight }}</span>.</li>
<li class="profile__guidelines__line">Gif images, in general, are only allowed when tiling.</li> <li class="profile__guidelines__line">GIF images, in general, are only allowed when tiling.</li>
</ul> </ul>
{% endif %} {% endif %}
</div> </div>

View file

@ -1,18 +1,6 @@
<?php <?php
use Index\Colour\Colour; use Index\Colour\Colour;
function byte_symbol(int $bytes, bool $decimal = false, array $symbols = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']): string {
if($bytes < 1)
return '0 B';
$divider = $decimal ? 1000 : 1024;
$exp = floor(log($bytes) / log($divider));
$bytes = $bytes / pow($divider, $exp);
$symbol = $symbols[$exp];
return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : '');
}
// render_error, render_info and render_info_or_json should be redone a bit better // render_error, render_info and render_info_or_json should be redone a bit better
// following a uniform format so there can be a global handler for em // following a uniform format so there can be a global handler for em