humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.93k stars 388 forks source link

finfo::file(): Failed identify data 0:invalid mode 00 in wp-smushit/lib/class-wp-smush.php on line 393 #183

Open nicholak opened 7 years ago

nicholak commented 7 years ago

Hi. This plugin is causing an issue with Smush. What Smush expects here is the original image, however it is getting something weird:

[Thu Jul 27 00:30:10 2017] [error] [client 54.79.115.248] s3://blog-com/uploads/2016/05/COTA-150x133.jpg
[Thu Jul 27 00:30:10 2017] [error] [client 54.79.115.248] PHP Warning:  finfo::file(): Failed identify data 0:invalid mode 00 in /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-content-org/plugins/wp-smushit/lib/class-wp-smush.php on line 393

I have added the input that is causing the error to the log: s3://blog-com/uploads/2016/05/COTA-150x133.jpg

I suspect this is a recently added file via WP admin, but the path is not right.

Here is my config:

define( 'S3_UPLOADS_BUCKET', 'blog-com' );
define( 'S3_UPLOADS_BUCKET_URL', 'https://s3-ap-southeast-2.amazonaws.com/blog-com' );
define( 'S3_UPLOADS_KEY', 'private' );
define( 'S3_UPLOADS_SECRET', 'private' );
define( 'S3_UPLOADS_REGION', 'ap-southeast-2' );

Please advise what I can do other than abandon another failed S3 uploads plugin.

Thanks.

rmccue commented 7 years ago

S3 Uploads uses a PHP stream wrapper to refer to files on S3. These are used as paths, and are accepted by most PHP internal functions in place of filenames, but under the hood have custom behaviour. In S3's case, they map to S3 URLs under the hood. The "original image" is on S3, stored at a URL, and the s3://... is essentially a shorthand to refer to that.

By the sounds of it, the finfo extension used by Smush is having trouble working with the stat data (file metadata). Searching for the error gives a bunch of results which mention S3, so it seems like this problem is common to all code using the AWS SDK. (Or in our case, derived from it.)

There's two places where this data could be returned from S3 Uploads: the stream_stat method, and the url_stat method. Can you try logging the return values from both of those, and report back the values (and which method is being called)?

nicholak commented 7 years ago

I don't know if this helps. I'm adding this stack trace to record the issue:

Stack trace
E_WARNING: finfo::file(): Failed identify data 0:invalid mode 00
in finfo::file called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-content-org/plugins/wp-smushit/lib/class-wp-smush.php (392)
in WpSmush::resize_from_meta_data called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-content-org/plugins/wp-smushit/lib/class-wp-smush.php (638)
in WpSmush::smush_image called at ? (?)
in call_user_func_array called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-includes/plugin.php (235)
in apply_filters called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-includes/post.php (4838)
in wp_update_attachment_metadata called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-admin/includes/media.php (371)
in media_handle_upload called at /var/www/blog/release/52f70fadc0d0d4b6ffbae53bb314202cef403c59/wp-admin/async-upload.php (91)

from cron request:

Stack trace
E_WARNING: finfo::file(): Failed identify data 0:invalid mode 00
in finfo::file called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-content-org/plugins/wp-smushit/lib/class-wp-smush.php (393)
in WpSmush::resize_from_meta_data called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-content-org/plugins/wp-smushit/lib/class-wp-smush.php (639)
in WpSmush::smush_image called at ? (?)
in call_user_func_array called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-includes/plugin.php (235)
in apply_filters called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-includes/post.php (4838)
in wp_update_attachment_metadata called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-content-org/plugins/wordpress-23-related-posts-plugin/thumbnailer.php (142)
in wp_rp_update_attachment_id called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-content-org/plugins/wordpress-23-related-posts-plugin/thumbnailer.php (156)
in wp_rp_cron_do_extract_images_from_post called at ? (?)
in call_user_func_array called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-includes/plugin.php (601)
in do_action_ref_array called at /var/www/blog/release/506622593f0cf0694df3d59c87bca2cc5bec4e98/wp-cron.php (117)
nicholak commented 7 years ago

The code where failing is the last line. is_file() function is working for the same input, but finfo::file() fails. Investigating further...

                if ( class_exists( 'finfo' ) ) {
                    $finfo = new finfo( FILEINFO_MIME_TYPE );
                } else {
                    $finfo = false;
                }
...
                    if ( $finfo ) {
                        $ext = is_file( $attachment_file_path_size ) ? $finfo->file( $attachment_file_path_size ) : '';
...
rmccue commented 7 years ago

Yeah, the part it appears to be choking on is the return value from the stat command, so best place to look is where that's being generated (inside the stream wrapper).

nicholak commented 7 years ago

OK. I have some news. Using reflector class to id the class reveals it is the native class via PHP extension, and is version 1.0.5. Smush is not providing a finfo library.

I think we are trying to solve a problem that should not exist. I do not require Smush to reduce the files in S3. I require Smush to reduce the original image on upload to WP, and for this plugin to subsequently upload the resized images to S3.

Is it possible for this plugin to operate in this manner?

joehoyle commented 5 years ago

Unfortunately finfo / mime_content_type don't appear to support remote streams, see https://forums.aws.amazon.com/message.jspa?messageID=505342. I don't know if it will apply here, but a workaround is typically to copy the image to /tmp and then you can use these functions files.

tristanbes commented 5 years ago

I'm running into the same issue. What should be done to avoid it ?

trovster commented 4 years ago

I am having the same issue with remote streams and WordPress working out the extension. On line #2720 in wp-includes/functions.php the following is causing issues and the file will not upload.


if ( $type && ! $real_mime && extension_loaded( 'fileinfo' )) {
    $finfo     = finfo_open( FILEINFO_MIME_TYPE );
    $real_mime = finfo_file( $finfo, $file );
    finfo_close( $finfo );
}