dockerfile
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\WebAdmin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\PublishZipVersionRequest;
|
||||
use App\Http\Requests\Admin\StoreCategoryRequest;
|
||||
use App\Http\Requests\Admin\StorePackageRequest;
|
||||
use App\Models\Category;
|
||||
@@ -220,17 +221,10 @@ class DashboardController extends Controller
|
||||
return redirect()->route('webadmin.packages.show', [$type, $slug])->with('success', '版本已添加:v' . $version->version);
|
||||
}
|
||||
|
||||
public function publishVersion(Request $request, string $type, string $slug): RedirectResponse
|
||||
public function publishVersion(PublishZipVersionRequest $request, string $type, string $slug): RedirectResponse
|
||||
{
|
||||
$package = $this->findPackage($type, $slug);
|
||||
|
||||
$validated = $request->validate([
|
||||
'package_file' => ['required', 'file', 'mimes:zip', 'max:51200'],
|
||||
'is_stable' => ['nullable', 'boolean'],
|
||||
'mark_as_latest' => ['nullable', 'boolean'],
|
||||
'changelog' => ['nullable', 'string'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
]);
|
||||
$validated = $request->validated();
|
||||
|
||||
try {
|
||||
$version = $this->publishService->publishFromZip($package, $request->file('package_file'), $validated);
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Validator;
|
||||
use ZipArchive;
|
||||
|
||||
class PublishZipVersionRequest extends FormRequest
|
||||
{
|
||||
@@ -11,14 +13,75 @@ class PublishZipVersionRequest extends FormRequest
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$extensions = $this->input('php_extensions');
|
||||
|
||||
if (is_string($extensions)) {
|
||||
$this->merge([
|
||||
'php_extensions' => collect(preg_split('/[,,\s]+/u', $extensions))
|
||||
->map(fn ($item) => trim((string) $item))
|
||||
->filter()
|
||||
->values()
|
||||
->all(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'package_file' => ['required', 'file', 'mimes:zip', 'max:51200'],
|
||||
'version' => ['nullable', 'string', 'max:32', 'regex:/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/'],
|
||||
'typecho_min' => ['nullable', 'string', 'max:16'],
|
||||
'typecho_max' => ['nullable', 'string', 'max:16'],
|
||||
'php_min' => ['nullable', 'string', 'max:16'],
|
||||
'php_max' => ['nullable', 'string', 'max:16'],
|
||||
'php_extensions' => ['nullable', 'array'],
|
||||
'php_extensions.*' => ['string', 'max:32'],
|
||||
'is_stable' => ['nullable', 'boolean'],
|
||||
'mark_as_latest' => ['nullable', 'boolean'],
|
||||
'changelog' => ['nullable', 'string'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(function (Validator $validator) {
|
||||
$file = $this->file('package_file');
|
||||
|
||||
if (!$file || !$file->isValid() || $this->zipHasManifestAtRoot($file->getRealPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (blank((string) $this->input('version', ''))) {
|
||||
$validator->errors()->add('version', 'zip 根目录没有 manifest.json 时,必须手动填写版本号。');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function zipHasManifestAtRoot(string $path): bool
|
||||
{
|
||||
$zip = new ZipArchive();
|
||||
|
||||
if ($zip->open($path) !== true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||||
$stat = $zip->statIndex($i);
|
||||
$name = trim((string) ($stat['name'] ?? ''), '/');
|
||||
|
||||
if ($name === 'manifest.json') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} finally {
|
||||
$zip->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user