From bbf017e550b02f5d11e409b07e689d45c984054d Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 10 Mar 2022 13:10:02 +0000 Subject: [PATCH] =?UTF-8?q?Media:=20Store=20attachment=E2=80=99s=20file=20?= =?UTF-8?q?size=20in=20metadata.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing `filesize` function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3. This change also introduces a new helper function called, `wp_filesize`. This is a wrapper around the `filesize` php function, that adds some helpful filters and ensures the return value is an integer. Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035. Fixes #49412. Built from https://develop.svn.wordpress.org/trunk@52837 git-svn-id: http://core.svn.wordpress.org/trunk@52426 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/file.php | 38 +++++++++++++++++++ wp-admin/includes/image.php | 17 +++++++-- wp-admin/includes/media.php | 2 +- wp-includes/class-wp-image-editor-gd.php | 1 + wp-includes/class-wp-image-editor-imagick.php | 1 + wp-includes/media.php | 2 +- wp-includes/post.php | 1 + wp-includes/version.php | 2 +- 8 files changed, 57 insertions(+), 7 deletions(-) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index ddd2be849a..759b0974bb 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -2555,3 +2555,41 @@ function wp_opcache_invalidate( $filepath, $force = false ) { return false; } + +/** + * Wrapper for PHP filesize with filters and casting the result as an integer. + * + * @since 6.0.0 + * + * @link https://www.php.net/manual/en/function.filesize.php + * + * @param string $path Path to the file. + * @return int The size of the file in bytes, or 0 in the event of an error. + */ +function wp_filesize( $path ) { + /** + * Filters the result of wp_filesize before the PHP function is run. + * + * @since 6.0.0 + * + * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. + * @param string $path Path to the file. + */ + $size = apply_filters( 'pre_wp_filesize', null, $path ); + + if ( is_int( $size ) ) { + return $size; + } + + $size = (int) @filesize( $path ); + + /** + * Filters the size of the file. + * + * @since 6.0.0 + * + * @param int $size The result of PHP filesize on the file. + * @param string $path Path to the file. + */ + return (int) apply_filters( 'wp_filesize', $size, $path ); +} diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 627fdfb4b5..2818b3dd5d 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -210,6 +210,9 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me // Store the original image file name in image_meta. $image_meta['original_image'] = wp_basename( $original_file ); + // Add image file size. + $image_meta['filesize'] = wp_filesize( $new_file ); + return $image_meta; } @@ -235,10 +238,11 @@ function wp_create_image_subsizes( $file, $attachment_id ) { // Default image meta. $image_meta = array( - 'width' => $imagesize[0], - 'height' => $imagesize[1], - 'file' => _wp_relative_upload_path( $file ), - 'sizes' => array(), + 'width' => $imagesize[0], + 'height' => $imagesize[1], + 'file' => _wp_relative_upload_path( $file ), + 'filesize' => wp_filesize( $file ), + 'sizes' => array(), ); // Fetch additional metadata from EXIF/IPTC. @@ -629,6 +633,11 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { // Remove the blob of binary data from the array. unset( $metadata['image']['data'] ); + // Capture file size for cases where it has not been captured yet, such as PDFs. + if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) { + $metadata['filesize'] = wp_filesize( $file ); + } + /** * Filters the generated attachment meta data. * diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index d9dcbd5d7d..093ee69360 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -3378,7 +3378,7 @@ function attachment_submitbox_metadata() { if ( isset( $meta['filesize'] ) ) { $file_size = $meta['filesize']; } elseif ( file_exists( $file ) ) { - $file_size = filesize( $file ); + $file_size = wp_filesize( $file ); } if ( ! empty( $file_size ) ) { diff --git a/wp-includes/class-wp-image-editor-gd.php b/wp-includes/class-wp-image-editor-gd.php index f32e2b4202..f745bcf9f8 100644 --- a/wp-includes/class-wp-image-editor-gd.php +++ b/wp-includes/class-wp-image-editor-gd.php @@ -497,6 +497,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor { 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type, + 'filesize' => wp_filesize( $filename ), ); } diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index d898db553a..fea2e5e4ef 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -729,6 +729,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type, + 'filesize' => wp_filesize( $filename ), ); } diff --git a/wp-includes/media.php b/wp-includes/media.php index 5ab3cd1913..b3204c2799 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -4046,7 +4046,7 @@ function wp_prepare_attachment_for_js( $attachment ) { if ( isset( $meta['filesize'] ) ) { $bytes = $meta['filesize']; } elseif ( file_exists( $attached_file ) ) { - $bytes = filesize( $attached_file ); + $bytes = wp_filesize( $attached_file ); } else { $bytes = ''; } diff --git a/wp-includes/post.php b/wp-includes/post.php index 10dddf859b..7cf8204e05 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -6538,6 +6538,7 @@ function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { * @type array $sizes Keys are size slugs, each value is an array containing * 'file', 'width', 'height', and 'mime-type'. * @type array $image_meta Image metadata. + * @type int $filesize File size of the attachment. * } */ function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 7705c0ca83..87e1cd24f8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-52836'; +$wp_version = '6.0-alpha-52837'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.