mirror of
https://github.com/GSManagerXZ/GameServerManager.git
synced 2026-06-02 11:02:33 +08:00
增加文件操作接口
This commit is contained in:
@@ -158,6 +158,141 @@ class GSM3API {
|
||||
return await this.request('/games')
|
||||
}
|
||||
|
||||
// ==================== 文件操作API ====================
|
||||
|
||||
/**
|
||||
* 读取文件内容
|
||||
* @param {string} filePath 文件路径(相对于服务器data目录)
|
||||
* @param {string} encoding 文件编码,默认为'utf-8',二进制文件使用'binary'
|
||||
*/
|
||||
async readFile(filePath, encoding = 'utf-8') {
|
||||
return await this.request('/files/read', {
|
||||
method: 'POST',
|
||||
body: { filePath, encoding }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文件内容
|
||||
* @param {string} filePath 文件路径(相对于服务器data目录)
|
||||
* @param {string} content 文件内容
|
||||
* @param {string} encoding 文件编码,默认为'utf-8'
|
||||
*/
|
||||
async writeFile(filePath, content, encoding = 'utf-8') {
|
||||
return await this.request('/files/write', {
|
||||
method: 'POST',
|
||||
body: { filePath, content, encoding }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param {string} filePath 文件路径(相对于服务器data目录)
|
||||
*/
|
||||
async deleteFile(filePath) {
|
||||
return await this.request('/files/delete', {
|
||||
method: 'DELETE',
|
||||
body: { filePath }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建目录
|
||||
* @param {string} dirPath 目录路径(相对于服务器data目录)
|
||||
* @param {boolean} recursive 是否递归创建父目录,默认为true
|
||||
*/
|
||||
async createDirectory(dirPath, recursive = true) {
|
||||
return await this.request('/files/mkdir', {
|
||||
method: 'POST',
|
||||
body: { dirPath, recursive }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除目录
|
||||
* @param {string} dirPath 目录路径(相对于服务器data目录)
|
||||
* @param {boolean} recursive 是否递归删除,默认为false
|
||||
*/
|
||||
async deleteDirectory(dirPath, recursive = false) {
|
||||
return await this.request('/files/rmdir', {
|
||||
method: 'DELETE',
|
||||
body: { dirPath, recursive }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出目录内容
|
||||
* @param {string} dirPath 目录路径(相对于服务器data目录),默认为根目录
|
||||
* @param {boolean} includeHidden 是否包含隐藏文件,默认为false
|
||||
*/
|
||||
async listDirectory(dirPath = '', includeHidden = false) {
|
||||
return await this.request('/files/list', {
|
||||
method: 'POST',
|
||||
body: { dirPath, includeHidden }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件或目录信息
|
||||
* @param {string} path 文件或目录路径(相对于服务器data目录)
|
||||
*/
|
||||
async getFileInfo(path) {
|
||||
return await this.request('/files/info', {
|
||||
method: 'POST',
|
||||
body: { path }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文件或目录是否存在
|
||||
* @param {string} path 文件或目录路径(相对于服务器data目录)
|
||||
*/
|
||||
async exists(path) {
|
||||
return await this.request('/files/exists', {
|
||||
method: 'POST',
|
||||
body: { path }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文件或目录
|
||||
* @param {string} sourcePath 源路径(相对于服务器data目录)
|
||||
* @param {string} destPath 目标路径(相对于服务器data目录)
|
||||
* @param {boolean} overwrite 是否覆盖已存在的文件,默认为false
|
||||
*/
|
||||
async copy(sourcePath, destPath, overwrite = false) {
|
||||
return await this.request('/files/copy', {
|
||||
method: 'POST',
|
||||
body: { sourcePath, destPath, overwrite }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动/重命名文件或目录
|
||||
* @param {string} sourcePath 源路径(相对于服务器data目录)
|
||||
* @param {string} destPath 目标路径(相对于服务器data目录)
|
||||
* @param {boolean} overwrite 是否覆盖已存在的文件,默认为false
|
||||
*/
|
||||
async move(sourcePath, destPath, overwrite = false) {
|
||||
return await this.request('/files/move', {
|
||||
method: 'POST',
|
||||
body: { sourcePath, destPath, overwrite }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索文件
|
||||
* @param {string} pattern 搜索模式(支持通配符)
|
||||
* @param {string} searchPath 搜索路径(相对于服务器data目录),默认为根目录
|
||||
* @param {boolean} recursive 是否递归搜索子目录,默认为true
|
||||
*/
|
||||
async searchFiles(pattern, searchPath = '', recursive = true) {
|
||||
return await this.request('/files/search', {
|
||||
method: 'POST',
|
||||
body: { pattern, searchPath, recursive }
|
||||
})
|
||||
}
|
||||
|
||||
// ==================== 通用API ====================
|
||||
|
||||
/**
|
||||
|
||||
@@ -308,6 +308,31 @@
|
||||
<button class="btn btn-warning" onclick="getSystemInfo()">获取系统信息</button>
|
||||
<button class="btn btn-info" onclick="getApiVersion()">获取API版本</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="demo-section">
|
||||
<h3>📁 文件操作演示</h3>
|
||||
<p>体验插件文件操作API的功能:</p>
|
||||
|
||||
<div class="button-group">
|
||||
<button class="btn" onclick="listRootDirectory()">列出根目录</button>
|
||||
<button class="btn btn-success" onclick="createTestFile()">创建测试文件</button>
|
||||
<button class="btn btn-warning" onclick="readTestFile()">读取测试文件</button>
|
||||
<button class="btn btn-info" onclick="deleteTestFile()">删除测试文件</button>
|
||||
</div>
|
||||
|
||||
<div class="button-group">
|
||||
<button class="btn" onclick="createTestDirectory()">创建测试目录</button>
|
||||
<button class="btn btn-success" onclick="copyTestFile()">复制文件</button>
|
||||
<button class="btn btn-warning" onclick="moveTestFile()">移动文件</button>
|
||||
<button class="btn btn-info" onclick="searchFiles()">搜索文件</button>
|
||||
</div>
|
||||
|
||||
<div class="button-group">
|
||||
<input type="text" id="filePathInput" placeholder="输入文件路径" value="test.txt" style="padding: 12px; border-radius: 25px; border: 1px solid rgba(255,255,255,0.3); background: rgba(255,255,255,0.1); color: white; margin-right: 10px;">
|
||||
<button class="btn" onclick="checkFileExists()">检查文件是否存在</button>
|
||||
<button class="btn btn-info" onclick="getFileInfo()">获取文件信息</button>
|
||||
</div>
|
||||
|
||||
<div id="apiResult" class="status-display" style="max-height: 300px; overflow-y: auto; white-space: pre-wrap; font-family: 'Courier New', monospace;">
|
||||
<strong>API调用结果:</strong> 点击上方按钮查看API响应
|
||||
@@ -517,6 +542,275 @@
|
||||
apiResult.style.borderLeft = '4px solid #ff6b6b'
|
||||
}
|
||||
|
||||
// ==================== 文件操作函数 ====================
|
||||
|
||||
// 列出根目录
|
||||
async function listRootDirectory() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在列出根目录...')
|
||||
const result = await window.gsm3.listDirectory()
|
||||
showApiResult('根目录内容', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '目录列表获取成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('列出目录失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '列出目录失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建测试文件
|
||||
async function createTestFile() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在创建测试文件...')
|
||||
const content = `这是一个测试文件\n创建时间: ${new Date().toLocaleString()}\n随机数: ${Math.random()}`
|
||||
const result = await window.gsm3.writeFile('test.txt', content)
|
||||
showApiResult('创建测试文件', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '测试文件创建成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('创建文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '创建文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 读取测试文件
|
||||
async function readTestFile() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在读取测试文件...')
|
||||
const result = await window.gsm3.readFile('test.txt')
|
||||
showApiResult('读取测试文件', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件读取成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('读取文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '读取文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除测试文件
|
||||
async function deleteTestFile() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在删除测试文件...')
|
||||
const result = await window.gsm3.deleteFile('test.txt')
|
||||
showApiResult('删除测试文件', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件删除成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('删除文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '删除文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建测试目录
|
||||
async function createTestDirectory() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在创建测试目录...')
|
||||
const result = await window.gsm3.createDirectory('test-dir')
|
||||
showApiResult('创建测试目录', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '目录创建成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('创建目录失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '创建目录失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
async function copyTestFile() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在检查源文件...')
|
||||
const exists = await window.gsm3.exists('test.txt')
|
||||
if (!exists.exists) {
|
||||
showApiError('复制文件失败', new Error('源文件 test.txt 不存在,请先创建测试文件'))
|
||||
return
|
||||
}
|
||||
|
||||
showApiLoading('正在复制文件...')
|
||||
const result = await window.gsm3.copy('test.txt', 'test-copy.txt')
|
||||
showApiResult('复制文件', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件复制成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('复制文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '复制文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 移动文件
|
||||
async function moveTestFile() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在检查源文件...')
|
||||
const exists = await window.gsm3.exists('test-copy.txt')
|
||||
if (!exists.exists) {
|
||||
showApiError('移动文件失败', new Error('源文件 test-copy.txt 不存在,请先复制文件'))
|
||||
return
|
||||
}
|
||||
|
||||
showApiLoading('正在移动文件...')
|
||||
const result = await window.gsm3.move('test-copy.txt', 'test-moved.txt')
|
||||
showApiResult('移动文件', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件移动成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('移动文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '移动文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索文件
|
||||
async function searchFiles() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在搜索文件...')
|
||||
const result = await window.gsm3.searchFiles('*.txt')
|
||||
showApiResult('搜索文件 (*.txt)', result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件搜索完成')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('搜索文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '搜索文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
async function checkFileExists() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
const filePath = document.getElementById('filePathInput').value
|
||||
if (!filePath) {
|
||||
showApiError('检查文件失败', new Error('请输入文件路径'))
|
||||
return
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在检查文件是否存在...')
|
||||
const result = await window.gsm3.exists(filePath)
|
||||
showApiResult(`检查文件是否存在: ${filePath}`, result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('info', '文件检查完成')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('检查文件失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '检查文件失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取文件信息
|
||||
async function getFileInfo() {
|
||||
try {
|
||||
if (!window.gsm3) {
|
||||
throw new Error('GSM3 API对象未找到')
|
||||
}
|
||||
|
||||
const filePath = document.getElementById('filePathInput').value
|
||||
if (!filePath) {
|
||||
showApiError('获取文件信息失败', new Error('请输入文件路径'))
|
||||
return
|
||||
}
|
||||
|
||||
showApiLoading('正在初始化API...')
|
||||
await window.gsm3.initialize()
|
||||
|
||||
showApiLoading('正在获取文件信息...')
|
||||
const result = await window.gsm3.getFileInfo(filePath)
|
||||
showApiResult(`获取文件信息: ${filePath}`, result)
|
||||
if (window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('success', '文件信息获取成功')
|
||||
}
|
||||
} catch (error) {
|
||||
showApiError('获取文件信息失败', error)
|
||||
if (window.gsm3 && window.gsm3.showNotification) {
|
||||
window.gsm3.showNotification('error', '获取文件信息失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 控制台欢迎信息
|
||||
console.log('%c🧩 GSM3 插件系统', 'color: #667eea; font-size: 20px; font-weight: bold;')
|
||||
console.log('%c欢迎使用示例插件!', 'color: #764ba2; font-size: 14px;')
|
||||
|
||||
Reference in New Issue
Block a user