You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
6.8 KiB
PHP
216 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Shared transport seam for one immutable object-storage artifact.
|
|
*
|
|
* Persistence and business state remain owned by the catalog modules. This
|
|
* module owns side-effect classification and exact-version capabilities so
|
|
* every catalog follows the same unknown-effect rules.
|
|
*/
|
|
final class CloudImmutableArtifactTransport
|
|
{
|
|
public function __construct(
|
|
private readonly ?CloudObjectStorageS3Client $client = null
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param array{config:array<string,mixed>,credentials:array<string,string>,
|
|
* revision:int} $runtime
|
|
* @param array{path:string,size_bytes:int,sha256:string} $file
|
|
* @return array{resolution:string,identity:?array,error_code:string,
|
|
* message:string}
|
|
*/
|
|
public function publish(
|
|
array $runtime,
|
|
string $objectKey,
|
|
array $file
|
|
): array {
|
|
self::assertRuntime($runtime);
|
|
self::assertFile($file);
|
|
$client = $this->client ?? new CloudObjectStorageS3Client();
|
|
try {
|
|
$identity = $client->putImmutableFile(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey,
|
|
$file['path'],
|
|
$file['size_bytes'],
|
|
$file['sha256']
|
|
);
|
|
return self::result('applied', $identity);
|
|
} catch (CloudObjectStorageTransferError $error) {
|
|
if (!$error->effectMayHaveApplied) {
|
|
return self::result(
|
|
'not_applied', null, $error->publicCode,
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
$failureCode = $error->publicCode;
|
|
} catch (\Throwable $error) {
|
|
$failureCode = 'artifact_publish_failed';
|
|
}
|
|
|
|
try {
|
|
$identity = $client->inspectObject(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey
|
|
);
|
|
} catch (\Throwable $error) {
|
|
return self::result(
|
|
'unknown', null, 'artifact_publish_reconcile_unavailable',
|
|
'无法确认不可变对象是否已经保存'
|
|
);
|
|
}
|
|
if ($identity === null) {
|
|
return self::result(
|
|
'not_applied', null, $failureCode,
|
|
'对象存储已确认不可变对象未保存'
|
|
);
|
|
}
|
|
if ((int) ($identity['size_bytes'] ?? 0) !== $file['size_bytes']) {
|
|
return self::result(
|
|
'conflict', $identity, 'artifact_identity_mismatch',
|
|
'不可变对象大小与本地文件不一致'
|
|
);
|
|
}
|
|
return self::result('applied', $identity);
|
|
}
|
|
|
|
/**
|
|
* @param array{config:array<string,mixed>,credentials:array<string,string>,
|
|
* revision:int} $runtime
|
|
*/
|
|
public function inspect(
|
|
array $runtime,
|
|
string $objectKey,
|
|
string $versionId = ''
|
|
): ?array {
|
|
self::assertRuntime($runtime);
|
|
return ($this->client ?? new CloudObjectStorageS3Client())
|
|
->inspectObject(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey,
|
|
$versionId
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array{config:array<string,mixed>,credentials:array<string,string>,
|
|
* revision:int} $runtime
|
|
* @return array{size_bytes:int,etag:string,version_id:string}
|
|
*/
|
|
public function head(
|
|
array $runtime,
|
|
string $objectKey,
|
|
string $versionId = ''
|
|
): array {
|
|
self::assertRuntime($runtime);
|
|
return ($this->client ?? new CloudObjectStorageS3Client())->headObject(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey,
|
|
$versionId
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array{config:array<string,mixed>,credentials:array<string,string>,
|
|
* revision:int} $runtime
|
|
*/
|
|
public function presignedDownload(
|
|
array $runtime,
|
|
string $objectKey,
|
|
string $versionId,
|
|
int $expiresSeconds
|
|
): string {
|
|
self::assertRuntime($runtime);
|
|
return ($this->client ?? new CloudObjectStorageS3Client())
|
|
->presignedVersionedGet(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey,
|
|
$versionId,
|
|
$expiresSeconds
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array{config:array<string,mixed>,credentials:array<string,string>,
|
|
* revision:int} $runtime
|
|
*/
|
|
public function deleteExact(
|
|
array $runtime,
|
|
string $objectKey,
|
|
string $versionId
|
|
): void {
|
|
self::assertRuntime($runtime);
|
|
($this->client ?? new CloudObjectStorageS3Client())->deleteObject(
|
|
$runtime['config'],
|
|
$runtime['credentials'],
|
|
$objectKey,
|
|
$versionId
|
|
);
|
|
}
|
|
|
|
/** @param array<string,mixed> $identity */
|
|
public static function identityMatches(
|
|
array $identity,
|
|
int $sizeBytes,
|
|
string $etag,
|
|
string $versionId
|
|
): bool {
|
|
return (int) ($identity['size_bytes'] ?? -1) === $sizeBytes
|
|
&& is_string($identity['etag'] ?? null)
|
|
&& hash_equals($etag, (string) $identity['etag'])
|
|
&& is_string($identity['version_id'] ?? null)
|
|
&& hash_equals($versionId, (string) $identity['version_id']);
|
|
}
|
|
|
|
/** @return array{resolution:string,identity:?array,error_code:string,message:string} */
|
|
private static function result(
|
|
string $resolution,
|
|
?array $identity = null,
|
|
string $errorCode = '',
|
|
string $message = ''
|
|
): array {
|
|
return [
|
|
'resolution' => $resolution,
|
|
'identity' => $identity,
|
|
'error_code' => $errorCode,
|
|
'message' => $message,
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $runtime */
|
|
private static function assertRuntime(array $runtime): void
|
|
{
|
|
if (!is_array($runtime['config'] ?? null)
|
|
|| !is_array($runtime['credentials'] ?? null)
|
|
|| !is_int($runtime['revision'] ?? null)
|
|
|| (int) $runtime['revision'] < 1) {
|
|
throw new \InvalidArgumentException(
|
|
'Immutable artifact storage runtime is invalid'
|
|
);
|
|
}
|
|
}
|
|
|
|
/** @param array<string,mixed> $file */
|
|
private static function assertFile(array $file): void
|
|
{
|
|
if (!is_string($file['path'] ?? null)
|
|
|| !is_int($file['size_bytes'] ?? null)
|
|
|| (int) $file['size_bytes'] < 1
|
|
|| !is_string($file['sha256'] ?? null)
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $file['sha256']) !== 1) {
|
|
throw new \InvalidArgumentException(
|
|
'Immutable artifact file identity is invalid'
|
|
);
|
|
}
|
|
}
|
|
}
|