182 lines
5.7 KiB
JavaScript
182 lines
5.7 KiB
JavaScript
/**
|
||
* 文件操作相关的功能类
|
||
* @type {module}
|
||
*/
|
||
import { XmlUtils } from './xml-utils.js';
|
||
|
||
export class FileOperations {
|
||
/**
|
||
* 加载服务文件列表
|
||
*/
|
||
static async loadServiceFiles() {
|
||
try {
|
||
const response = await fetch('/api/service-files');
|
||
if (!response.ok) {
|
||
throw new Error(`服务器响应错误: ${response.status}`);
|
||
}
|
||
return await response.json();
|
||
} catch (error) {
|
||
console.error('加载服务文件失败:', error);
|
||
|
||
// 延迟3秒后重试
|
||
return new Promise((resolve) => {
|
||
setTimeout(async () => {
|
||
try {
|
||
console.log('尝试重新加载服务文件列表...');
|
||
const retryResponse = await fetch('/api/service-files');
|
||
if (retryResponse.ok) {
|
||
resolve(await retryResponse.json());
|
||
} else {
|
||
console.error('重试加载服务文件失败');
|
||
resolve([]);
|
||
}
|
||
} catch (retryError) {
|
||
console.error('重试加载服务文件失败:', retryError);
|
||
resolve([]);
|
||
}
|
||
}, 3000);
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 加载文件内容
|
||
*/
|
||
static async loadFileContent(filePath) {
|
||
try {
|
||
const response = await fetch(`/api/service-file-content?path=${encodeURIComponent(filePath)}`);
|
||
if (!response.ok) {
|
||
throw new Error(`服务器响应错误: ${response.status}`);
|
||
}
|
||
|
||
const content = await response.text();
|
||
|
||
// 解析XML内容
|
||
const parser = new DOMParser();
|
||
let xmlDoc;
|
||
|
||
try {
|
||
xmlDoc = parser.parseFromString(content, 'application/xml');
|
||
// 检查解析错误
|
||
const parseError = xmlDoc.querySelector('parsererror');
|
||
if (parseError) {
|
||
throw new Error('XML解析错误');
|
||
}
|
||
|
||
// 特殊处理CommandList元素,确保命令以属性方式存储
|
||
XmlUtils.ensureCommandListFormat(xmlDoc);
|
||
|
||
return {
|
||
content,
|
||
xmlDoc,
|
||
error: null
|
||
};
|
||
} catch (parseError) {
|
||
console.error('XML解析错误:', parseError);
|
||
}
|
||
} catch (error) {
|
||
console.error('加载文件内容失败:', error);
|
||
return {
|
||
content: null,
|
||
xmlDoc: null,
|
||
error: error.message
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存文件内容
|
||
*/
|
||
static async saveFileContent(filePath, content) {
|
||
try {
|
||
const response = await fetch('/api/save-service-file', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({
|
||
path: filePath,
|
||
content: content
|
||
})
|
||
});
|
||
|
||
if (!response.ok) {
|
||
const errorData = await response.json();
|
||
throw new Error(errorData.error || `保存失败: ${response.status}`);
|
||
}
|
||
|
||
return await response.json();
|
||
} catch (error) {
|
||
console.error('保存文件失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建新配置
|
||
*/
|
||
static async createNewConfig(fileName) {
|
||
try {
|
||
const response = await fetch('/api/create-service-file', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({ fileName })
|
||
});
|
||
|
||
if (!response.ok) {
|
||
const errorData = await response.json();
|
||
throw new Error(errorData.error || `创建失败: ${response.status}`);
|
||
}
|
||
|
||
return await response.json();
|
||
} catch (error) {
|
||
console.error('创建新配置文件失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 另存为新文件
|
||
*/
|
||
static async saveFileAs(fileName, content, currentFile, overwrite = false) {
|
||
try {
|
||
const response = await fetch('/api/save-service-as', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({
|
||
fileName,
|
||
content,
|
||
currentFile,
|
||
overwrite
|
||
})
|
||
});
|
||
|
||
if (!response.ok) {
|
||
const errorData = await response.json();
|
||
return {
|
||
success: false,
|
||
data: errorData,
|
||
error: errorData.error || `保存失败: ${response.status}`
|
||
};
|
||
}
|
||
|
||
const result = await response.json();
|
||
return {
|
||
success: true,
|
||
data: result,
|
||
error: null
|
||
};
|
||
} catch (error) {
|
||
console.error('另存为失败:', error);
|
||
return {
|
||
success: false,
|
||
data: null,
|
||
error: error.message
|
||
};
|
||
}
|
||
}
|
||
}
|