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
This commit is contained in:
2026-03-03 12:30:18 +01:00
commit 3248cbb029
2086 changed files with 359427 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\Parser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\CommandInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\ResultInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Psr\Http\Message\ResponseInterface;
/**
* A custom mutator for a GetBucketLocation request, which
* extract the bucket location value and injects it into the
* result as the `LocationConstraint` field.
*
* @internal
*/
final class GetBucketLocationResultMutator implements S3ResultMutator
{
/**
* @inheritDoc
*/
public function __invoke(ResultInterface $result, CommandInterface $command, ResponseInterface $response) : ResultInterface
{
if ($command->getName() !== 'GetBucketLocation') {
return $result;
}
static $location = 'us-east-1';
static $pattern = '/>(.+?)<\\/LocationConstraint>/';
if (\preg_match($pattern, $response->getBody(), $matches)) {
$location = $matches[1] === 'EU' ? 'eu-west-1' : $matches[1];
}
$result['LocationConstraint'] = $location;
$response->getBody()->rewind();
return $result;
}
}

200
vendor/Aws3/Aws/S3/Parser/S3Parser.php vendored Normal file
View File

@@ -0,0 +1,200 @@
<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\Parser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\ErrorParser\XmlErrorParser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\Parser\AbstractParser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\Parser\Exception\ParserException;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\Service;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\StructureShape;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\CommandInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Exception\AwsException;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\ResultInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\GuzzleHttp\Psr7\Utils;
use DeliciousBrains\WP_Offload_Media\Aws3\Psr\Http\Message\ResponseInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Psr\Http\Message\StreamInterface;
/**
* Custom S3 parser on top of the S3 protocol parser
* for handling specific S3 parsing scenarios.
*
* @internal
*/
final class S3Parser extends AbstractParser
{
/** @var AbstractParser */
private $protocolParser;
/** @var XmlErrorParser */
private $errorParser;
/** @var string */
private $exceptionClass;
/** @var array */
private $s3ResultMutators;
/**
* @param AbstractParser $protocolParser
* @param XmlErrorParser $errorParser
* @param Service $api
* @param string $exceptionClass
*/
public function __construct(AbstractParser $protocolParser, XmlErrorParser $errorParser, Service $api, string $exceptionClass = AwsException::class)
{
parent::__construct($api);
$this->protocolParser = $protocolParser;
$this->errorParser = $errorParser;
$this->exceptionClass = $exceptionClass;
$this->s3ResultMutators = [];
}
/**
* Parses a S3 response.
*
* @param CommandInterface $command The command that originated the request.
* @param ResponseInterface $response The response received from the service.
*
* @return ResultInterface|null
*/
public function __invoke(CommandInterface $command, ResponseInterface $response) : ?ResultInterface
{
// Check first if the response is an error
$this->parse200Error($command, $response);
try {
$parseFn = $this->protocolParser;
$result = $parseFn($command, $response);
} catch (ParserException $e) {
// Parsing errors will be considered retryable.
throw new $this->exceptionClass("Error parsing response for {$command->getName()}:" . " AWS parsing error: {$e->getMessage()}", $command, ['connection_error' => \true, 'exception' => $e], $e);
}
return $this->executeS3ResultMutators($result, $command, $response);
}
/**
* Tries to parse a 200 response as an error from S3.
* If the parsed result contains a code and message then that means an error
* was found, and hence an exception is thrown with that error.
*
* @param CommandInterface $command
* @param ResponseInterface $response
*
* @return void
*/
private function parse200Error(CommandInterface $command, ResponseInterface $response) : void
{
// This error parsing should be just for 200 error responses
// and operations where its output shape does not have a streaming
// member.
if (200 !== $response->getStatusCode() || !$this->shouldBeConsidered200Error($command->getName())) {
return;
}
// To guarantee we try the error parsing just for an Error xml response.
if (!$this->isFirstRootElementError($response->getBody())) {
return;
}
try {
$errorParserFn = $this->errorParser;
$parsedError = $errorParserFn($response, $command);
} catch (ParserException $e) {
// Parsing errors will be considered retryable.
$parsedError = ['code' => 'ConnectionError', 'message' => "An error connecting to the service occurred" . " while performing the " . $command->getName() . " operation."];
}
if (isset($parsedError['code']) && isset($parsedError['message'])) {
throw new $this->exceptionClass($parsedError['message'], $command, ['connection_error' => \true, 'code' => $parsedError['code'], 'message' => $parsedError['message']]);
}
}
/**
* Checks if a specific operation should be considered
* a s3 200 error. Operations where any of its output members
* has a streaming or httpPayload trait should be not considered.
*
* @param $commandName
*
* @return bool
*/
private function shouldBeConsidered200Error($commandName) : bool
{
$operation = $this->api->getOperation($commandName);
$output = $operation->getOutput();
foreach ($output->getMembers() as $_ => $memberProps) {
if (!empty($memberProps['eventstream']) || !empty($memberProps['streaming'])) {
return \false;
}
}
return \true;
}
/**
* Checks if the root element of the response body is "Error", which is
* when we should try to parse an error from a 200 response from s3.
* It is recommended to make sure the stream given is seekable, otherwise
* the rewind call will cause a user warning.
*
* @param StreamInterface $responseBody
*
* @return bool
*/
private function isFirstRootElementError(StreamInterface $responseBody) : bool
{
static $pattern = '/<\\?xml version="1\\.0" encoding="UTF-8"\\?>\\s*<Error>/';
// To avoid performance overhead in large streams
$reducedBodyContent = $responseBody->read(64);
$foundErrorElement = \preg_match($pattern, $reducedBodyContent);
// A rewind is needed because the stream is partially or entirely consumed
// in the previous read operation.
$responseBody->rewind();
return $foundErrorElement;
}
/**
* Execute mutator implementations over a result.
* Mutators are logics that modifies a result.
*
* @param ResultInterface $result
* @param CommandInterface $command
* @param ResponseInterface $response
*
* @return ResultInterface
*/
private function executeS3ResultMutators(ResultInterface $result, CommandInterface $command, ResponseInterface $response) : ResultInterface
{
foreach ($this->s3ResultMutators as $mutator) {
$result = $mutator($result, $command, $response);
}
return $result;
}
/**
* Adds a mutator into the list of mutators.
*
* @param string $mutatorName
* @param S3ResultMutator $s3ResultMutator
* @return void
*/
public function addS3ResultMutator(string $mutatorName, S3ResultMutator $s3ResultMutator) : void
{
if (isset($this->s3ResultMutators[$mutatorName])) {
\trigger_error("The S3 Result Mutator {$mutatorName} already exists!", \E_USER_WARNING);
return;
}
$this->s3ResultMutators[$mutatorName] = $s3ResultMutator;
}
/**
* Removes a mutator from the mutator list.
*
* @param string $mutatorName
* @return void
*/
public function removeS3ResultMutator(string $mutatorName) : void
{
if (!isset($this->s3ResultMutators[$mutatorName])) {
\trigger_error("The S3 Result Mutator {$mutatorName} does not exist!", \E_USER_WARNING);
return;
}
unset($this->s3ResultMutators[$mutatorName]);
}
/**
* Returns the list of result mutators available.
*
* @return array
*/
public function getS3ResultMutators() : array
{
return $this->s3ResultMutators;
}
public function parseMemberFromStream(StreamInterface $stream, StructureShape $member, $response)
{
return $this->protocolParser->parseMemberFromStream($stream, $member, $response);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\Parser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\CommandInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\ResultInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Psr\Http\Message\ResponseInterface;
/**
* Interface for S3 result mutator implementations.
* A S3 result mutator is meant for modifying a request
* result before returning it to the user.
* One example is if a custom field is needed to be injected
* into the result or if an existent field needs to be modified.
* Since the command and the response itself are parameters when
* invoking the mutators then, this facilitates to make better
* decisions that may involve validations using the command parameters
* or response fields, etc.
*
* @internal
*/
interface S3ResultMutator
{
/**
* @param ResultInterface $result the result object to be modified.
* @param CommandInterface $command the command that originated the request.
* @param ResponseInterface $response the response resulting from the request.
*
* @return ResultInterface
*/
public function __invoke(ResultInterface $result, CommandInterface $command, ResponseInterface $response) : ResultInterface;
}

View File

@@ -0,0 +1,112 @@
<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\Parser;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Api\Service;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\CommandInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\ResultInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\CalculatesChecksumTrait;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3\Exception\S3Exception;
use DeliciousBrains\WP_Offload_Media\Aws3\Psr\Http\Message\ResponseInterface;
/**
* A custom s3 result mutator that validates the response checksums.
*
* @internal
*/
final class ValidateResponseChecksumResultMutator implements S3ResultMutator
{
use CalculatesChecksumTrait;
/** @var Service $api */
private $api;
/**
* @param Service $api
*/
public function __construct(Service $api)
{
$this->api = $api;
}
/**
* @param ResultInterface $result
* @param CommandInterface|null $command
* @param ResponseInterface|null $response
*
* @return ResultInterface
*/
public function __invoke(ResultInterface $result, CommandInterface $command = null, ResponseInterface $response = null) : ResultInterface
{
$operation = $this->api->getOperation($command->getName());
// Skip this middleware if the operation doesn't have an httpChecksum
$checksumInfo = empty($operation['httpChecksum']) ? null : $operation['httpChecksum'];
if (null === $checksumInfo) {
return $result;
}
// Skip this middleware if the operation doesn't send back a checksum,
// or the user doesn't opt in
$checksumModeEnabledMember = $checksumInfo['requestValidationModeMember'] ?? "";
$checksumModeEnabled = $command[$checksumModeEnabledMember] ?? "";
$responseAlgorithms = $checksumInfo['responseAlgorithms'] ?? [];
if (empty($responseAlgorithms) || \strtolower($checksumModeEnabled) !== "enabled") {
return $result;
}
if (\extension_loaded('awscrt')) {
$checksumPriority = ['CRC32C', 'CRC32', 'SHA1', 'SHA256'];
} else {
$checksumPriority = ['CRC32', 'SHA1', 'SHA256'];
}
$checksumsToCheck = \array_intersect($responseAlgorithms, $checksumPriority);
$checksumValidationInfo = $this->validateChecksum($checksumsToCheck, $response);
if ($checksumValidationInfo['status'] == "SUCCEEDED") {
$result['ChecksumValidated'] = $checksumValidationInfo['checksum'];
} elseif ($checksumValidationInfo['status'] == "FAILED") {
// Ignore failed validations on GetObject if it's a multipart get
// which returned a full multipart object
if ($command->getName() === "GetObject" && !empty($checksumValidationInfo['checksumHeaderValue'])) {
$headerValue = $checksumValidationInfo['checksumHeaderValue'];
$lastDashPos = \strrpos($headerValue, '-');
$endOfChecksum = \substr($headerValue, $lastDashPos + 1);
if (\is_numeric($endOfChecksum) && \intval($endOfChecksum) > 1 && \intval($endOfChecksum) < 10000) {
return $result;
}
}
throw new S3Exception("Calculated response checksum did not match the expected value", $command);
}
return $result;
}
/**
* @param $checksumPriority
* @param ResponseInterface $response
*
* @return array
*/
private function validateChecksum($checksumPriority, ResponseInterface $response) : array
{
$checksumToValidate = $this->chooseChecksumHeaderToValidate($checksumPriority, $response);
$validationStatus = "SKIPPED";
$checksumHeaderValue = null;
if (!empty($checksumToValidate)) {
$checksumHeaderValue = $response->getHeader('x-amz-checksum-' . $checksumToValidate);
if (isset($checksumHeaderValue)) {
$checksumHeaderValue = $checksumHeaderValue[0];
$calculatedChecksumValue = $this->getEncodedValue($checksumToValidate, $response->getBody());
$validationStatus = $checksumHeaderValue == $calculatedChecksumValue ? "SUCCEEDED" : "FAILED";
}
}
return ["status" => $validationStatus, "checksum" => $checksumToValidate, "checksumHeaderValue" => $checksumHeaderValue];
}
/**
* @param $checksumPriority
* @param ResponseInterface $response
*
* @return string
*/
private function chooseChecksumHeaderToValidate($checksumPriority, ResponseInterface $response) : ?string
{
foreach ($checksumPriority as $checksum) {
$checksumHeader = 'x-amz-checksum-' . $checksum;
if ($response->hasHeader($checksumHeader)) {
return $checksum;
}
}
return null;
}
}