humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.95k stars 391 forks source link

S3_UPLOADS_USE_LOCAL not replacing path or basedir #647

Open knlb opened 1 year ago

knlb commented 1 year ago

Is there any reason why filter_upload_dir in class-plugin.php still returns the s3 path when S3_UPLOADS_USE_LOCAL is defined? url and baseurl are replaced with the local directory correctly but path and basedir or not. It's causing some compatibility problems with another plugin, but maybe they're using the wrong field. I'm not quite clear on when to use path/basedir vs url/baseurl, but it seems that they should both be pointed to the local directory when USE_LOCAL is defined as true.

knlb commented 1 year ago

Seem that the solution is to set the path and basedir back to the original values-

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_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_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path']);
                $dirs['baseurl'] = str_replace($s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir']);
                $dirs['path'] = $this->original_upload_dir['path'];
                $dirs['basedir'] = $this->original_upload_dir['basedir'];
            } else {
                $dirs['url'] = str_replace($s3_path, $this->get_s3_url(), $dirs['path']);
                $dirs['baseurl'] = str_replace($s3_path, $this->get_s3_url(), $dirs['basedir']);
            }
        }

        return $dirs;
    }