Files
WPS3Media/vendor/Aws3/Aws/S3/MultipartUploadingTrait.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

100 lines
4.0 KiB
PHP

<?php
namespace DeliciousBrains\WP_Offload_Media\Aws3\Aws\S3;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\CommandInterface;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\Multipart\UploadState;
use DeliciousBrains\WP_Offload_Media\Aws3\Aws\ResultInterface;
trait MultipartUploadingTrait
{
/**
* Creates an UploadState object for a multipart upload by querying the
* service for the specified upload's information.
*
* @param S3ClientInterface $client S3Client used for the upload.
* @param string $bucket Bucket for the multipart upload.
* @param string $key Object key for the multipart upload.
* @param string $uploadId Upload ID for the multipart upload.
*
* @return UploadState
*/
public static function getStateFromService(S3ClientInterface $client, $bucket, $key, $uploadId)
{
$state = new UploadState(['Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId]);
foreach ($client->getPaginator('ListParts', $state->getId()) as $result) {
// Get the part size from the first part in the first result.
if (!$state->getPartSize()) {
$state->setPartSize($result->search('Parts[0].Size'));
}
// Mark all the parts returned by ListParts as uploaded.
foreach ($result['Parts'] as $part) {
$state->markPartAsUploaded($part['PartNumber'], ['PartNumber' => $part['PartNumber'], 'ETag' => $part['ETag']]);
}
}
$state->setStatus(UploadState::INITIATED);
return $state;
}
protected function handleResult(CommandInterface $command, ResultInterface $result)
{
$partData = [];
$partData['PartNumber'] = $command['PartNumber'];
$partData['ETag'] = $this->extractETag($result);
if (isset($command['ChecksumAlgorithm'])) {
$checksumMemberName = 'Checksum' . \strtoupper($command['ChecksumAlgorithm']);
$partData[$checksumMemberName] = $result[$checksumMemberName];
}
$this->getState()->markPartAsUploaded($command['PartNumber'], $partData);
}
protected abstract function extractETag(ResultInterface $result);
protected function getCompleteParams()
{
$config = $this->getConfig();
$params = isset($config['params']) ? $config['params'] : [];
$params['MultipartUpload'] = ['Parts' => $this->getState()->getUploadedParts()];
return $params;
}
protected function determinePartSize()
{
// Make sure the part size is set.
$partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
// Adjust the part size to be larger for known, x-large uploads.
if ($sourceSize = $this->getSourceSize()) {
$partSize = (int) \max($partSize, \ceil($sourceSize / MultipartUploader::PART_MAX_NUM));
}
// Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
throw new \InvalidArgumentException('The part size must be no less ' . 'than 5 MB and no greater than 5 GB.');
}
return $partSize;
}
protected function getInitiateParams()
{
$config = $this->getConfig();
$params = isset($config['params']) ? $config['params'] : [];
if (isset($config['acl'])) {
$params['ACL'] = $config['acl'];
}
// Set the ContentType if not already present
if (empty($params['ContentType']) && ($type = $this->getSourceMimeType())) {
$params['ContentType'] = $type;
}
return $params;
}
/**
* @return UploadState
*/
protected abstract function getState();
/**
* @return array
*/
protected abstract function getConfig();
/**
* @return int
*/
protected abstract function getSourceSize();
/**
* @return string|null
*/
protected abstract function getSourceMimeType();
}