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.

Built from https://develop.svn.wordpress.org/branches/4.7@40133


git-svn-id: http://core.svn.wordpress.org/branches/4.7@40070 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2017-02-27 19:25:34 +00:00
parent c2a90ea28d
commit 498f71615f
2 changed files with 10 additions and 2 deletions

View File

@ -251,7 +251,15 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$editor = wp_get_image_editor( $file );
if ( ! is_wp_error( $editor ) ) { // No support for this type of file
$uploaded = $editor->save( $file, 'image/jpeg' );
/*
* PDFs may have the same file filename as JPEGs.
* Ensure the PDF preview image does not overwrite any JPEG images that already exist.
*/
$dirname = dirname( $file ) . '/';
$ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
$preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
$uploaded = $editor->save( $preview_file, 'image/jpeg' );
unset( $editor );
// Resize based on the full size image, rather than the source.

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7.3-alpha-40132';
$wp_version = '4.7.3-alpha-40133';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.