Make WordPress Core

Changeset 40133 for branches/4.7


Ignore:
Timestamp:
02/27/2017 07:24:50 PM (10 years ago)
Author:
joemcgill
Message:

Media: Keep PDF previews from overwriting files.

Since support for PDF previews were added in [38949], it's possible
that the generated image file could overwrite an existing image file
with the same name. This uses wp_unique_filename() to avoid this
issue and adds a '-pdf' identifier on the end of filenames.

Props gitlost, desrosj, mikeschroder, joemcgill.
Merges [40130] and [40131] to the 4.7 branch.
Fixes #39875. See #31050.

Location:
branches/4.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-admin/includes/image.php

    r39633 r40133  
    252252
    253253                        if ( ! is_wp_error( $editor ) ) { // No support for this type of file
    254                                 $uploaded = $editor->save( $file, 'image/jpeg' );
     254                                /*
     255                                 * PDFs may have the same file filename as JPEGs.
     256                                 * Ensure the PDF preview image does not overwrite any JPEG images that already exist.
     257                                 */
     258                                $dirname = dirname( $file ) . '/';
     259                                $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
     260                                $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
     261
     262                                $uploaded = $editor->save( $preview_file, 'image/jpeg' );
    255263                                unset( $editor );
    256264
  • branches/4.7/tests/phpunit/tests/image/functions.php

    r39633 r40133  
    1919
    2020                include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' );
     21
     22                // Ensure no legacy / failed tests detritus.
     23                $folder = '/tmp/wordpress-gsoc-flyer*.{jpg,pdf}';
     24
     25                foreach ( glob( $folder, GLOB_BRACE ) as $file ) {
     26                        unlink( $file );
     27                }
    2128        }
    2229
     
    374381                        'sizes' => array(
    375382                                'thumbnail' => array(
    376                                         'file'      => "wordpress-gsoc-flyer-116x150.jpg",
     383                                        'file'      => "wordpress-gsoc-flyer-pdf-116x150.jpg",
    377384                                        'width'     => 116,
    378385                                        'height'    => 150,
     
    380387                                ),
    381388                                'medium'    => array(
    382                                         'file'      => "wordpress-gsoc-flyer-232x300.jpg",
     389                                        'file'      => "wordpress-gsoc-flyer-pdf-232x300.jpg",
    383390                                        'width'     => 232,
    384391                                        'height'    => 300,
     
    386393                                ),
    387394                                'large'     => array(
    388                                         'file'      => "wordpress-gsoc-flyer-791x1024.jpg",
     395                                        'file'      => "wordpress-gsoc-flyer-pdf-791x1024.jpg",
    389396                                        'width'     => 791,
    390397                                        'height'    => 1024,
     
    392399                                ),
    393400                                'full'      => array(
    394                                         'file'      => "wordpress-gsoc-flyer.jpg",
     401                                        'file'      => "wordpress-gsoc-flyer-pdf.jpg",
    395402                                        'width'     => 1088,
    396403                                        'height'    => 1408,
     
    404411
    405412                unlink( $test_file );
     413                foreach ( $metadata['sizes'] as $size ) {
     414                        unlink ( '/tmp/' . $size['file'] );
     415                }
    406416        }
    407417
     
    428438
    429439                $expected = array(
    430                         'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
     440                        'file'      => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
    431441                        'width'     => 77,
    432442                        'height'    => 100,
     
    442452
    443453                unlink( $test_file );
     454                foreach ( $metadata['sizes'] as $size ) {
     455                        unlink ( '/tmp/' . $size['file'] );
     456                }
    444457        }
    445458
     
    450463                return $fallback_sizes;
    451464        }
     465
     466        /**
     467         * Test PDF preview doesn't overwrite existing JPEG.
     468         * @ticket 39875
     469         */
     470        public function test_pdf_preview_doesnt_overwrite_existing_jpeg() {
     471                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     472                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     473                }
     474
     475                // Dummy JPEGs.
     476                $jpg1_path = '/tmp/test.jpg'; // Straight.
     477                file_put_contents( $jpg1_path, 'asdf' );
     478                $jpg2_path = '/tmp/test-pdf.jpg'; // With PDF marker.
     479                file_put_contents( $jpg2_path, 'fdsa' );
     480
     481                // PDF with same name as JPEG.
     482                $pdf_path = '/tmp/test.pdf';
     483                copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path );
     484
     485                $attachment_id = $this->factory->attachment->create_object( $pdf_path, 0, array(
     486                        'post_mime_type' => 'application/pdf',
     487                ) );
     488
     489                $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path );
     490                $preview_path = '/tmp/' . $metadata['sizes']['full']['file'];
     491
     492                // PDF preview didn't overwrite PDF.
     493                $this->assertNotEquals( $pdf_path, $preview_path );
     494                // PDF preview didn't overwrite JPG with same name.
     495                $this->assertNotEquals( $jpg1_path, $preview_path );
     496                $this->assertSame( 'asdf', file_get_contents( $jpg1_path ) );
     497                // PDF preview didn't overwrite PDF preview with same name.
     498                $this->assertNotEquals( $jpg2_path, $preview_path );
     499                $this->assertSame( 'fdsa', file_get_contents( $jpg2_path ) );
     500
     501                // Cleanup.
     502                unlink( $jpg1_path );
     503                unlink( $jpg2_path );
     504                unlink( $pdf_path );
     505                foreach ( $metadata['sizes'] as $size ) {
     506                        unlink( '/tmp/' . $size['file'] );
     507                }
     508        }
    452509}
Note: See TracChangeset for help on using the changeset viewer.