Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show image size limit in Frio as "usagemessage" for photo_upload #12267

Merged
merged 51 commits into from Nov 30, 2022

Conversation

MarekBenjamin
Copy link

As a first step on no image size limit shown on upload site issue , the configured limit is now printed in the already existing but previously empty usageinfo in the frio template for photos_upload.tp

The value of maximagesize in bytes is converted to MB before printing.

Please comment this request regarding code quality and practice.

@MarekBenjamin
Copy link
Author

forget to run run_xgettext.sh, fixing it.

mod/photos.php Outdated Show resolved Hide resolved
mod/photos.php Outdated Show resolved Hide resolved
MarekBenjamin and others added 3 commits November 25, 2022 21:53
Co-authored-by: Hypolite Petovan <hypolite@mrpetovan.com>
…filesize' is dominant for the actual image upload limit and print out the lower (relevant) one.
mod/photos.php Outdated
// init output var in case no number value could be retrieved.
$maximagesize_Mbytes = 0;
$sizelimitedby = '';
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this curly bracket block for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons:

  1. Got the feeling this might be a little messy to be placed inline in photos_contens.php and should be moved to a function, e.g. to be reused in the Admin Site.
  2. But until I am not sure where to put it best as a Function, I used the local block for hiding the wars to the rest of the function to emphasize that they are just used for the conversion and comparison.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the curly brackets can be removed at will, no problem at all :-)

mod/photos.php Outdated
// Get the relevant size limits for uploads. Abbreviated var names: MaxImageSize -> mis; upload_max_filesize -> umf
$mis_bytes = DI::config()->get('system', 'maximagesize');
// get_cfg_var('upload_max_filesize') outputs a string in the shorthand notation, need to convert it to bytes
$umf_string = get_cfg_var('upload_max_filesize');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ini_get() instead, it will return the computed value as an integer value or an integer string you can use directly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least not on my setup

gettype(ini_get('upload_max_filesize')): String
ini_get('upload_max_filesize'): 20M

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gasp!

Never mind then, here's a conversion function you should use (notice the 1024 factor instead of the 1000 you've been using): https://stackoverflow.com/a/6846537

You can declare it in the src/Util/Strings.php file with the signature public static function getBytesFromShorthand(string $val): int and then you can refer to it in mod/photos.php as Strings::getBytesFromShorthand().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will do so shortly.

Already thought about the binary / si prefix issue myself, which is also related to the issue: Whenever I encounter a si prefix (kB, MB, ...) I assume it is decimal prefix 10**x .
When it is about binary prefixes, shouldn't we use KiB, MiB, Gib?

But for the moment: Which interpretation of k, M, G is used in this project: 10**x (si / deicmal) or 2**x (binary). I never be sure in the k, M, G cases, only when binary prexies are used it is celar for me that they relate to 2**x :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHP manual you linked to earlier mentions the shorthands are meant to be binary prefixes.

This comment was marked as resolved.

This comment was marked as resolved.

mod/photos.php Outdated Show resolved Hide resolved
mod/photos.php Outdated Show resolved Hide resolved
src/Util/Strings.php Outdated Show resolved Hide resolved
src/Util/Strings.php Outdated Show resolved Hide resolved
@MrPetovan MrPetovan added this to the 2022.12 milestone Nov 26, 2022
MarekBenjamin and others added 4 commits November 26, 2022 23:12
Co-authored-by: Hypolite Petovan <hypolite@mrpetovan.com>
Co-authored-by: Hypolite Petovan <hypolite@mrpetovan.com>
… cluclate 2**20 for just deviding the bytes by one constant divisor.
mod/photos.php Outdated Show resolved Hide resolved
Marek Bachmann added 2 commits November 26, 2022 23:44
@MarekBenjamin MarekBenjamin marked this pull request as ready for review November 27, 2022 00:29
Marek Bachmann added 2 commits November 27, 2022 23:52
# Conflicts:
#	src/Util/Strings.php
#	view/lang/C/messages.po
@MarekBenjamin
Copy link
Author

While wrapping Strings::getBytesFromShorthand() at the remaining locations, I found that the case $maximagesize = 0 is not handled as INFINITY but as literally 0, e.g. in Photo.php:982

if ($filesize > $maximagesize) {
				Logger::notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
				return null;
			}

Should we not convert a $maximagesize = 0 to $maximagesize = INF for such cases?

@MrPetovan
Copy link
Collaborator

I've never used infinity, but feel free to do so if tests are successful.

@MarekBenjamin
Copy link
Author

Already started it, should not be a problem as long as we have INF only in the numerators.

@MarekBenjamin MarekBenjamin marked this pull request as ready for review November 30, 2022 02:03
@MarekBenjamin
Copy link
Author

For me, at the moment anything is done besides a general discussion about how we handle limit of -1 and 0 in general, but I created a new issue for that. This PR now should behave as defined with 0 meaning actually no limited and any size is accepted where ever the image size is evaluated.

@MarekBenjamin MarekBenjamin marked this pull request as draft November 30, 2022 02:58
@MarekBenjamin
Copy link
Author

There still seams to be a problem with formatBytes(int $bytes, int $precision = 2) in Strings.php:221 when no limited == INF is set. So no pull at the moment.

@MarekBenjamin
Copy link
Author

MarekBenjamin commented Nov 30, 2022

Fixed it. But there is this method Strings::formatBytes($maximagesize)) in Strings:221 which, in theory, never should be called with a maximagesize values of INF but I expect it to crash when it called with INF for the value.

Will suggest a handling for INF in the next commit.

@MarekBenjamin
Copy link
Author

Should all be fixed, upload now works for profile pics. Main Upload still broken due to another issue not addressed here

@MarekBenjamin MarekBenjamin marked this pull request as ready for review November 30, 2022 03:40
Copy link
Collaborator

@MrPetovan MrPetovan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced by the use of INF, we were already checking whether maximagesize had a value or not, which is the same condition as replacing its value by INF.


if ($maximagesize == 0) {
$maximagesize = INF;
}

if ($maximagesize && ($filesize > $maximagesize)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition was already effectively using 0 to disable the check, not sure using INF here adds anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the point, if we pass maximagesize == 0 to line 663, the processing will be discarded and since fileesize will always > 0. And this is contradicting the semantic that maximagesize == 0 should effect "no limit"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh sorry, now I get it (as you already guessed I am not too familiar with PHP ;-) ) since the AND condition is already false because PHP trades "0" as false. I get it. You are right, then it is not necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two variants of this if statement in line 663, the other one is !isempty($maxfilesize). I will adjust it by removing isempty() .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it bothered me too but I didn't want to pile on you.

Copy link
Collaborator

@MrPetovan MrPetovan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last change!

src/Module/Media/Photo/Upload.php Outdated Show resolved Hide resolved
src/Module/Media/Photo/Upload.php Outdated Show resolved Hide resolved
MarekBenjamin and others added 3 commits November 30, 2022 18:16
Co-authored-by: Hypolite Petovan <hypolite@mrpetovan.com>
Co-authored-by: Hypolite Petovan <hypolite@mrpetovan.com>
Copy link
Collaborator

@MrPetovan MrPetovan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work!

@MrPetovan MrPetovan merged commit 35ca496 into friendica:develop Nov 30, 2022
@MarekBenjamin MarekBenjamin deleted the show_image_upload_limit branch November 30, 2022 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants