| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package main
- import (
- "context"
- "fmt"
- "github.com/wailsapp/wails/v2/pkg/runtime"
- "io"
- "os"
- "path/filepath"
- "strings"
- )
- // App struct
- type App struct {
- ctx context.Context
- }
- // NewApp creates a new App application struct
- func NewApp() *App {
- return &App{}
- }
- // startup is called when the app starts. The context is saved
- // so we can call the runtime methods
- func (a *App) startup(ctx context.Context) {
- a.ctx = ctx
- }
- // Greet returns a greeting for the given name
- func (a *App) Greet(name string) string {
- return fmt.Sprintf("Hello %s, It's show time!", name)
- }
- // SelectDirectory 目录选择方法
- func (a *App) SelectDirectory() (string, error) {
- //目录选择方法
- options := runtime.OpenDialogOptions{
- Title: "选择目录",
- Filters: []runtime.FileFilter{},
- DefaultDirectory: "",
- DefaultFilename: "", // 允许选择目录
- }
- result, err := runtime.OpenDirectoryDialog(a.ctx, options)
- if err != nil {
- return "", err
- }
- if result == "" {
- return "", nil // 用户取消选择
- }
- return result, nil
- }
- // HandlerDirectory 处理目录复制的主要方法
- func (a *App) HandlerDirectory(sourceDir, targetDir string) error {
- // 检查源目录是否存在
- if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
- return fmt.Errorf("源目录不存在: %s", sourceDir)
- }
- // 检查目标目录,如果不存在则创建
- if _, err := os.Stat(targetDir); os.IsNotExist(err) {
- // 源目录不存在时不需要创建目录,但目标目录可以创建
- err := os.MkdirAll(targetDir, os.ModePerm)
- if err != nil {
- return fmt.Errorf("创建目标目录失败: %v", err)
- }
- }
- // 执行具体的处理逻辑
- return a.processDirectories(sourceDir, targetDir)
- }
- // ProcessResult 处理结果结构体
- type ProcessResult struct {
- Success bool `json:"success"`
- Message string `json:"message"`
- Progress int `json:"progress"`
- }
- // isValidImageFile 检查文件是否为有效的图片文件
- func (a *App) isValidImageFile(filename string) bool {
- validExtensions := []string{".avif", ".bmp", ".png", ".jpg", ".jpeg"}
- ext := strings.ToLower(filepath.Ext(filename))
- for _, validExt := range validExtensions {
- if ext == validExt {
- return true
- }
- }
- return false
- }
- // ProcessCallback 处理过程回调函数类型
- type ProcessCallback func(result ProcessResult)
- // processDirectories 实际处理目录的逻辑
- func (a *App) processDirectories(sourceDir, targetDir string) error {
- // 发送初始进度
- a.sendProgress(ProcessResult{
- Success: true,
- Message: "开始处理目录...",
- Progress: 0,
- })
- // 遍历和处理逻辑
- err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
- if err != nil {
- a.sendProgress(ProcessResult{
- Success: false,
- Message: fmt.Sprintf("访问路径出错: %v", err),
- Progress: 0,
- })
- return err
- }
- fmt.Printf("正在处理: %s\n", path)
- // 处理 800x800 目录的逻辑
- if info.IsDir() && strings.Contains(info.Name(), "800x800") {
- fmt.Printf("👍👍👍符合条件: %s\n", path)
- // 获取上级目录名称
- parentDirName := filepath.Base(filepath.Dir(path))
- // 在目标目录中创建以父目录命名的子目录
- newTargetDir := filepath.Join(targetDir, parentDirName)
- // 检查目标目录,如果不存在则创建
- if _, err := os.Stat(newTargetDir); os.IsNotExist(err) {
- // 源目录不存在时不需要创建目录,但目标目录可以创建
- err := os.MkdirAll(newTargetDir, os.ModePerm)
- if err != nil {
- return fmt.Errorf("创建目标目录失败: %v", err)
- }
- }
- a.sendProgress(ProcessResult{
- Success: true,
- Message: fmt.Sprintf("找到目录: %s,将在目标目录创建子目录: %s", info.Name(), parentDirName),
- Progress: 50,
- })
- // 执行复制操作到新创建的子目录
- err := a.copyFilesFromDir(path, newTargetDir)
- if err != nil {
- a.sendProgress(ProcessResult{
- Success: false,
- Message: fmt.Sprintf("复制文件到:%v 失败: %v", newTargetDir, err),
- Progress: 50,
- })
- return err
- }
- }
- return nil
- })
- // 发送完成进度
- if err != nil {
- a.sendProgress(ProcessResult{
- Success: false,
- Message: fmt.Sprintf("处理失败: %v", err),
- Progress: 100,
- })
- } else {
- a.sendProgress(ProcessResult{
- Success: true,
- Message: "处理完成",
- Progress: 100,
- })
- }
- return err
- }
- // copyFilesFromDir 从指定目录复制所有文件到目标目录
- func (a *App) copyFilesFromDir(sourceDir, targetDir string) error {
- entries, err := os.ReadDir(sourceDir)
- if err != nil {
- return fmt.Errorf("读取目录失败 %s: %v", sourceDir, err)
- }
- for _, entry := range entries {
- // 只处理文件,跳过子目录
- if entry.IsDir() {
- continue // 忽略目录
- }
- // 检查是否为有效的图片文件
- if !a.isValidImageFile(entry.Name()) {
- continue // 跳过非图片文件
- }
- sourcePath := filepath.Join(sourceDir, entry.Name())
- targetPath := filepath.Join(targetDir, entry.Name())
- err := a.copyFile(sourcePath, targetPath)
- if err != nil {
- return fmt.Errorf("复制文件失败 %s 到 %s: %v", sourcePath, targetPath, err)
- }
- }
- return nil
- }
- // copyFile 复制单个文件
- func (a *App) copyFile(src, dst string) error {
- sourceFile, err := os.Open(src)
- if err != nil {
- return err
- }
- defer sourceFile.Close()
- // 创建目标文件
- destFile, err := os.Create(dst)
- if err != nil {
- return err
- }
- defer destFile.Close()
- // 复制文件内容
- _, err = io.Copy(destFile, sourceFile)
- if err != nil {
- return err
- }
- // 同步文件内容到磁盘
- return destFile.Sync()
- }
- // sendProgress 发送进度更新
- func (a *App) sendProgress(result ProcessResult) {
- // 这里可以通过事件系统发送进度更新
- // 具体实现在下一步中说明
- runtime.EventsEmit(a.ctx, "copy800-progress", result)
- }
|