修复mc整合包部署

This commit is contained in:
小朱
2025-07-20 00:05:50 +08:00
parent 1f0230677b
commit 2f96a0a42f
6 changed files with 29 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
import axios from 'axios';
import express, { Request, Response } from 'express';
import * as fs from 'fs-extra';
import { promises as fsPromises } from 'fs';
import { createWriteStream, createReadStream } from 'fs';
import * as path from 'path';
import * as os from 'os';
@@ -44,10 +45,10 @@ export class FactorioDeployer {
private async detectFileFormat(filePath: string): Promise<string> {
try {
const buffer = Buffer.alloc(512);
const fd = await fs.open(filePath, 'r');
const fd = await fsPromises.open(filePath, 'r');
try {
await fs.read(fd, buffer, 0, 512, 0);
await fd.read(buffer, 0, 512, 0);
// 检查文件头魔数
const header = buffer.toString('hex');
@@ -86,7 +87,7 @@ export class FactorioDeployer {
}
} finally {
await fs.close(fd);
await fd.close();
}
// 如果魔数检测失败,根据文件扩展名判断
@@ -200,7 +201,7 @@ export class FactorioDeployer {
await this.downloadServer(initialTempFilePath);
// 检查实际下载的文件路径
const tempFiles = await fs.readdir(tempDir);
const tempFiles = await fsPromises.readdir(tempDir);
const downloadedFile = tempFiles.find(file =>
file.startsWith('factorio-server-') &&
(file.endsWith('.tar.xz') || file.endsWith('.tar.gz') || file.endsWith('.zip') || file.endsWith('.tar'))
@@ -326,7 +327,7 @@ export class FactorioDeployer {
return new Promise((resolve, reject) => {
writer.on('finish', () => {
if (this.cancelled) {
fs.unlink(adjustedFilePath).catch(() => {});
fsPromises.unlink(adjustedFilePath).catch(() => {});
reject(new Error('操作已取消'));
return;
}
@@ -341,13 +342,13 @@ export class FactorioDeployer {
});
writer.on('error', (error) => {
fs.unlink(adjustedFilePath).catch(() => {});
fsPromises.unlink(adjustedFilePath).catch(() => {});
reject(error);
});
response.data.on('error', (error: any) => {
if (error.name === 'AbortError') {
fs.unlink(adjustedFilePath).catch(() => {});
fsPromises.unlink(adjustedFilePath).catch(() => {});
reject(new Error('操作已取消'));
} else {
reject(error);
@@ -376,7 +377,7 @@ export class FactorioDeployer {
*/
private async checkFileIntegrity(filePath: string): Promise<boolean> {
try {
const stats = await fs.stat(filePath);
const stats = await fsPromises.stat(filePath);
console.log(`文件大小: ${stats.size} 字节`);
// 检查文件大小是否合理Factorio服务端通常大于10MB
@@ -387,10 +388,10 @@ export class FactorioDeployer {
// 尝试读取文件头部分来验证文件完整性
const buffer = Buffer.alloc(1024);
const fd = await fs.open(filePath, 'r');
const fd = await fsPromises.open(filePath, 'r');
try {
await fs.read(fd, buffer, 0, 1024, 0);
await fd.read(buffer, 0, 1024, 0);
// 检查是否为有效的压缩文件头
if (buffer[0] === 0x00 && buffer[1] === 0x00) {
@@ -400,7 +401,7 @@ export class FactorioDeployer {
return true;
} finally {
await fs.close(fd);
await fd.close();
}
} catch (error) {
console.error('文件完整性检查失败:', error);

View File

@@ -1,5 +1,6 @@
import axios from 'axios';
import * as fs from 'fs-extra';
import { promises as fsPromises } from 'fs';
import { createWriteStream } from 'fs';
import * as path from 'path';
import { spawn, ChildProcess } from 'child_process';
@@ -228,7 +229,7 @@ export class FileManager {
try {
// 尝试在目录中创建一个临时文件来测试写权限
const testFile = path.join(dirPath, '.write-test-' + Date.now());
await fs.writeFile(testFile, 'test');
await fsPromises.writeFile(testFile, 'test');
await fs.remove(testFile);
return true;
} catch (error) {
@@ -256,7 +257,7 @@ export class FileManager {
*/
static async fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
await fsPromises.access(filePath);
return true;
} catch {
return false;
@@ -365,7 +366,7 @@ export class FileManager {
}
// 获取临时目录中的所有文件
const files = await fs.readdir(this.tempDir);
const files = await fsPromises.readdir(this.tempDir);
if (onLog) {
onLog(`正在移动 ${files.length} 个文件到目标目录: ${normalizedTargetDir}`, 'info');
@@ -394,7 +395,7 @@ export class FileManager {
throw new Error(`文件移动后验证失败: ${file}`);
}
const stat = await fs.stat(targetPath);
const stat = await fsPromises.stat(targetPath);
if (stat.isFile()) {
if (onLog) {
onLog(`已移动文件: ${file}`, 'info');

View File

@@ -1,5 +1,6 @@
import axios from 'axios';
import * as fs from 'fs-extra';
import { promises as fsPromises } from 'fs';
import { createWriteStream } from 'fs';
import * as path from 'path';
import * as yauzl from 'yauzl';
@@ -643,7 +644,7 @@ export class MrpackServerAPI {
await fs.ensureDir(targetDir);
// 获取源目录中的所有文件
const files = await fs.readdir(sourceDir);
const files = await fsPromises.readdir(sourceDir);
if (onProgress) {
onProgress(`正在移动 ${files.length} 个文件到目标目录...`, 'info');
@@ -657,7 +658,7 @@ export class MrpackServerAPI {
// 统一使用move操作无论是文件还是目录
await fs.move(sourcePath, targetPath, { overwrite: true });
const stat = await fs.stat(targetPath);
const stat = await fsPromises.stat(targetPath);
if (stat.isFile()) {
if (onProgress) {
onProgress(`已移动文件: ${file}`, 'info');

View File

@@ -420,7 +420,7 @@ export async function createTempDirectory(deployment: ActiveDeployment, prefix:
export async function getMinecraftServerCategories(): Promise<MinecraftServerCategory[]> {
try {
// 使用minecraft-server-api.ts中的API服务
const { getServerCategories } = await import('./minecraft-server-api');
const { getServerCategories } = await import('./minecraft-server-api.js');
const categories = await getServerCategories();
// 转换为统一函数库的格式
@@ -443,7 +443,7 @@ export async function getMinecraftServerCategories(): Promise<MinecraftServerCat
export async function getMinecraftVersions(server: string): Promise<string[]> {
try {
// 使用minecraft-server-api.ts中的API服务
const { getAvailableVersions } = await import('./minecraft-server-api');
const { getAvailableVersions } = await import('./minecraft-server-api.js');
return await getAvailableVersions(server);
} catch (error) {
throw new Error(`获取版本列表失败: ${error instanceof Error ? error.message : String(error)}`);
@@ -456,7 +456,7 @@ export async function getMinecraftVersions(server: string): Promise<string[]> {
export async function getMinecraftDownloadInfo(server: string, version: string): Promise<MinecraftDownloadData> {
try {
// 使用minecraft-server-api.ts中的API服务
const { getDownloadInfo } = await import('./minecraft-server-api');
const { getDownloadInfo } = await import('./minecraft-server-api.js');
const downloadData = await getDownloadInfo(server, version);
return {
url: downloadData.url,
@@ -601,7 +601,7 @@ export async function downloadFileWithCancellation(
// 清理可能已创建的部分文件
writer.end();
try {
await fs.unlink(filePath);
await fsPromises.unlink(filePath);
} catch (unlinkError) {
// 忽略删除文件时的错误
}
@@ -1366,7 +1366,7 @@ export async function extractZipFileWithCancellation(zipPath: string, extractPat
deployment.cancellationToken.onCancelled(() => {
readStream.destroy();
writeStream.destroy();
fs.unlink(filePath).catch(() => {});
fsPromises.unlink(filePath).catch(() => {});
});
writeStream.on('finish', () => {

View File

@@ -1,5 +1,5 @@
import { Router, Request, Response } from 'express'
import { getServerCategories, getAvailableVersions, getDownloadInfo, validateJavaEnvironment } from '../modules/game/othergame/minecraft-server-api.js'
import { getServerCategories, getAvailableVersions, getDownloadInfo, validateJavaEnvironment } from '../modules/game/othergame/minecraft-server-api'
import {
getMinecraftServerCategories,
getMinecraftVersions,

View File

@@ -2,9 +2,9 @@ import { Router, Request, Response } from 'express'
import { promises as fs } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { deployTModLoaderServer, deployFactorioServer, cancelDeployment, getActiveDeployments, getTModLoaderInfo, searchMrpackModpacks, getMrpackProjectVersions, deployMrpackServer } from '../modules/game/othergame/unified-functions.js'
import { authenticateToken } from '../middleware/auth.js'
import logger from '../utils/logger.js'
import { deployTModLoaderServer, deployFactorioServer, cancelDeployment, getActiveDeployments, getTModLoaderInfo, searchMrpackModpacks, getMrpackProjectVersions, deployMrpackServer } from '../modules/game/othergame/unified-functions'
import { authenticateToken } from '../middleware/auth'
import logger from '../utils/logger'
import { Server as SocketIOServer } from 'socket.io'
const __filename = fileURLToPath(import.meta.url)