chore: 针对备份及还原处理
This commit is contained in:
@@ -75,12 +75,8 @@ class AutoDeployer
|
||||
$zipFiles = $this->analyzeZipStructure($zip);
|
||||
$this->logMessage('INFO', "ZIP包中包含 {$zipFiles['fileCount']} 个文件");
|
||||
|
||||
// 分析目标目录结构
|
||||
$targetFiles = $this->analyzeTargetDirectory();
|
||||
$this->logMessage('INFO', "目标目录中包含 {$targetFiles['fileCount']} 个文件");
|
||||
|
||||
// 比较差异并部署
|
||||
$result = $this->compareAndDeploy($zip, $zipFiles, $targetFiles);
|
||||
// 基于ZIP文件结构进行部署,不再预先分析目标目录
|
||||
$result = $this->compareAndDeploy($zip, $zipFiles);
|
||||
|
||||
// 关闭ZIP文件
|
||||
$zip->close();
|
||||
@@ -136,49 +132,25 @@ class AutoDeployer
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析目标目录结构
|
||||
* 获取单个文件信息
|
||||
*/
|
||||
private function analyzeTargetDirectory(): array
|
||||
private function getFileInfo(string $filePath): ?array
|
||||
{
|
||||
$files = [];
|
||||
$fileCount = 0;
|
||||
$totalSize = 0;
|
||||
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator(
|
||||
$this->targetDir,
|
||||
RecursiveDirectoryIterator::SKIP_DOTS
|
||||
),
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$relativePath = $this->getRelativePath($file->getPathname(), $this->targetDir);
|
||||
$files[$relativePath] = [
|
||||
'size' => $file->getSize(),
|
||||
'modified' => $file->getMTime(),
|
||||
'permissions' => $file->getPerms()
|
||||
];
|
||||
|
||||
$fileCount++;
|
||||
$totalSize += $file->getSize();
|
||||
if (!file_exists($filePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'files' => $files,
|
||||
'fileCount' => $fileCount,
|
||||
'totalSize' => $totalSize
|
||||
'size' => filesize($filePath),
|
||||
'modified' => filemtime($filePath),
|
||||
'permissions' => fileperms($filePath)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较差异并部署文件
|
||||
*/
|
||||
private function compareAndDeploy(ZipArchive $zip, array $zipFiles, array $targetFiles): array
|
||||
private function compareAndDeploy(ZipArchive $zip, array $zipFiles): array
|
||||
{
|
||||
$results = [
|
||||
'added' => 0,
|
||||
@@ -190,10 +162,10 @@ class AutoDeployer
|
||||
|
||||
foreach ($zipFiles['files'] as $filePath => $zipFileInfo) {
|
||||
$targetFilePath = $this->targetDir . DIRECTORY_SEPARATOR . $filePath;
|
||||
$targetFileExists = isset($targetFiles['files'][$filePath]);
|
||||
|
||||
try {
|
||||
if (!$targetFileExists) {
|
||||
// 直接检查文件是否存在,不再依赖预先分析的目标目录结构
|
||||
if (!file_exists($targetFilePath)) {
|
||||
// 新文件 - 需要创建目录并复制文件
|
||||
$result = $this->deployNewFile($zip, $filePath, $targetFilePath);
|
||||
$results['added']++;
|
||||
@@ -204,7 +176,8 @@ class AutoDeployer
|
||||
];
|
||||
} else {
|
||||
// 文件已存在 - 检查是否需要更新
|
||||
$needsUpdate = $this->needsUpdate($zip, $filePath, $targetFiles['files'][$filePath]);
|
||||
$targetFileInfo = $this->getFileInfo($targetFilePath);
|
||||
$needsUpdate = $this->needsUpdate($zip, $filePath, $targetFileInfo);
|
||||
|
||||
if ($needsUpdate) {
|
||||
$result = $this->deployUpdatedFile($zip, $filePath, $targetFilePath);
|
||||
@@ -313,8 +286,13 @@ class AutoDeployer
|
||||
/**
|
||||
* 检查文件是否需要更新
|
||||
*/
|
||||
private function needsUpdate(ZipArchive $zip, string $filePath, array $targetFileInfo): bool
|
||||
private function needsUpdate(ZipArchive $zip, string $filePath, ?array $targetFileInfo): bool
|
||||
{
|
||||
// 如果目标文件信息不存在,返回false(这种情况实际上不会发生,因为前面已经检查了文件存在)
|
||||
if ($targetFileInfo === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$zipFileInfo = $zip->statName($filePath);
|
||||
if ($zipFileInfo === false) {
|
||||
return false;
|
||||
@@ -333,7 +311,8 @@ class AutoDeployer
|
||||
}
|
||||
|
||||
// 比较CRC32校验(最可靠的方法)
|
||||
$targetCrc = hash_file('crc32b', $this->targetDir . DIRECTORY_SEPARATOR . $filePath);
|
||||
$targetFilePath = $this->targetDir . DIRECTORY_SEPARATOR . $filePath;
|
||||
$targetCrc = hash_file('crc32b', $targetFilePath);
|
||||
$zipCrc = sprintf('%08x', $zipFileInfo['crc']);
|
||||
|
||||
if (strtolower($targetCrc) !== strtolower($zipCrc)) {
|
||||
|
||||
Reference in New Issue
Block a user