Skip to content

Commit 7fc225a

Browse files
committed
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
1 parent 4a9f5fe commit 7fc225a

11 files changed

Lines changed: 142 additions & 14 deletions

File tree

src/wp-admin/includes/file.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,3 +2555,41 @@ function wp_opcache_invalidate( $filepath, $force = false ) {
25552555

25562556
return false;
25572557
}
2558+
2559+
/**
2560+
* Wrapper for PHP filesize with filters and casting the result as an integer.
2561+
*
2562+
* @since 6.0.0
2563+
*
2564+
* @link https://www.php.net/manual/en/function.filesize.php
2565+
*
2566+
* @param string $path Path to the file.
2567+
* @return int The size of the file in bytes, or 0 in the event of an error.
2568+
*/
2569+
function wp_filesize( $path ) {
2570+
/**
2571+
* Filters the result of wp_filesize before the PHP function is run.
2572+
*
2573+
* @since 6.0.0
2574+
*
2575+
* @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call.
2576+
* @param string $path Path to the file.
2577+
*/
2578+
$size = apply_filters( 'pre_wp_filesize', null, $path );
2579+
2580+
if ( is_int( $size ) ) {
2581+
return $size;
2582+
}
2583+
2584+
$size = (int) @filesize( $path );
2585+
2586+
/**
2587+
* Filters the size of the file.
2588+
*
2589+
* @since 6.0.0
2590+
*
2591+
* @param int $size The result of PHP filesize on the file.
2592+
* @param string $path Path to the file.
2593+
*/
2594+
return (int) apply_filters( 'wp_filesize', $size, $path );
2595+
}

src/wp-admin/includes/image.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me
210210
// Store the original image file name in image_meta.
211211
$image_meta['original_image'] = wp_basename( $original_file );
212212

213+
// Add image file size.
214+
$image_meta['filesize'] = wp_filesize( $new_file );
215+
213216
return $image_meta;
214217
}
215218

@@ -235,10 +238,11 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
235238

236239
// Default image meta.
237240
$image_meta = array(
238-
'width' => $imagesize[0],
239-
'height' => $imagesize[1],
240-
'file' => _wp_relative_upload_path( $file ),
241-
'sizes' => array(),
241+
'width' => $imagesize[0],
242+
'height' => $imagesize[1],
243+
'file' => _wp_relative_upload_path( $file ),
244+
'filesize' => wp_filesize( $file ),
245+
'sizes' => array(),
242246
);
243247

244248
// Fetch additional metadata from EXIF/IPTC.
@@ -629,6 +633,11 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
629633
// Remove the blob of binary data from the array.
630634
unset( $metadata['image']['data'] );
631635

636+
// Capture file size for cases where it has not been captured yet, such as PDFs.
637+
if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) {
638+
$metadata['filesize'] = wp_filesize( $file );
639+
}
640+
632641
/**
633642
* Filters the generated attachment meta data.
634643
*

src/wp-admin/includes/media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ function attachment_submitbox_metadata() {
33783378
if ( isset( $meta['filesize'] ) ) {
33793379
$file_size = $meta['filesize'];
33803380
} elseif ( file_exists( $file ) ) {
3381-
$file_size = filesize( $file );
3381+
$file_size = wp_filesize( $file );
33823382
}
33833383

33843384
if ( ! empty( $file_size ) ) {

src/wp-includes/class-wp-image-editor-gd.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
497497
'width' => $this->size['width'],
498498
'height' => $this->size['height'],
499499
'mime-type' => $mime_type,
500+
'filesize' => wp_filesize( $filename ),
500501
);
501502
}
502503

src/wp-includes/class-wp-image-editor-imagick.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
729729
'width' => $this->size['width'],
730730
'height' => $this->size['height'],
731731
'mime-type' => $mime_type,
732+
'filesize' => wp_filesize( $filename ),
732733
);
733734
}
734735

src/wp-includes/media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4046,7 +4046,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
40464046
if ( isset( $meta['filesize'] ) ) {
40474047
$bytes = $meta['filesize'];
40484048
} elseif ( file_exists( $attached_file ) ) {
4049-
$bytes = filesize( $attached_file );
4049+
$bytes = wp_filesize( $attached_file );
40504050
} else {
40514051
$bytes = '';
40524052
}

src/wp-includes/post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6538,6 +6538,7 @@ function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
65386538
* @type array $sizes Keys are size slugs, each value is an array containing
65396539
* 'file', 'width', 'height', and 'mime-type'.
65406540
* @type array $image_meta Image metadata.
6541+
* @type int $filesize File size of the attachment.
65416542
* }
65426543
*/
65436544
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {

tests/phpunit/tests/file.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,39 @@ public function filter_trust_plus85Tq_key( $keys ) {
262262

263263
return $keys;
264264
}
265+
266+
/**
267+
* @ticket 49412
268+
* @covers ::wp_filesize
269+
*/
270+
function test_wp_filesize_with_nonexistent_file() {
271+
$file = 'nonexistent/file.jpg';
272+
$this->assertEquals( 0, wp_filesize( $file ) );
273+
}
274+
275+
/**
276+
* @ticket 49412
277+
* @covers ::wp_filesize
278+
*/
279+
function test_wp_filesize() {
280+
$file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
281+
282+
$this->assertEquals( filesize( $file ), wp_filesize( $file ) );
283+
284+
$filter = function() {
285+
return 999;
286+
};
287+
288+
add_filter( 'wp_filesize', $filter );
289+
290+
$this->assertEquals( 999, wp_filesize( $file ) );
291+
292+
$pre_filter = function() {
293+
return 111;
294+
};
295+
296+
add_filter( 'pre_wp_filesize', $pre_filter );
297+
298+
$this->assertEquals( 111, wp_filesize( $file ) );
299+
}
265300
}

