feat: list folder recursively

This commit is contained in:
TaurusXin 2025-02-14 16:17:24 +08:00
parent c523f1a59c
commit f9c4c1c7cf
Signed by: taurusxin
GPG Key ID: C334DCA04AC2D2CC
3 changed files with 61 additions and 2 deletions

16
app.go
View File

@ -6,6 +6,7 @@ import (
"github.com/wailsapp/wails/v2/pkg/runtime"
"git.taurusxin.com/taurusxin/ncmdump-go/ncmcrypt"
"git.taurusxin.com/taurusxin/ncmdump-gui/utils"
)
// App struct
@ -51,6 +52,21 @@ func (a *App) SelectFolder() string {
return folder
}
func (a *App) SelectFilesFromFolder(ext string) []string {
folder, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "请选择文件夹",
})
if err != nil {
return []string{}
} else {
files, err := utils.ListFilesFromFolder(folder, ext)
if err != nil {
return []string{}
}
return files
}
}
type Status = string
const (

View File

@ -38,11 +38,12 @@ import {
DocumentAddRegular,
DeleteRegular,
DeleteDismissRegular,
FolderAddRegular,
WindowPlayRegular,
} from '@fluentui/react-icons'
import { Status, Item, SaveTo } from './types'
import { SelectFiles, SelectFolder, ProcessFiles } from '../wailsjs/go/main/App'
import { SelectFiles, SelectFolder, SelectFilesFromFolder, ProcessFiles } from '../wailsjs/go/main/App'
import { Load, Save } from '../wailsjs/go/utils/ConfigManager'
import { main } from '../wailsjs/go/models'
import { EventsOn, OnFileDrop } from '../wailsjs/runtime/runtime'
@ -155,6 +156,17 @@ export const App = () => {
})
}
const selectFilesFromFolder = () => {
if (isProcessing) {
return
}
SelectFilesFromFolder('ncm').then(files => {
for (const file of files) {
setItems(prev => [...prev, { file, status: 'pending' }])
}
})
}
const showDialog = (message: string) => {
setMessage(message)
setOpen(true)
@ -178,7 +190,7 @@ export const App = () => {
showDialog('当前文件列表已全部处理完毕,请重新添加新的文件。')
return
}
if(saveTo === 'custom' && savePath === '') {
if (saveTo === 'custom' && savePath === '') {
showDialog('保存路径为空,请先设置保存路径。')
return
}
@ -254,6 +266,7 @@ export const App = () => {
<Button onClick={selectFiles} icon={<DocumentAddRegular />}>
</Button>
<Button onClick={selectFilesFromFolder} icon={<FolderAddRegular />}></Button>
<Button
onClick={() => {
if (!isProcessing) {

30
utils/file_util.go Normal file
View File

@ -0,0 +1,30 @@
package utils
import (
"os"
"path/filepath"
)
func ListFilesFromFolder(root string, ext string) ([]string, error) {
var files []string
// Walk函数会遍历文件树递归地访问每个目录和文件
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 检查是否是文件以及文件后缀是否匹配
if !info.IsDir() && filepath.Ext(path) == "."+ext {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}