getKey();
$backup = CleanupBackup::with('files')->find($backupId);
if (!$backup) {
return $this->response()
->error('备份不存在');
}
$files = $backup->files;
if ($files->isEmpty()) {
return $this->response()
->info('该备份暂无文件');
}
$html = '
';
$html .= '| 表名 | 文件名 | 文件大小 | 文件路径 | 备份类型 | 压缩类型 | 哈希值 |
';
$html .= '';
$backupTypes = [1 => 'SQL', 2 => 'JSON', 3 => 'CSV'];
$compressionTypes = [1 => '无压缩', 2 => 'GZIP', 3 => 'ZIP'];
foreach ($files as $file) {
$backupType = $backupTypes[$file->backup_type] ?? '未知';
$compressionType = $compressionTypes[$file->compression_type] ?? '未知';
$fileSize = $this->formatBytes($file->file_size);
$hash = $file->file_hash ? substr($file->file_hash, 0, 16) . '...' : '-';
$html .= "";
$html .= "| {$file->table_name} | ";
$html .= "{$file->file_name} | ";
$html .= "{$fileSize} | ";
$html .= "{$file->file_path} | ";
$html .= "{$backupType} | ";
$html .= "{$compressionType} | ";
$html .= "{$hash} | ";
$html .= "
";
}
$html .= '
';
// 添加统计信息
$totalSize = $files->sum('file_size');
$totalFiles = $files->count();
$html .= '';
$html .= '
';
$html .= '
' . $totalFiles . '
';
$html .= '文件总数';
$html .= '';
$html .= '
';
$html .= '
' . $this->formatBytes($totalSize) . '
';
$html .= '文件总大小';
$html .= '';
$html .= '
';
return $this->response()
->success('备份文件列表')
->detail($html);
} catch (\Exception $e) {
return $this->response()
->error('查看失败:' . $e->getMessage());
}
}
/**
* 格式化字节大小
*/
private function formatBytes($bytes)
{
if ($bytes == 0) return '0 B';
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$base = log($bytes, 1024);
$index = floor($base);
if ($index >= count($units)) {
$index = count($units) - 1;
}
$size = round(pow(1024, $base - $index), 2);
return $size . ' ' . $units[$index];
}
/**
* 渲染按钮
*/
public function render()
{
return <<
{$this->title}
HTML;
}
}