[wp-trac] [WordPress Trac] #53668: Generated images for one file can be overwritten by another with the same name when mapping mime types for generated images

WordPress Trac noreply at wordpress.org
Tue Aug 17 18:00:15 UTC 2021


#53668: Generated images for one file can be overwritten by another with the same
name when mapping mime types for generated images
----------------------------------------------------+---------------------
 Reporter:  ianmjones                               |       Owner:  (none)
     Type:  defect (bug)                            |      Status:  new
 Priority:  normal                                  |   Milestone:  5.8.1
Component:  Media                                   |     Version:  5.8
 Severity:  normal                                  |  Resolution:
 Keywords:  has-patch needs-testing has-unit-tests  |     Focuses:
----------------------------------------------------+---------------------

Comment (by azaozz):

 53668.4.diff looks better but I still think that it's not good to run
 `wp_unique_filename()` recursively:
 - It's slower.
 - It would fire the filters several times for the same file which might
 cause problems.

 Also, this won't work well imho:
 {{{
 // If filename already versioned, get version and un-versioned filename.
 if ( preg_match( '/-(\d)$/', $fname, $matches ) ) {
      $fname  = preg_replace( '/' . $matches[0] . '$/', '', $fname );
      $number = (int) $matches[1];
 }
 }}}

 It may match an uploaded file's name and we'll end up changing the
 original file name instead of appending a dash and a number. This can get
 pretty "messy" when somebody tries to upload files named something like:
 `text-1.pdf`, `text-2.pdf`, `text-3.pdf`, etc.

 Ideally the increasing of $number and the renaming should happen all in
 the same loop. That would ensure simplicity and consistency. When checking
 alternate file names all of them, including the original name, have to be
 checked at the same time. Cannot be done in a loop one by one as there may
 be "gaps" in the numbering of existing files.

 For example: existing files like img.jpg, img-1.png, img-2.webp, img-3.png
 and converting PNG and JPEG to WebP, and uploading img.jpg again. Then the
 sub-sizes of img-3.png will be overwritten if the alt name with .png
 extension is checked before the alt name with .webp (I'll try to make
 another test for this edge case).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53668#comment:25>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list