performance icon indicating copy to clipboard operation
performance copied to clipboard

Remove the extension "-jpg" from the generated file name of the full sized webp version

Open rilwis opened this issue 1 year ago • 18 comments

Summary

In #545, a problem when uploading a image.jpg file is that the plugin will create the full sized version of the image with the name image-jpg.webp. This PR changes this behavior by not automatically append -jpg to the file name. The file name is only appended when there's an existing image with the same name.

Fixes #545

Relevant technical choices

Previously, the plugin relies on $editor->generate_filename. But this function always consider a suffix and add -{$suffix} to the file name. There's no way to eliminate this. So I have to create a helper function webp_uploads_generate_filename to handle this.

There is 2 things that this function consider:

  1. Whether the image size is full
  2. Whether the filter above is true

Checklist

  • [x] PR has either [Focus] or Infrastructure label.
  • [x] PR has a [Type] label.
  • [x] PR has a milestone or the no milestone label.

rilwis avatar Mar 07 '24 07:03 rilwis

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @reteinformatica, @kenny-nt, @ddur, @jamesozzie.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: reteinformatica, kenny-nt, ddur, jamesozzie.

Co-authored-by: rilwis <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: thelovekesh <[email protected]>
Co-authored-by: phanduynam <[email protected]>
Co-authored-by: PaulSchiretz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Mar 07 '24 07:03 github-actions[bot]

@rilwis Thank you for the PR.

However, I'm not sure we should apply this change. The reason the extension is added is to prevent conflicts. This was a conscious decision which actually came in response to bugs reported. For example, if a user uploads image.jpg and image.jpeg, they would both become image.webp and thus cause a conflict.

I realize this is a very specific scenario, but it is a bug that actually happened and was reported. So I'm not sure we should allow removing the original file extension suffix just for better looking file names - unless there is another reason that the suffix is a problem.

felixarntz avatar Mar 07 '24 16:03 felixarntz

@felixarntz Thanks for your comment. This PR doesn't change the default behavior that you described. When people making the change via the filter, they have to do that with a clear intention. So it is the user's problem when they upload images with the same file name like image.jpg and image.jpeg.

More details are discussed in the issue #545 and I think @mukeshpanchal27 can provide more info, because he suggested the filter.

rilwis avatar Mar 08 '24 14:03 rilwis

@rilwis Having a filter for that seems okay, but I don't find it a great solution. Filters can only be set by developers, but then users need to live with the consequences. If you're the developer and administrator of a site, that makes sense. But many sites are not like that, the people that use the site and the people that develop the site are different.

I think a better solution would be to handle it automatically, in a smart way that only applies the suffix when needed:

  • By default, don't add a suffix for the original file extension.
    • For example, if you upload image.jpg, you get image.webp.
  • However, if a file of the same name already exists, add the original extension to prevent a conflict.
    • For example, if you upload image.jpeg after uploading image.jpg, you get image-jpeg.webp in addition to image.webp.

This way, it just works, no developer intervention required, and satisfies both issues:

  • There is no chance for conflicts.
  • The file names look nicer (unless there's a conflict).

felixarntz avatar Mar 08 '24 18:03 felixarntz

@felixarntz After checking the source code of WordPress, I found that WordPress already has a mechanism to prevent duplicated file names:

https://github.com/WordPress/WordPress/blob/7ae05b77532b5902c0f67a5791abaec45220b7ca/wp-includes/functions.php#L2577

This function checks only the file name without extension and append numbers if neccessary.

So when a user uploads image.jpeg after uploading image.jpg, you'll get image-1.jpeg automatically. And with our code, it will be converted to image-1.webp. I've tested and saw it works that way.

I've updated the code below. Please take a look.

rilwis avatar Mar 08 '24 22:03 rilwis

Thanks, @rilwis, for working on the PR. I appreciate the way you implemented the changes. However, as @felixarntz mentioned, we should go with the prefix to avoid conflicts. Since the PR generates image names like WP did, I'm happy to proceed with it, but we need to get Felix's feedback on it. Once we agree, we'll need to address the unit tests as some of them are failing now. Thanks! cc @adamsilverstein

mukeshpanchal27 avatar Mar 11 '24 09:03 mukeshpanchal27

@mukeshpanchal27 Tests are updated. There's also a test for uploading image.jpeg and image.jpg.

rilwis avatar Mar 11 '24 15:03 rilwis

@rilwis

https://github.com/WordPress/WordPress/blob/7ae05b77532b5902c0f67a5791abaec45220b7ca/wp-includes/functions.php#L2577

This function checks only the file name without extension and append numbers if neccessary.

So when a user uploads image.jpeg after uploading image.jpg, you'll get image-1.jpeg automatically. And with our code, it will be converted to image-1.webp. I've tested and saw it works that way.

I'm aware of the code that does this, but as far as I remember, that function doesn't consider image.jpeg and image.jpg the same file name - because they aren't. So it only becomes a problem when you change both of their file extensions to .webp, and that happens in a completely different place in the code which currently doesn't check for duplicate file names.

But maybe I'm missing something. If so, can you point me to where that function detects those files to be the same file name even though their file extensions are different?

When you tested this, did you test it in both configurations? See Settings > Media, it needs to work both when only generating WebP as well as when generating JPEG and WebP.

felixarntz avatar Mar 11 '24 20:03 felixarntz

@rilwis Hi! Could you update the PR description to reflect the latest state of this PR? That will help us get new reviewers so we can get this merged for the next release.

westonruter avatar May 03 '24 18:05 westonruter

@westonruter Done.

rilwis avatar May 06 '24 09:05 rilwis

This is confusing because there is no error in the GHA-run tests.

@westonruter what environment are you using for this?

thelovekesh avatar May 08 '24 17:05 thelovekesh

@thelovekesh I'm testing locally with wp-env which is supposed to be the same as on GHA.

westonruter avatar May 08 '24 17:05 westonruter

Has this issue been resolved?

phanduynam avatar Sep 06 '24 07:09 phanduynam

Has this issue been resolved?

NO.

reteinformatica avatar Sep 06 '24 07:09 reteinformatica

I think we should focus on solving this problem because it has existed for a long time.

phanduynam avatar Sep 18 '24 19:09 phanduynam

please update avif image format also

phanduynam avatar Sep 18 '24 19:09 phanduynam