Skip to content

Commit

Permalink
Merge pull request #632 from humanmade/backport-564-to-v3-branch
Browse files Browse the repository at this point in the history
[Backport v3-branch] Added `get_s3_path` function instead of hard coding s3 path.
  • Loading branch information
joehoyle committed May 4, 2023
2 parents 1f3a7a2 + fc88fbd commit b65f2c7
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public function register_stream_wrapper() {
stream_context_set_option( stream_context_get_default(), 's3', 'seekable', true );
}

/**
* Get the s3:// path for the bucket.
*/
public function get_s3_path() {
return 's3://' . $this->bucket;
}

/**
* Overwrite the default wp_upload_dir.
*
Expand All @@ -163,19 +170,20 @@ public function register_stream_wrapper() {
public function filter_upload_dir( array $dirs ) : array {

$this->original_upload_dir = $dirs;
$s3_path = $this->get_s3_path();

$dirs['path'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['path'] );
$dirs['basedir'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['basedir'] );
$dirs['path'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['path'] );
$dirs['basedir'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['basedir'] );

if ( ! defined( 'S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL' ) || ! S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL ) {

if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) {
$dirs['url'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] );
$dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] );
$dirs['url'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] );
$dirs['baseurl'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] );

} else {
$dirs['url'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['path'] );
$dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['basedir'] );
$dirs['url'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['path'] );
$dirs['baseurl'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['basedir'] );
}
}

Expand Down

0 comments on commit b65f2c7

Please sign in to comment.