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

@@ -262,4 +262,39 @@ class Tests_File extends WP_UnitTestCase {
return $keys;
}
/**
* @ticket 49412
* @covers ::wp_filesize
*/
function test_wp_filesize_with_nonexistent_file() {
$file = 'nonexistent/file.jpg';
$this->assertEquals( 0, wp_filesize( $file ) );
}
/**
* @ticket 49412
* @covers ::wp_filesize
*/
function test_wp_filesize() {
$file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
$this->assertEquals( filesize( $file ), wp_filesize( $file ) );
$filter = function() {
return 999;
};
add_filter( 'wp_filesize', $filter );
$this->assertEquals( 999, wp_filesize( $file ) );
$pre_filter = function() {
return 111;
};
add_filter( 'pre_wp_filesize', $pre_filter );
$this->assertEquals( 111, wp_filesize( $file ) );
}
}

View File

@@ -100,6 +100,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
);
@@ -300,6 +301,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 10,
'height' => 7,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
),
// #1
@@ -308,6 +310,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 75,
'height' => 50,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
),
// #2
@@ -316,6 +319,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 30,
'height' => 20,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
),
// #3
@@ -324,6 +328,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 45,
'height' => 400,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
),
// #4
@@ -332,6 +337,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
// #5
@@ -340,6 +346,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 55,
'height' => 37,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
),
// #6
@@ -348,6 +355,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 83,
'height' => 55,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
),
// #7
@@ -356,6 +364,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 90,
'height' => 60,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
),
// #8
@@ -364,6 +373,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 105,
'height' => 70,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
),
// #9
@@ -372,6 +382,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
'width' => 200,
'height' => 133,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
),
);

View File

@@ -90,6 +90,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
);
@@ -289,6 +290,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 10,
'height' => 7,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
),
// #1
@@ -297,6 +299,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 75,
'height' => 50,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
),
// #2
@@ -305,6 +308,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 30,
'height' => 20,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
),
// #3
@@ -313,6 +317,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 45,
'height' => 400,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
),
// #4
@@ -321,6 +326,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 50,
'height' => 33,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
),
// #5
@@ -329,6 +335,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 55,
'height' => 37,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
),
// #6
@@ -337,6 +344,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 83,
'height' => 55,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
),
// #7
@@ -345,6 +353,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 90,
'height' => 60,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
),
// #8
@@ -353,6 +362,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 105,
'height' => 70,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
),
// #9
@@ -361,6 +371,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
'width' => 200,
'height' => 133,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
),
);

View File

@@ -479,40 +479,47 @@ class Tests_Image_Functions extends WP_UnitTestCase {
$this->assertNotEmpty( $attachment_id );
$temp_dir = get_temp_dir();
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$expected = array(
'sizes' => array(
'sizes' => array(
'full' => array(
'file' => 'wordpress-gsoc-flyer-pdf.jpg',
'width' => 1088,
'height' => 1408,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
),
'medium' => array(
'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
'width' => 232,
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'width' => 791,
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'width' => 116,
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
'filesize' => wp_filesize( $test_file ),
);
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$this->assertSame( $expected, $metadata );
unlink( $test_file );
$temp_dir = get_temp_dir();
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
@@ -549,41 +556,49 @@ class Tests_Image_Functions extends WP_UnitTestCase {
$this->assertNotEmpty( $attachment_id );
$temp_dir = get_temp_dir();
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$expected = array(
'sizes' => array(
'sizes' => array(
'full' => array(
'file' => 'wordpress-gsoc-flyer-pdf.jpg',
'width' => 1088,
'height' => 1408,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
),
'medium' => array(
'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
'width' => 300,
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'width' => 791,
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'width' => 116,
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
'filesize' => wp_filesize( $test_file ),
);
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$this->assertSame( $expected, $metadata );
unlink( $test_file );
foreach ( $metadata['sizes'] as $size ) {
unlink( get_temp_dir() . $size['file'] );
unlink( $temp_dir . $size['file'] );
}
}
@@ -617,14 +632,21 @@ class Tests_Image_Functions extends WP_UnitTestCase {
add_image_size( 'test-size', 100, 100 );
add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$temp_dir = get_temp_dir();
$expected = array(
'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
'width' => 77,
'height' => 100,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
);
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
// Different environments produce slightly different filesize results.
$this->assertSame( $metadata['sizes']['test-size'], $expected );
$this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' );
$this->assertSame( $expected, $metadata['sizes']['test-size'] );
@@ -632,7 +654,6 @@ class Tests_Image_Functions extends WP_UnitTestCase {
remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
unlink( $test_file );
$temp_dir = get_temp_dir();
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}