boonebgorges / buddypress-docs

GNU General Public License v3.0
106 stars 44 forks source link

Amazon S3 Offload plugin #676

Open QurQuma opened 3 years ago

QurQuma commented 3 years ago

I’d like to use docs joined with Amazon S3 Offload, a plugin that automatically move all of the uploaded media to an S3 bucket. Actually Buddypress docs lose his link from the uploaded media. Is there a way to fix this? This would be very useful to not overload wp platform resources. Thanks, Francesco

boonebgorges commented 3 years ago

Actually Buddypress docs lose his link from the uploaded media.

Could you say more about this? Exactly what is happening?

  1. Are you able to upload the file using the Docs front-end interface? If so, is it properly sent to Amazon?
  2. When you view a list of Docs attachments, what is the format of the URL for the attachments?
  3. What happens when you click the attachment download link?
QurQuma commented 3 years ago

Hi,

  1. Uploading from frontend is ok and then plugin uploads correctly the file to amazon bucket and delete it from wp server;
  2. This is how doc attachment looks like Schermata 2020-10-13 alle 19 51 14 and this is the link https://XXX.xxx/docs/doc-name/?bp-attachment=120809262_1696870730475433_6852389121015225895_o.jpg
  3. File not found
boonebgorges commented 3 years ago

Thanks very much. One more question: What should the URL look like?

QurQuma commented 3 years ago

You're so welcome! It has to take URL from this: Schermata 2020-10-13 alle 19 56 01 this is the link to amazon file over the bucket. Actually it looks like this: https://AMAZON_S3_BUCKET_NAME.s3.eu-west-3.amazonaws.com/website/wp-content/uploads/2020/10/13174853/120809262_1696870730475433_6852389121015225895_o.jpg Is this the information you need? Thanks

boonebgorges commented 3 years ago

Yes, that's what I need - I need to understand how the Offload plugin filters URLs.

Can you confirm that this is the plugin you're using? https://wordpress.org/plugins/amazon-s3-and-cloudfront/

If so, I'll take a look to see if there's a simple way to apply the plugin's filters to the Docs attachment links.

QurQuma commented 3 years ago

Yes, exactly. Thanks!

QurQuma commented 3 years ago

Updates about this? Thanks @boonebgorges

boonebgorges commented 3 years ago

I had a look and it looks like this is a minimal solution. In a plugin or a theme file:

add_filter(
    'bp_docs_attachment_url_base',
    function( $att_url, $attachment ) {
        $att_url = wp_get_attachment_url( $attachment->ID );
        return apply_filters( 'as3cf_filter_post_local_to_provider', $att_url );
    },
    10,
    2
);

Note that this doesn't do any attachment protection, so that the docs are downloadable by anyone who has the URL.

If this works for you, I can examine how/whether to build support for this feature into buddypress-docs itself.

QurQuma commented 3 years ago

It work perfectly for me, @boonebgorges. I'm here to support for every further step to improve protection (how does it work with actual implementation?).

boonebgorges commented 3 years ago

The Doc protection feature has two parts.

  1. Direct download URLs are masked. For a doc with URL like example.com/docs/test-doc, attachment download links appear as example.com/docs/test-doc?bp-attachment=image.jpg. WP then catches these ?bp-attachment requests and serves the file using readfile(). This prevents people from seeing the actual URL filepath (in wp-content).

  2. Second, direct access to files is blocked, when using Apache. An .htaccess file is automatically placed in the wp-content directory that contains the attachments for a non-public Doc, and that .htaccess file redirects requests for files inside of that directory to the corresponding ?bp-attachment URL.

It's not clear to me how 2 would be possible with files offloaded to S3.

We might be able to accomplish some version of 1, but it would require PHP-level redirects to the S3 URL.

QurQuma commented 3 years ago

Ok, it would be great to implement 1. As for 2. isn't yet possible to allow access to amazon s3 buckets only from a specific website? It would be possible to solve this way, but I'm not sure about this.