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.
132 lines
5.3 KiB
PHP
132 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateArtifact;
|
|
use App\Models\CloudTemplateImportIntent;
|
|
|
|
/** Template-facing facade over the shared replicated storage ledger. */
|
|
final class CloudTemplateArtifactManager
|
|
{
|
|
private const DOWNLOAD_SECONDS = 604800;
|
|
|
|
/**
|
|
* Bind a protected storage artifact while the template intent is locked.
|
|
* Publication and business finalization share the caller transaction.
|
|
*/
|
|
public static function adoptImportIntentLocked(
|
|
CloudTemplate $template,
|
|
CloudTemplateImportIntent $intent
|
|
): CloudTemplateArtifact {
|
|
if (!$template->exists || !in_array((string) $template->state,
|
|
['preparing', 'importing'], true)
|
|
|| (string) $template->id !== (string) $intent->template_id
|
|
|| (string) $intent->state !== 'source_ready'
|
|
|| (int) $intent->source_size_bytes
|
|
!== (int) $template->image_bytes
|
|
|| !hash_equals(
|
|
(string) $intent->source_sha256,
|
|
(string) $template->sha256
|
|
)) {
|
|
throw new CloudError('模板导入对象身份无效', 409);
|
|
}
|
|
$binding = CloudTemplateStoragePublicationService::binding($intent);
|
|
if (($binding['protected'] ?? false) !== true) {
|
|
throw new CloudError('模板双副本保护尚未完成', 409);
|
|
}
|
|
$identity = CloudTemplateStoragePublicationService::primaryIdentity(
|
|
$intent
|
|
);
|
|
$existing = CloudTemplateArtifact::query()
|
|
->where('template_id', (string) $template->id)
|
|
->lockForUpdate()->first();
|
|
if ($existing instanceof CloudTemplateArtifact) {
|
|
if ((int) ($existing->storage_artifact_id ?? 0)
|
|
!== (int) $binding['artifact_id']
|
|
|| (int) $existing->size_bytes
|
|
!== (int) $intent->source_size_bytes
|
|
|| !hash_equals(
|
|
(string) $existing->sha256,
|
|
(string) $intent->source_sha256
|
|
)
|
|
|| (string) $existing->state !== 'ready') {
|
|
throw new CloudError('模板主副本记录与存储账本冲突', 409);
|
|
}
|
|
return $existing;
|
|
}
|
|
if ((string) $template->state !== 'preparing') {
|
|
throw new CloudError('模板主副本记录与导入任务不一致', 409);
|
|
}
|
|
|
|
CloudStoragePublicationFinalizerService::complete(
|
|
'template',
|
|
(string) $intent->id,
|
|
(int) $binding['artifact_id'],
|
|
(string) $binding['operation_id']
|
|
);
|
|
|
|
/** @var \App\Models\CloudStorageReplica $primary */
|
|
$primary = $binding['replica'];
|
|
$now = time();
|
|
$artifact = new CloudTemplateArtifact();
|
|
$artifact->template_id = (string) $template->id;
|
|
$artifact->storage_artifact_id = (int) $binding['artifact_id'];
|
|
$artifact->storage_revision = (int) $intent->storage_revision;
|
|
$artifact->object_key = (string) $primary->object_key;
|
|
$artifact->object_version_id = (string) $identity['version_id'];
|
|
$artifact->object_etag = (string) $identity['etag'];
|
|
$artifact->size_bytes = (int) $identity['size_bytes'];
|
|
$artifact->sha256 = (string) $identity['sha256'];
|
|
$artifact->state = 'ready';
|
|
$artifact->error_code = '';
|
|
$artifact->delete_operation_id = '';
|
|
$artifact->delete_generation = 0;
|
|
$artifact->delete_cycle_attempts = 0;
|
|
$artifact->delete_presence_confirmations = 0;
|
|
$artifact->delete_last_checked_at = 0;
|
|
$artifact->delete_next_check_at = 0;
|
|
$artifact->delete_attention_at = null;
|
|
$artifact->verified_at = $now;
|
|
$artifact->created_at = $artifact->updated_at = $now;
|
|
$artifact->save();
|
|
return $artifact;
|
|
}
|
|
|
|
/** Return a bounded URL from one exact, healthy, verified replica. */
|
|
public static function downloadSource(
|
|
CloudTemplate $template,
|
|
?CloudObjectStorageS3Client $client = null
|
|
): array {
|
|
$artifact = CloudTemplateArtifact::find((string) $template->id);
|
|
$storageArtifactId = (int) ($artifact?->storage_artifact_id ?? 0);
|
|
if (!$artifact instanceof CloudTemplateArtifact
|
|
|| $storageArtifactId < 1
|
|
|| (string) $artifact->state !== 'ready'
|
|
|| (int) $artifact->size_bytes !== (int) $template->image_bytes
|
|
|| !hash_equals(
|
|
(string) $artifact->sha256,
|
|
(string) $template->sha256
|
|
)) {
|
|
throw new CloudError('模板主副本尚未就绪', 409);
|
|
}
|
|
$read = CloudStorageArtifactService::presignedRead(
|
|
$storageArtifactId,
|
|
self::DOWNLOAD_SECONDS,
|
|
$client
|
|
);
|
|
if ((int) $read['size_bytes'] !== (int) $artifact->size_bytes
|
|
|| !hash_equals(
|
|
(string) $artifact->sha256,
|
|
(string) $read['sha256']
|
|
)) {
|
|
throw new CloudError('模板读取副本身份与目录不一致', 409);
|
|
}
|
|
return [
|
|
'source_url' => (string) $read['url'],
|
|
'source_size_bytes' => (int) $artifact->size_bytes,
|
|
'sha256' => (string) $artifact->sha256,
|
|
];
|
|
}
|
|
}
|