Files
WPS3Media/vendor/Gcp/google/cloud-storage/src/ReadStream.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

87 lines
2.8 KiB
PHP

<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace DeliciousBrains\WP_Offload_Media\Gcp\Google\Cloud\Storage;
use DeliciousBrains\WP_Offload_Media\Gcp\GuzzleHttp\Psr7\StreamDecoratorTrait;
use DeliciousBrains\WP_Offload_Media\Gcp\Psr\Http\Message\StreamInterface;
/**
* A Stream implementation that wraps a GuzzleHttp download stream to
* provide `getSize()` from the response headers.
*/
class ReadStream implements StreamInterface
{
use StreamDecoratorTrait;
private $stream;
/**
* Create a new ReadStream.
*
* @param StreamInterface $stream The stream interface to wrap
*/
public function __construct(StreamInterface $stream)
{
$this->stream = $stream;
}
/**
* Return the full size of the buffer. If the underlying stream does
* not report it's size, try to fetch the size from the Content-Length
* response header.
*
* @return int The size of the stream.
*/
public function getSize() : ?int
{
return $this->stream->getSize() ?: $this->getSizeFromMetadata();
}
/**
* Attempt to fetch the size from the Content-Length response header.
* If we cannot, return 0.
*
* @return int The Size of the stream
*/
private function getSizeFromMetadata() : int
{
foreach ($this->stream->getMetadata('wrapper_data') as $value) {
if (\substr($value, 0, 15) == "Content-Length:") {
return (int) \substr($value, 16);
}
}
return 0;
}
/**
* Read bytes from the underlying buffer, retrying until we have read
* enough bytes or we cannot read any more. We do this because the
* internal C code for filling a buffer does not account for when
* we try to read large chunks from a user-land stream that does not
* return enough bytes.
*
* @param int $length The number of bytes to read.
* @return string Read bytes from the underlying stream.
*/
public function read($length) : string
{
$data = '';
do {
$moreData = $this->stream->read($length);
$data .= $moreData;
$readLength = \strlen($moreData);
$length -= $readLength;
} while ($length > 0 && $readLength > 0);
return $data;
}
}