Make WordPress Core

Ticket #39875: 39875.2.patch

File 39875.2.patch, 5.1 KB (added by desrosj, 8 years ago)

Uses wp_unique_filename() with no random string in PDF file previews.

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

     
    251251                        $editor = wp_get_image_editor( $file );
    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 JPGs.
     256                                 * Ensure the PDF preview image does not overwrite any JPG images that already exist.
     257                                 */
     258                                $dirname = dirname( $file );
     259                                $filename = preg_replace( '/\.[^.]+$/', '', $file )  . '-pdf.pdf';
     260                                $filename = $dirname . '/' . wp_unique_filename( $dirname, wp_basename( $filename ) );
     261
     262                                $uploaded = $editor->save( $filename, 'image/jpeg' );
    255263                                unset( $editor );
    256264
    257265                                // Resize based on the full size image, rather than the source.
  • tests/phpunit/data/images/test-image.pdf

     
     1%PDF-1.1
     2%¥±ë
     3
     41 0 obj
     5  << /Type /Catalog
     6     /Pages 2 0 R
     7  >>
     8endobj
     9
     102 0 obj
     11  << /Type /Pages
     12     /Kids [3 0 R]
     13     /Count 1
     14     /MediaBox [0 0 612 792]
     15  >>
     16endobj
     17
     183 0 obj
     19  <<  /Type /Page
     20      /Parent 2 0 R
     21      /Resources
     22       << /Font
     23           << /F1
     24               << /Type /Font
     25                  /Subtype /Type1
     26                  /BaseFont /Times-Roman
     27               >>
     28           >>
     29       >>
     30      /Contents 4 0 R
     31  >>
     32endobj
     33
     344 0 obj
     35  << /Length 55 >>
     36stream
     37  BT
     38    /F1 18 Tf
     39    0 0 Td
     40    (Hello World) Tj
     41  ET
     42endstream
     43endobj
     44
     45xref
     460 5
     470000000000 65535 f
     480000000018 00000 n
     490000000077 00000 n
     500000000178 00000 n
     510000000457 00000 n
     52trailer
     53  <<  /Root 1 0 R
     54      /Size 5
     55  >>
     56startxref
     57565
     58%%EOF
  • tests/phpunit/tests/image/functions.php

    Property changes on: tests/phpunit/data/images/test-image.pdf
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    373373                $expected = array(
    374374                        'sizes' => array(
    375375                                'thumbnail' => array(
    376                                         'file'      => "wordpress-gsoc-flyer-116x150.jpg",
     376                                        'file'      => "wordpress-gsoc-flyer-pdf-116x150.jpg",
    377377                                        'width'     => 116,
    378378                                        'height'    => 150,
    379379                                        'mime-type' => "image/jpeg",
    380380                                ),
    381381                                'medium'    => array(
    382                                         'file'      => "wordpress-gsoc-flyer-232x300.jpg",
     382                                        'file'      => "wordpress-gsoc-flyer-pdf-232x300.jpg",
    383383                                        'width'     => 232,
    384384                                        'height'    => 300,
    385385                                        'mime-type' => "image/jpeg",
    386386                                ),
    387387                                'large'     => array(
    388                                         'file'      => "wordpress-gsoc-flyer-791x1024.jpg",
     388                                        'file'      => "wordpress-gsoc-flyer-pdf-791x1024.jpg",
    389389                                        'width'     => 791,
    390390                                        'height'    => 1024,
    391391                                        'mime-type' => "image/jpeg",
    392392                                ),
    393393                                'full'      => array(
    394                                         'file'      => "wordpress-gsoc-flyer.jpg",
     394                                        'file'      => "wordpress-gsoc-flyer-pdf.jpg",
    395395                                        'width'     => 1088,
    396396                                        'height'    => 1408,
    397397                                        'mime-type' => "image/jpeg",
     
    427427                add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
    428428
    429429                $expected = array(
    430                         'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
     430                        'file'      => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
    431431                        'width'     => 77,
    432432                        'height'    => 100,
    433433                        'mime-type' => 'image/jpeg',
     
    443443                unlink( $test_file );
    444444        }
    445445
     446        /**
     447         * Test PDF preview JPG does not overwrite an image with the same name.
     448         *
     449         * @ticket 39875
     450         */
     451        function test_pdf_preview_does_not_overwrite_preexisting_jpg() {
     452                $orig_jpg_file = DIR_TESTDATA . '/images/waffles.jpg';
     453                $test_jpg_file = '/tmp/waffles.jpg';
     454                copy( $orig_jpg_file, $test_jpg_file );
     455
     456                $jpg_attachment_id = $this->factory->attachment->create_object( $test_jpg_file, 0, array(
     457                        'post_mime_type' => 'image/jpeg',
     458                ) );
     459
     460                $this->assertNotEmpty( $jpg_attachment_id );
     461
     462                $jpg_metadata = wp_generate_attachment_metadata( $jpg_attachment_id, $test_jpg_file );
     463
     464                $orig_pdf_file = DIR_TESTDATA . '/images/waffles.pdf';
     465                $test_pdf_file = '/tmp/waffles.pdf';
     466                copy( $orig_pdf_file, $test_pdf_file );
     467
     468                $pdf_attachment_id = $this->factory->attachment->create_object( $test_pdf_file, 0, array(
     469                        'post_mime_type' => 'application/pdf',
     470                ) );
     471
     472                $this->assertNotEmpty( $pdf_attachment_id );
     473
     474                $pdf_metadata = wp_generate_attachment_metadata( $pdf_attachment_id, $test_pdf_file );
     475
     476                $this->assertEquals( 'waffles-pdf.jpg', $pdf_metadata['sizes']['full']['file'] );
     477
     478                unlink( $test_jpg_file );
     479                unlink( $test_pdf_file );
     480        }
     481
    446482        function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {
    447483                // Add the 'test-size' to the list of fallback sizes.
    448484                $fallback_sizes[] = 'test-size';