This commit is contained in:
pompurin404 2024-08-07 21:24:54 +08:00
parent fd86c0dba4
commit 8093aa95bb
No known key found for this signature in database
4 changed files with 48 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "mihomo-party", "name": "mihomo-party",
"version": "0.1.6", "version": "0.2.0",
"description": "Mihomo Party", "description": "Mihomo Party",
"main": "./out/main/index.js", "main": "./out/main/index.js",
"author": "mihomo-party", "author": "mihomo-party",

View File

@ -35,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy'
import { checkUpdate } from '../resolve/autoUpdater' import { checkUpdate } from '../resolve/autoUpdater'
import { exePath, mihomoCorePath } from './dirs' import { exePath, mihomoCorePath } from './dirs'
import { execSync } from 'child_process' import { execSync } from 'child_process'
import fs from 'fs'
export function registerIpcMainHandlers(): void { export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoVersion', mihomoVersion) ipcMain.handle('mihomoVersion', mihomoVersion)
@ -70,6 +71,8 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable)) ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable) ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str)) ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))
ipcMain.handle('getFilePath', getFilePath)
ipcMain.handle('readTextFile', (_e, filePath) => readTextFile(filePath))
ipcMain.handle('checkUpdate', () => checkUpdate()) ipcMain.handle('checkUpdate', () => checkUpdate())
ipcMain.handle('getVersion', () => app.getVersion()) ipcMain.handle('getVersion', () => app.getVersion())
ipcMain.handle('platform', () => process.platform) ipcMain.handle('platform', () => process.platform)
@ -77,6 +80,18 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('quitApp', () => app.quit()) ipcMain.handle('quitApp', () => app.quit())
} }
function getFilePath(): string[] | undefined {
return dialog.showOpenDialogSync({
title: '选择订阅文件',
filters: [{ name: 'Yaml Files', extensions: ['yml', 'yaml'] }],
properties: ['openFile']
})
}
function readTextFile(filePath: string): string {
return fs.readFileSync(filePath, 'utf8')
}
async function setupFirewall(): Promise<void> { async function setupFirewall(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const removeCommand = ` const removeCommand = `

View File

@ -2,6 +2,7 @@ import { Button, Input } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page' import BasePage from '@renderer/components/base/base-page'
import ProfileItem from '@renderer/components/profiles/profile-item' import ProfileItem from '@renderer/components/profiles/profile-item'
import { useProfileConfig } from '@renderer/hooks/use-profile-config' import { useProfileConfig } from '@renderer/hooks/use-profile-config'
import { getFilePath, readTextFile } from '@renderer/utils/ipc'
import { useEffect, useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { MdContentPaste } from 'react-icons/md' import { MdContentPaste } from 'react-icons/md'
@ -40,22 +41,18 @@ const Profiles: React.FC = () => {
e.stopPropagation() e.stopPropagation()
setFileOver(false) setFileOver(false)
}) })
pageRef.current?.addEventListener('drop', (event) => { pageRef.current?.addEventListener('drop', async (event) => {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
if (event.dataTransfer?.files) { if (event.dataTransfer?.files) {
const file = event.dataTransfer.files[0] const file = event.dataTransfer.files[0]
if (file.name.endsWith('.yml') || file.name.endsWith('.yaml')) { if (file.name.endsWith('.yml') || file.name.endsWith('.yaml')) {
const reader = new FileReader() const content = await readTextFile(file.path)
reader.onload = async (e): Promise<void> => {
const content = e.target?.result as string
try { try {
await addProfileItem({ name: file.name, type: 'local', file: content }) await addProfileItem({ name: file.name, type: 'local', file: content })
} finally { } finally {
setFileOver(false) setFileOver(false)
} }
}
reader.readAsText(file)
} else { } else {
alert('不支持的文件类型') alert('不支持的文件类型')
} }
@ -74,7 +71,6 @@ const Profiles: React.FC = () => {
<div className="sticky top-[48px] z-40 backdrop-blur bg-background/40 flex p-2"> <div className="sticky top-[48px] z-40 backdrop-blur bg-background/40 flex p-2">
<Input <Input
variant="bordered" variant="bordered"
className="mr-2"
size="sm" size="sm"
value={url} value={url}
onValueChange={setUrl} onValueChange={setUrl}
@ -96,12 +92,29 @@ const Profiles: React.FC = () => {
<Button <Button
size="sm" size="sm"
color="primary" color="primary"
className="ml-2"
isDisabled={url === ''} isDisabled={url === ''}
isLoading={importing} isLoading={importing}
onPress={handleImport} onPress={handleImport}
> >
</Button> </Button>
<Button
size="sm"
color="primary"
className="ml-2"
onPress={() => {
getFilePath().then(async (files) => {
if (files?.length) {
const content = await readTextFile(files[0])
const fileName = files[0].split('/').pop()?.split('\\').pop()
await addProfileItem({ name: fileName, type: 'local', file: content })
}
})
}}
>
</Button>
</div> </div>
<div <div
className={`${fileOver ? 'blur-sm' : ''} grid sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2 mx-2`} className={`${fileOver ? 'blur-sm' : ''} grid sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2 mx-2`}

View File

@ -131,6 +131,14 @@ export async function encryptString(str: string): Promise<Buffer> {
return await window.electron.ipcRenderer.invoke('encryptString', str) return await window.electron.ipcRenderer.invoke('encryptString', str)
} }
export async function getFilePath(): Promise<string[] | undefined> {
return await window.electron.ipcRenderer.invoke('getFilePath')
}
export async function readTextFile(filePath: string): Promise<string> {
return await window.electron.ipcRenderer.invoke('readTextFile', filePath)
}
export async function checkUpdate(): Promise<string | undefined> { export async function checkUpdate(): Promise<string | undefined> {
return await window.electron.ipcRenderer.invoke('checkUpdate') return await window.electron.ipcRenderer.invoke('checkUpdate')
} }