Files
tk-electron-ai/main/ipc/files.js
2025-10-28 19:40:13 +08:00

25 lines
912 B
JavaScript

// main/ipc/files.js
const { ipcMain } = require('electron')
const fsp = require('node:fs/promises')
const { normalizePath } = require('../utils/paths')
function registerFileIpc() {
ipcMain.removeHandler('file-exists')
ipcMain.handle('file-exists', async (_evt, targetPath, baseDir) => {
try {
const full = normalizePath(targetPath, baseDir)
const stat = await fsp.stat(full).catch(err => (err?.code === 'ENOENT' ? null : Promise.reject(err)))
if (!stat) return { ok: true, exists: false, path: full }
return {
ok: true, exists: true, path: full,
isFile: stat.isFile(), isDirectory: stat.isDirectory(),
size: stat.size, mtimeMs: stat.mtimeMs
}
} catch (e) {
return { ok: false, error: e.message || String(e) }
}
})
}
module.exports = { registerFileIpc }