Media: Store attachment’s file size in metadata.

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. 



git-svn-id: https://develop.svn.wordpress.org/trunk@52837 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2022-03-10 13:08:19 +00:00
parent 4a9f5fe3be
commit 7fc225aa87
11 changed files with 142 additions and 14 deletions

View File

@@ -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.
*