dockerfile

This commit is contained in:
浪子
2026-03-19 19:39:14 +08:00
parent 04eccc850d
commit 62cffc6f5f
13 changed files with 578 additions and 51 deletions
+87 -36
View File
@@ -32,8 +32,8 @@ class VersionPublishService
$sha256 = hash_file('sha256', $incomingPath);
$size = filesize($incomingPath) ?: 0;
$manifest = $this->extractAndValidate($incomingPath, $extractDir, $package, $sha256);
$versionNumber = (string) $manifest['version'];
$metadata = $this->extractAndValidate($incomingPath, $extractDir, $package, $sha256, $options);
$versionNumber = (string) $metadata['version'];
if ($package->versions()->where('version', $versionNumber)->exists()) {
throw new RuntimeException('version already exists');
@@ -46,12 +46,12 @@ class VersionPublishService
$payload = [
'version' => $versionNumber,
'changelog' => $options['changelog'] ?? ($manifest['changelog'] ?? ''),
'typecho_min' => data_get($manifest, 'compatibility.typecho_min', '1.2.0'),
'typecho_max' => data_get($manifest, 'compatibility.typecho_max', ''),
'php_min' => data_get($manifest, 'compatibility.php_min', '7.4'),
'php_max' => data_get($manifest, 'compatibility.php_max', ''),
'php_extensions' => data_get($manifest, 'compatibility.php_extensions', []),
'changelog' => $options['changelog'] ?? ($metadata['changelog'] ?? ''),
'typecho_min' => $metadata['typecho_min'] ?? '1.2.0',
'typecho_max' => $metadata['typecho_max'] ?? '',
'php_min' => $metadata['php_min'] ?? '7.4',
'php_max' => $metadata['php_max'] ?? '',
'php_extensions' => $metadata['php_extensions'] ?? [],
'package_url' => $this->buildPackageUrl($package, $versionNumber),
'package_size' => $size,
'sha256' => $sha256,
@@ -74,7 +74,7 @@ class VersionPublishService
}
}
private function extractAndValidate(string $zipPath, string $extractDir, Package $package, string $sha256): array
private function extractAndValidate(string $zipPath, string $extractDir, Package $package, string $sha256, array $options): array
{
$zip = new ZipArchive();
$opened = $zip->open($zipPath);
@@ -84,7 +84,6 @@ class VersionPublishService
$topLevel = [];
$manifestRaw = null;
$rootDirExists = false;
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
@@ -104,41 +103,42 @@ class VersionPublishService
}
}
if ($manifestRaw === null) {
$zip->close();
throw new RuntimeException('manifest.json not found at zip root');
}
try {
if ($manifestRaw !== null) {
$manifest = json_decode($manifestRaw, true);
if (!is_array($manifest)) {
throw new RuntimeException('invalid manifest.json');
}
$manifest = json_decode($manifestRaw, true);
if (!is_array($manifest)) {
$zip->close();
throw new RuntimeException('invalid manifest.json');
}
$this->validateManifest($manifest, $package, $sha256);
$this->assertTopLevelStructure($topLevel, ['manifest.json', $package->slug], 'zip root structure must contain only manifest.json and package root dir');
$metadata = [
'version' => (string) $manifest['version'],
'changelog' => (string) ($manifest['changelog'] ?? ''),
'typecho_min' => (string) data_get($manifest, 'compatibility.typecho_min', '1.2.0'),
'typecho_max' => (string) data_get($manifest, 'compatibility.typecho_max', ''),
'php_min' => (string) data_get($manifest, 'compatibility.php_min', '7.4'),
'php_max' => (string) data_get($manifest, 'compatibility.php_max', ''),
'php_extensions' => $this->normalizePhpExtensions(data_get($manifest, 'compatibility.php_extensions', [])),
];
} else {
$this->assertTopLevelStructure($topLevel, [$package->slug], 'zip root structure must contain only package root dir when manifest.json is absent');
$metadata = $this->buildFallbackMetadata($options);
}
$this->validateManifest($manifest, $package, $sha256);
$allowedTopLevel = ['manifest.json', $package->slug];
$topLevelNames = array_keys($topLevel);
sort($topLevelNames);
sort($allowedTopLevel);
if ($topLevelNames !== $allowedTopLevel) {
if (!$zip->extractTo($extractDir)) {
throw new RuntimeException('failed to extract zip package');
}
} finally {
$zip->close();
throw new RuntimeException('zip root structure must contain only manifest.json and package root dir');
}
if (!$zip->extractTo($extractDir)) {
$zip->close();
throw new RuntimeException('failed to extract zip package');
}
$zip->close();
$rootDir = $extractDir . DIRECTORY_SEPARATOR . $package->slug;
$rootDirExists = is_dir($rootDir);
if (!$rootDirExists) {
if (!is_dir($rootDir)) {
throw new RuntimeException('package root dir missing after extraction');
}
return $manifest;
return $metadata;
}
private function validateManifest(array $manifest, Package $package, string $sha256): void
@@ -212,4 +212,55 @@ class VersionPublishService
return $base === '' ? $path : $base . $path;
}
private function assertTopLevelStructure(array $topLevel, array $allowedTopLevel, string $message): void
{
$topLevelNames = array_keys($topLevel);
sort($topLevelNames);
sort($allowedTopLevel);
if ($topLevelNames !== $allowedTopLevel) {
throw new RuntimeException($message);
}
}
private function buildFallbackMetadata(array $options): array
{
$version = trim((string) ($options['version'] ?? ''));
if ($version === '') {
throw new RuntimeException('manifest.json not found at zip root; version is required for zip publish');
}
if (!preg_match('/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/', $version)) {
throw new RuntimeException('fallback version is not valid semver');
}
return [
'version' => $version,
'changelog' => (string) ($options['changelog'] ?? ''),
'typecho_min' => (string) ($options['typecho_min'] ?? '1.2.0'),
'typecho_max' => (string) ($options['typecho_max'] ?? ''),
'php_min' => (string) ($options['php_min'] ?? '7.4'),
'php_max' => (string) ($options['php_max'] ?? ''),
'php_extensions' => $this->normalizePhpExtensions($options['php_extensions'] ?? []),
];
}
private function normalizePhpExtensions(mixed $value): array
{
if (is_string($value)) {
$value = preg_split('/[,\s]+/u', $value);
}
if (!is_array($value)) {
return [];
}
return collect($value)
->map(fn ($item) => trim((string) $item))
->filter()
->values()
->all();
}
}