feat: implement IPC handlers for service management and add MySQL operation service support
This commit is contained in:
@@ -10,6 +10,7 @@ import { nginxManagerService } from '../services/NginxManagerService'
|
||||
import { phpMyAdminService } from '../services/PhpMyAdminService'
|
||||
import { portMappingService } from '../services/PortMappingService'
|
||||
import path from 'path'
|
||||
import { mySqlOperationService } from '../services/MySqlOperationService'
|
||||
|
||||
function findExecutable(dir: string, name: string): string | null {
|
||||
if (!fs.existsSync(dir)) return null
|
||||
@@ -349,6 +350,17 @@ export function registerIpcHandlers(): void {
|
||||
return result.filePaths[0]
|
||||
})
|
||||
|
||||
ipcMain.handle('dialog:select-file', async (_event, filters: any[]) => {
|
||||
const result = await dialog.showOpenDialog({
|
||||
properties: ['openFile'],
|
||||
filters: filters || [
|
||||
{ name: 'SQL & Archives', extensions: ['sql', 'zip', 'gz', 'tar'] }
|
||||
]
|
||||
})
|
||||
if (result.canceled) return null
|
||||
return result.filePaths[0]
|
||||
})
|
||||
|
||||
ipcMain.handle('binaries:download', async (_event, { name, url }) => {
|
||||
return downloadService.downloadAndExtract(url, name, (p) => {
|
||||
// TODO: Emit progress to renderer via webContents.send
|
||||
@@ -411,4 +423,75 @@ export function registerIpcHandlers(): void {
|
||||
return { success: false, message: `Sıfırlama hatası: ${error.message}` }
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.handle('mysql:import-wizard:check-db', async (_event, { versionId, dbName }) => {
|
||||
const versions = await mySqlManagerService.getVersions()
|
||||
const v = versions.find(v => v.id === versionId)
|
||||
if (!v) return { exists: false, error: 'MySQL version not found' }
|
||||
|
||||
const binDir = path.join(configService.getBasePath(), 'bin', versionId)
|
||||
const binPath = findExecutable(binDir, 'mysql.exe')
|
||||
if (!binPath) return { exists: false, error: 'mysql.exe not found' }
|
||||
|
||||
const exists = await mySqlOperationService.checkDatabaseExists({
|
||||
binPath,
|
||||
host: '127.0.0.1',
|
||||
port: v.port,
|
||||
rootUser: 'root',
|
||||
rootPass: '' // Assuming root has no password by default in this setup
|
||||
}, dbName)
|
||||
|
||||
return { exists }
|
||||
})
|
||||
|
||||
ipcMain.handle('mysql:import-wizard:start', async (event, { versionId, filePath, dbName, user, pass, overwrite }) => {
|
||||
const versions = await mySqlManagerService.getVersions()
|
||||
const v = versions.find(v => v.id === versionId)
|
||||
if (!v) return { success: false, message: 'MySQL version not found' }
|
||||
|
||||
const binDir = path.join(configService.getBasePath(), 'bin', versionId)
|
||||
const mysqlBin = findExecutable(binDir, 'mysql.exe')
|
||||
if (!mysqlBin) return { success: false, message: 'mysql.exe not found' }
|
||||
|
||||
const config = {
|
||||
binPath: mysqlBin,
|
||||
host: '127.0.0.1',
|
||||
port: v.port,
|
||||
rootUser: 'root',
|
||||
rootPass: ''
|
||||
}
|
||||
|
||||
const log = (msg: string) => event.sender.send('mysql:import:log', msg)
|
||||
|
||||
try {
|
||||
if (overwrite) {
|
||||
log(`Dropping existing database: ${dbName}`)
|
||||
await mySqlOperationService.dropDatabase(config, dbName)
|
||||
}
|
||||
|
||||
log(`Creating database and user: ${dbName} / ${user}`)
|
||||
const createRes = await mySqlOperationService.createDatabaseAndUser(config, dbName, user, pass)
|
||||
if (!createRes.success) return createRes
|
||||
|
||||
let sqlFiles: string[] = []
|
||||
if (filePath.toLowerCase().endsWith('.sql')) {
|
||||
sqlFiles = [filePath]
|
||||
} else {
|
||||
log(`Extracting archive: ${path.basename(filePath)}`)
|
||||
const extractDir = path.join(app.getPath('temp'), `mysql_import_${Date.now()}`)
|
||||
sqlFiles = await mySqlOperationService.extractArchive(filePath, extractDir)
|
||||
if (sqlFiles.length === 0) return { success: false, message: 'No .sql files found in archive.' }
|
||||
}
|
||||
|
||||
for (const sqlFile of sqlFiles) {
|
||||
log(`Importing: ${path.basename(sqlFile)}`)
|
||||
const importRes = await mySqlOperationService.importSql(config, dbName, sqlFile, (msg) => log(msg))
|
||||
if (!importRes.success) return importRes
|
||||
}
|
||||
|
||||
return { success: true, message: 'Import completed successfully.' }
|
||||
} catch (error: any) {
|
||||
return { success: false, message: `Import error: ${error.message}` }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user