From d6bec79fe72eabd827b677be52c1c95d4f51b801 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Mon, 27 Feb 2017 15:39:47 +0000 Subject: [PATCH] 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, derosj, mikeschroder, joemcgill. Fixes #39875. See #31050. Built from https://develop.svn.wordpress.org/trunk@40130 git-svn-id: http://core.svn.wordpress.org/trunk@40067 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 10 +++++++++- wp-includes/version.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 4ae53b9c3a..40cab01940 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -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. diff --git a/wp-includes/version.php b/wp-includes/version.php index c927d9f382..2f2ee9bb26 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-alpha-40129'; +$wp_version = '4.8-alpha-40130'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.