app.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/wailsapp/wails/v2/pkg/runtime"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. // App struct
  12. type App struct {
  13. ctx context.Context
  14. }
  15. // NewApp creates a new App application struct
  16. func NewApp() *App {
  17. return &App{}
  18. }
  19. // startup is called when the app starts. The context is saved
  20. // so we can call the runtime methods
  21. func (a *App) startup(ctx context.Context) {
  22. a.ctx = ctx
  23. }
  24. // Greet returns a greeting for the given name
  25. func (a *App) Greet(name string) string {
  26. return fmt.Sprintf("Hello %s, It's show time!", name)
  27. }
  28. // SelectDirectory 目录选择方法
  29. func (a *App) SelectDirectory() (string, error) {
  30. //目录选择方法
  31. options := runtime.OpenDialogOptions{
  32. Title: "选择目录",
  33. Filters: []runtime.FileFilter{},
  34. DefaultDirectory: "",
  35. DefaultFilename: "", // 允许选择目录
  36. }
  37. result, err := runtime.OpenDirectoryDialog(a.ctx, options)
  38. if err != nil {
  39. return "", err
  40. }
  41. if result == "" {
  42. return "", nil // 用户取消选择
  43. }
  44. return result, nil
  45. }
  46. // HandlerDirectory 处理目录复制的主要方法
  47. func (a *App) HandlerDirectory(sourceDir, targetDir string) error {
  48. // 检查源目录是否存在
  49. if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
  50. return fmt.Errorf("源目录不存在: %s", sourceDir)
  51. }
  52. // 检查目标目录,如果不存在则创建
  53. if _, err := os.Stat(targetDir); os.IsNotExist(err) {
  54. // 源目录不存在时不需要创建目录,但目标目录可以创建
  55. err := os.MkdirAll(targetDir, os.ModePerm)
  56. if err != nil {
  57. return fmt.Errorf("创建目标目录失败: %v", err)
  58. }
  59. }
  60. // 执行具体的处理逻辑
  61. return a.processDirectories(sourceDir, targetDir)
  62. }
  63. // ProcessResult 处理结果结构体
  64. type ProcessResult struct {
  65. Success bool `json:"success"`
  66. Message string `json:"message"`
  67. Progress int `json:"progress"`
  68. }
  69. // isValidImageFile 检查文件是否为有效的图片文件
  70. func (a *App) isValidImageFile(filename string) bool {
  71. validExtensions := []string{".avif", ".bmp", ".png", ".jpg", ".jpeg"}
  72. ext := strings.ToLower(filepath.Ext(filename))
  73. for _, validExt := range validExtensions {
  74. if ext == validExt {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. // ProcessCallback 处理过程回调函数类型
  81. type ProcessCallback func(result ProcessResult)
  82. // processDirectories 实际处理目录的逻辑
  83. func (a *App) processDirectories(sourceDir, targetDir string) error {
  84. // 发送初始进度
  85. a.sendProgress(ProcessResult{
  86. Success: true,
  87. Message: "开始处理目录...",
  88. Progress: 0,
  89. })
  90. // 遍历和处理逻辑
  91. err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
  92. if err != nil {
  93. a.sendProgress(ProcessResult{
  94. Success: false,
  95. Message: fmt.Sprintf("访问路径出错: %v", err),
  96. Progress: 0,
  97. })
  98. return err
  99. }
  100. fmt.Printf("正在处理: %s\n", path)
  101. // 处理 800x800 目录的逻辑
  102. if info.IsDir() && strings.Contains(info.Name(), "800x800") {
  103. fmt.Printf("👍👍👍符合条件: %s\n", path)
  104. // 获取上级目录名称
  105. parentDirName := filepath.Base(filepath.Dir(path))
  106. // 在目标目录中创建以父目录命名的子目录
  107. newTargetDir := filepath.Join(targetDir, parentDirName)
  108. // 检查目标目录,如果不存在则创建
  109. if _, err := os.Stat(newTargetDir); os.IsNotExist(err) {
  110. // 源目录不存在时不需要创建目录,但目标目录可以创建
  111. err := os.MkdirAll(newTargetDir, os.ModePerm)
  112. if err != nil {
  113. return fmt.Errorf("创建目标目录失败: %v", err)
  114. }
  115. }
  116. a.sendProgress(ProcessResult{
  117. Success: true,
  118. Message: fmt.Sprintf("找到目录: %s,将在目标目录创建子目录: %s", info.Name(), parentDirName),
  119. Progress: 50,
  120. })
  121. // 执行复制操作到新创建的子目录
  122. err := a.copyFilesFromDir(path, newTargetDir)
  123. if err != nil {
  124. a.sendProgress(ProcessResult{
  125. Success: false,
  126. Message: fmt.Sprintf("复制文件到:%v 失败: %v", newTargetDir, err),
  127. Progress: 50,
  128. })
  129. return err
  130. }
  131. }
  132. return nil
  133. })
  134. // 发送完成进度
  135. if err != nil {
  136. a.sendProgress(ProcessResult{
  137. Success: false,
  138. Message: fmt.Sprintf("处理失败: %v", err),
  139. Progress: 100,
  140. })
  141. } else {
  142. a.sendProgress(ProcessResult{
  143. Success: true,
  144. Message: "处理完成",
  145. Progress: 100,
  146. })
  147. }
  148. return err
  149. }
  150. // copyFilesFromDir 从指定目录复制所有文件到目标目录
  151. func (a *App) copyFilesFromDir(sourceDir, targetDir string) error {
  152. entries, err := os.ReadDir(sourceDir)
  153. if err != nil {
  154. return fmt.Errorf("读取目录失败 %s: %v", sourceDir, err)
  155. }
  156. for _, entry := range entries {
  157. // 只处理文件,跳过子目录
  158. if entry.IsDir() {
  159. continue // 忽略目录
  160. }
  161. // 检查是否为有效的图片文件
  162. if !a.isValidImageFile(entry.Name()) {
  163. continue // 跳过非图片文件
  164. }
  165. sourcePath := filepath.Join(sourceDir, entry.Name())
  166. targetPath := filepath.Join(targetDir, entry.Name())
  167. err := a.copyFile(sourcePath, targetPath)
  168. if err != nil {
  169. return fmt.Errorf("复制文件失败 %s 到 %s: %v", sourcePath, targetPath, err)
  170. }
  171. }
  172. return nil
  173. }
  174. // copyFile 复制单个文件
  175. func (a *App) copyFile(src, dst string) error {
  176. sourceFile, err := os.Open(src)
  177. if err != nil {
  178. return err
  179. }
  180. defer sourceFile.Close()
  181. // 创建目标文件
  182. destFile, err := os.Create(dst)
  183. if err != nil {
  184. return err
  185. }
  186. defer destFile.Close()
  187. // 复制文件内容
  188. _, err = io.Copy(destFile, sourceFile)
  189. if err != nil {
  190. return err
  191. }
  192. // 同步文件内容到磁盘
  193. return destFile.Sync()
  194. }
  195. // sendProgress 发送进度更新
  196. func (a *App) sendProgress(result ProcessResult) {
  197. // 这里可以通过事件系统发送进度更新
  198. // 具体实现在下一步中说明
  199. runtime.EventsEmit(a.ctx, "copy800-progress", result)
  200. }