janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

How about S3-like storage? #97

Closed janboddez closed 11 months ago

janboddez commented 11 months ago

Depends on the specific plugin in use, probably. I mean, if the images aren't stored locally, we can't just upload them, I suppose.

janboddez commented 11 months ago

Affected sites (this isn't a bug per se, by the way) could filter attachment_url_to_postid. Or we could get the IDs from Gutenberg's Image blocks.

janboddez commented 11 months ago

So, to be able to fetch image IDs for in-post content, we'd have to filter attachment_url_to_postid.

But that's just one part of the puzzle. Given these IDs (or that of a featured image, or attached images, for which we already know the ID), we still need to re-convert them to a (local) file.

Assuming the local file (still) exists, it's a matter of hooking into overriding both wp_get_attachment_image_src() and wp_get_attachment_url(). Or, we could add a "shortcut" to Image_Handler::upload_image(), to set $file_path without calling these functions at all.

If the local file does not exist, I guess we could wp_remote_get() it (or similar), and still send it along.

Or we could just not support any of that (although merely adding the hooks doesn't hurt).

janboddez commented 11 months ago

So, to be able to fetch image IDs for in-post content, we'd have to filter attachment_url_to_postid.

Unless they're served through a reverse proxy handling the default WordPress (e.g., wp-content/uploads*) URLs. So this is the "less critical" part of the whole operation.

The other part is downloading the file data in order to then upload it.

We use file_get_contents( $file_path ) were $file_path is assumed to be a local path on the server. But it could be s3://bucket_name/<etc.> too, depending on what plugin was in use. And I'm not sure if that would work (doubt it).

If it's not a file PHP can read (we actually check is_file( $file_path )), nothing happens.

We could "prefilter" this path, download the file to a temp folder (that we prevent access to), set $file_path accordingly, then upload it after it is sent successfully.

janboddez commented 11 months ago

Currently using S3 Uploads, and the paths (i.e., the basedir returned by wp_upload_dir()) do indeed look like s3://bucket_name/<etc.>, and I can upload these images OK. I.e., PHP seems able to read them just fine using nothing but file_get_contents( $file_path ).