add executable file basic check

This commit is contained in:
geekgeekrun
2026-02-08 00:11:07 +08:00
parent 3f2b709097
commit 8cc94baa18
2 changed files with 33 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ import { BrowserWindow, dialog, ipcMain, shell } from 'electron'
import gtag from './gtag'
import buildInfo from '../../common/build-info.json'
import os from 'node:os'
import fs from 'node:fs'
import {
ensureStorageFileExist,
readStorageFile,
@@ -85,4 +86,28 @@ export default function initPublicIpc() {
return dialog.showOpenDialog(win, fileChooserConfig)
}
})
ipcMain.handle('check-executable-file', (ev, filePath: string) => {
if (!filePath?.trim()) {
return {
message: '文件名无效'
}
}
if (!fs.existsSync(filePath)) {
return {
message: '文件不存在'
}
}
if (!fs.statSync(filePath).isFile()) {
const messageSeg = ['文件不是可执行文件']
if (os.platform() === 'darwin') {
messageSeg.push(
'macOS 平台可执行文件位于“App包.app/Contents/MacOS/ 文件夹下”而不是“App包.app”文件夹整体'
)
}
return {
message: messageSeg.join('')
}
}
return null
})
}

View File

@@ -124,15 +124,19 @@ const formData = ref({
const rules = {
browserPath: {
validator: (_, value, callback) => {
validator: async (_, value, callback) => {
if (!value?.trim()) {
callback(new Error('请输入浏览器可执行文件路径'))
return
}
// TODO: 检查文件是否存在
else {
const err = await ipcRenderer.invoke('check-executable-file', value)
if (err) {
callback(err?.message ?? '文件无效 - 未知原因')
} else {
callback()
}
}
},
trigger: 'blur'
}
}