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",
"version": "0.1.6",
"version": "0.2.0",
"description": "Mihomo Party",
"main": "./out/main/index.js",
"author": "mihomo-party",

View File

@ -35,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy'
import { checkUpdate } from '../resolve/autoUpdater'
import { exePath, mihomoCorePath } from './dirs'
import { execSync } from 'child_process'
import fs from 'fs'
export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoVersion', mihomoVersion)
@ -70,6 +71,8 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
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('getVersion', () => app.getVersion())
ipcMain.handle('platform', () => process.platform)
@ -77,6 +80,18 @@ export function registerIpcMainHandlers(): void {
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> {
return new Promise((resolve, reject) => {
const removeCommand = `

View File

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