tests/phpunit/tests/image/editorGd.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function test_single_multi_resize() {
100100
'width' => 50,
101101
'height' => 33,
102102
'mime-type' => 'image/jpeg',
103+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
103104
),
104105
);
105106

@@ -300,6 +301,7 @@ public function test_multi_resize() {
300301
'width' => 10,
301302
'height' => 7,
302303
'mime-type' => 'image/jpeg',
304+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
303305
),
304306

305307
// #1
@@ -308,6 +310,7 @@ public function test_multi_resize() {
308310
'width' => 75,
309311
'height' => 50,
310312
'mime-type' => 'image/jpeg',
313+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
311314
),
312315

313316
// #2
@@ -316,6 +319,7 @@ public function test_multi_resize() {
316319
'width' => 30,
317320
'height' => 20,
318321
'mime-type' => 'image/jpeg',
322+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
319323
),
320324

321325
// #3
@@ -324,6 +328,7 @@ public function test_multi_resize() {
324328
'width' => 45,
325329
'height' => 400,
326330
'mime-type' => 'image/jpeg',
331+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
327332
),
328333

329334
// #4
@@ -332,6 +337,7 @@ public function test_multi_resize() {
332337
'width' => 50,
333338
'height' => 33,
334339
'mime-type' => 'image/jpeg',
340+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
335341
),
336342

337343
// #5
@@ -340,6 +346,7 @@ public function test_multi_resize() {
340346
'width' => 55,
341347
'height' => 37,
342348
'mime-type' => 'image/jpeg',
349+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
343350
),
344351

345352
// #6
@@ -348,6 +355,7 @@ public function test_multi_resize() {
348355
'width' => 83,
349356
'height' => 55,
350357
'mime-type' => 'image/jpeg',
358+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
351359
),
352360

353361
// #7
@@ -356,6 +364,7 @@ public function test_multi_resize() {
356364
'width' => 90,
357365
'height' => 60,
358366
'mime-type' => 'image/jpeg',
367+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
359368
),
360369

361370
// #8
@@ -364,6 +373,7 @@ public function test_multi_resize() {
364373
'width' => 105,
365374
'height' => 70,
366375
'mime-type' => 'image/jpeg',
376+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
367377
),
368378

369379
// #9
@@ -372,6 +382,7 @@ public function test_multi_resize() {
372382
'width' => 200,
373383
'height' => 133,
374384
'mime-type' => 'image/jpeg',
385+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
375386
),
376387
);
377388

tests/phpunit/tests/image/editorImagick.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function test_single_multi_resize() {
9090
'width' => 50,
9191
'height' => 33,
9292
'mime-type' => 'image/jpeg',
93+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
9394
),
9495
);
9596

@@ -289,6 +290,7 @@ public function test_multi_resize() {
289290
'width' => 10,
290291
'height' => 7,
291292
'mime-type' => 'image/jpeg',
293+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
292294
),
293295

294296
// #1
@@ -297,6 +299,7 @@ public function test_multi_resize() {
297299
'width' => 75,
298300
'height' => 50,
299301
'mime-type' => 'image/jpeg',
302+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
300303
),
301304

302305
// #2
@@ -305,6 +308,7 @@ public function test_multi_resize() {
305308
'width' => 30,
306309
'height' => 20,
307310
'mime-type' => 'image/jpeg',
311+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
308312
),
309313

310314
// #3
@@ -313,6 +317,7 @@ public function test_multi_resize() {
313317
'width' => 45,
314318
'height' => 400,
315319
'mime-type' => 'image/jpeg',
320+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
316321
),
317322

318323
// #4
@@ -321,6 +326,7 @@ public function test_multi_resize() {
321326
'width' => 50,
322327
'height' => 33,
323328
'mime-type' => 'image/jpeg',
329+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
324330
),
325331

326332
// #5
@@ -329,6 +335,7 @@ public function test_multi_resize() {
329335
'width' => 55,
330336
'height' => 37,
331337
'mime-type' => 'image/jpeg',
338+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
332339
),
333340

334341
// #6
@@ -337,6 +344,7 @@ public function test_multi_resize() {
337344
'width' => 83,
338345
'height' => 55,
339346
'mime-type' => 'image/jpeg',
347+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
340348
),
341349

342350
// #7
@@ -345,6 +353,7 @@ public function test_multi_resize() {
345353
'width' => 90,
346354
'height' => 60,
347355
'mime-type' => 'image/jpeg',
356+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
348357
),
349358

350359
// #8
@@ -353,6 +362,7 @@ public function test_multi_resize() {
353362
'width' => 105,
354363
'height' => 70,
355364
'mime-type' => 'image/jpeg',
365+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
356366
),
357367

358368
// #9
@@ -361,6 +371,7 @@ public function test_multi_resize() {
361371
'width' => 200,
362372
'height' => 133,
363373
'mime-type' => 'image/jpeg',
374+
'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
364375
),
365376
);
366377

0 commit comments

Comments
 (0)