deliciousbrains / wp-amazon-s3-and-cloudfront

Automatically copies media uploads to Amazon S3 for delivery. Optionally configure Amazon CloudFront for even faster delivery.
https://wordpress.org/plugins/amazon-s3-and-cloudfront/
312 stars 150 forks source link

Bug: get_attached_file() returns URL not PATH #618

Closed doiftrue closed 1 year ago

doiftrue commented 1 year ago

Describe the bug After installing the Save SVG plugin page load increased to 30 sec.

This happens because of the get_attached_file() returns the URL but not the PATH. Save SVG tries to get the remote file content via HTTP request.

I found the solution to return the file PATH as is (even if the file does not exist locally). And now all work correctly.

What is the best solution in this case? Saving images locally is not an option for me.

To Reproduce Steps to reproduce the behavior:

  1. Install Save SVG plugin.
  2. Upload SVG image.
  3. Use that image anywhere on front using wp_get_attachment_image_src() function.
  4. See the bug.

Related plugin option

'remove-local-file' => true

ianmjones commented 1 year ago

If you're using Remove Local Files, then get_attached_file() has no choice but to return a stream wrapper URL to the object in the bucket. :shrug:

When that function is used, you're saying you want access to a file in order to do something with it. It sounds like Save SVG is either wanting to process it in some way, or should instead be using one of the built in WP core functions for getting an item's URL.

Please raise this as an issue with Save SVG.

Until you can get a fix from them, there is a filter you could use (you might be already) to return the $file instead of the $url, it's signature is as follows:

/**
 * This filter gives filter implementors a chance to copy back missing item files
 * from the provider before WordPress returns the file name/path for it. Defaults to
 * returning the remote URL.
 *
 * @param string             $url           Item URL
 * @param string             $file          Local file path
 * @param int                $attachment_id Attachment post id
 * @param Media_Library_Item $as3cf_item    The Item object
 */
return apply_filters( 'as3cf_get_attached_file', $url, $file, $attachment_id, $as3cf_item );

But of course, this will mean you're returning a file path that does not exist, which may cause you more issues.

doiftrue commented 1 year ago

Thanks a lot! Yes, I am using the as3cf_get_attached_file hook to force a PATH return. So far no problems with this solution.