Files
WPS3Media/vendor/Aws3/Aws/Endpoint/PartitionEndpointProvider.php
Malin 3248cbb029 feat: add S3-compatible storage provider (MinIO, Ceph, R2, etc.)
Adds a new 'S3-Compatible Storage' provider that works with any
S3-API-compatible object storage service, including MinIO, Ceph,
Cloudflare R2, Backblaze B2, and others.

Changes:
- New provider class: classes/providers/storage/s3-compatible-provider.php
  - Provider key: s3compatible
  - Reads user-configured endpoint URL from settings
  - Uses path-style URL access (required by most S3-compatible services)
  - Supports credentials via AS3CF_S3COMPAT_ACCESS_KEY_ID /
    AS3CF_S3COMPAT_SECRET_ACCESS_KEY wp-config.php constants
  - Disables AWS-specific features (Block Public Access, Object Ownership)
- New provider SVG icons (s3compatible.svg, -link.svg, -round.svg)
- Registered provider in main plugin class with endpoint setting support
- Updated StorageProviderSubPage to show endpoint URL input for S3-compatible
- Built pro settings bundle with rollup (Svelte 4.2.19)
- Added package.json and updated rollup.config.mjs for pro-only builds
2026-03-03 12:30:18 +01:00

114 lines
3.9 KiB
PHP

<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\Endpoint;
use DeliciousBrains\WP_Offload_Media\Aws3\JmesPath\Env;
class PartitionEndpointProvider
{
/** @var Partition[] */
private $partitions;
/** @var string */
private $defaultPartition;
/** @var array */
private $options;
/**
* The 'options' parameter accepts the following arguments:
*
* - sts_regional_endpoints: For STS legacy regions, set to 'regional' to
* use regional endpoints, 'legacy' to use the legacy global endpoint.
* Defaults to 'legacy'.
* - s3_us_east_1_regional_endpoint: For S3 us-east-1 region, set to 'regional'
* to use the regional endpoint, 'legacy' to use the legacy global endpoint.
* Defaults to 'legacy'.
*
* @param array $partitions
* @param string $defaultPartition
* @param array $options
*/
public function __construct(array $partitions, $defaultPartition = 'aws', $options = [])
{
$this->partitions = \array_map(function (array $definition) {
return new Partition($definition);
}, \array_values($partitions));
$this->defaultPartition = $defaultPartition;
$this->options = $options;
}
public function __invoke(array $args = [])
{
$partition = $this->getPartition(isset($args['region']) ? $args['region'] : '', isset($args['service']) ? $args['service'] : '');
$args['options'] = $this->options;
return $partition($args);
}
/**
* Returns the partition containing the provided region or the default
* partition if no match is found.
*
* @param string $region
* @param string $service
*
* @return Partition
*/
public function getPartition($region, $service)
{
foreach ($this->partitions as $partition) {
if ($partition->isRegionMatch($region, $service)) {
return $partition;
}
}
return $this->getPartitionByName($this->defaultPartition);
}
/**
* Returns the partition with the provided name or null if no partition with
* the provided name can be found.
*
* @param string $name
*
* @return Partition|null
*/
public function getPartitionByName($name)
{
foreach ($this->partitions as $partition) {
if ($name === $partition->getName()) {
return $partition;
}
}
}
/**
* Creates and returns the default SDK partition provider.
*
* @param array $options
* @return PartitionEndpointProvider
*/
public static function defaultProvider($options = [])
{
$data = \DeliciousBrains\WP_Offload_Media\Aws3\Aws\load_compiled_json(__DIR__ . '/../data/endpoints.json');
$prefixData = \DeliciousBrains\WP_Offload_Media\Aws3\Aws\load_compiled_json(__DIR__ . '/../data/endpoints_prefix_history.json');
$mergedData = self::mergePrefixData($data, $prefixData);
return new self($mergedData['partitions'], 'aws', $options);
}
/**
* Copy endpoint data for other prefixes used by a given service
*
* @param $data
* @param $prefixData
* @return array
*/
public static function mergePrefixData($data, $prefixData)
{
$prefixGroups = $prefixData['prefix-groups'];
foreach ($data["partitions"] as $index => $partition) {
foreach ($prefixGroups as $current => $old) {
$serviceData = Env::search("services.\"{$current}\"", $partition);
if (!empty($serviceData)) {
foreach ($old as $prefix) {
if (empty(Env::search("services.\"{$prefix}\"", $partition))) {
$data["partitions"][$index]["services"][$prefix] = $serviceData;
}
}
}
}
}
return $data;
}
}