From 9eb5d2bd96a4f8bc1e8beae93e2538630dcd8001 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Mon, 24 Jul 2023 10:40:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20file=20hash=20duplicate=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wailsjs/go/duplicate/Support.d.ts | 8 ++ .../frontend/wailsjs/go/duplicate/Support.js | 15 ++++ .../frontend/wailsjs/go/file/Support.d.ts | 11 +++ app/wails/frontend/wailsjs/go/file/Support.js | 19 ++++ .../frontend/wailsjs/go/hash/Support.d.ts | 6 ++ app/wails/frontend/wailsjs/go/hash/Support.js | 11 +++ app/wails/go.mod | 2 +- app/wails/main.go | 10 ++- app/wails/pkg/sdk/file/duplicate/duplicate.go | 86 +++++++++++++++++++ app/wails/pkg/sdk/file/file.go | 79 +++++++++++++++++ app/wails/pkg/sdk/file/hash/hash.go | 58 +++++++++++++ app/wails/pkg/utils/json/json.go | 10 +++ 12 files changed, 313 insertions(+), 2 deletions(-) create mode 100644 app/wails/frontend/wailsjs/go/duplicate/Support.d.ts create mode 100644 app/wails/frontend/wailsjs/go/duplicate/Support.js create mode 100644 app/wails/frontend/wailsjs/go/file/Support.d.ts create mode 100644 app/wails/frontend/wailsjs/go/file/Support.js create mode 100644 app/wails/frontend/wailsjs/go/hash/Support.d.ts create mode 100644 app/wails/frontend/wailsjs/go/hash/Support.js create mode 100644 app/wails/pkg/sdk/file/duplicate/duplicate.go create mode 100644 app/wails/pkg/sdk/file/file.go create mode 100644 app/wails/pkg/sdk/file/hash/hash.go create mode 100644 app/wails/pkg/utils/json/json.go diff --git a/app/wails/frontend/wailsjs/go/duplicate/Support.d.ts b/app/wails/frontend/wailsjs/go/duplicate/Support.d.ts new file mode 100644 index 0000000..a4778fa --- /dev/null +++ b/app/wails/frontend/wailsjs/go/duplicate/Support.d.ts @@ -0,0 +1,8 @@ +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT + +export function CalcMD5(arg1:string):Promise; + +export function CalcSHA1(arg1:string):Promise; + +export function RecursiveScan(arg1:string,arg2:boolean,arg3:boolean):Promise>; diff --git a/app/wails/frontend/wailsjs/go/duplicate/Support.js b/app/wails/frontend/wailsjs/go/duplicate/Support.js new file mode 100644 index 0000000..2b81b82 --- /dev/null +++ b/app/wails/frontend/wailsjs/go/duplicate/Support.js @@ -0,0 +1,15 @@ +// @ts-check +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT + +export function CalcMD5(arg1) { + return window['go']['duplicate']['Support']['CalcMD5'](arg1); +} + +export function CalcSHA1(arg1) { + return window['go']['duplicate']['Support']['CalcSHA1'](arg1); +} + +export function RecursiveScan(arg1, arg2, arg3) { + return window['go']['duplicate']['Support']['RecursiveScan'](arg1, arg2, arg3); +} diff --git a/app/wails/frontend/wailsjs/go/file/Support.d.ts b/app/wails/frontend/wailsjs/go/file/Support.d.ts new file mode 100644 index 0000000..ea6eaa4 --- /dev/null +++ b/app/wails/frontend/wailsjs/go/file/Support.d.ts @@ -0,0 +1,11 @@ +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT +import {io} from '../models'; + +export function CopyN(arg1:io.Writer,arg2:string,arg3:number):Promise; + +export function IsDir(arg1:string):Promise; + +export function IsFile(arg1:string):Promise; + +export function SaveJSONFile(arg1:string,arg2:any):Promise; diff --git a/app/wails/frontend/wailsjs/go/file/Support.js b/app/wails/frontend/wailsjs/go/file/Support.js new file mode 100644 index 0000000..1af0368 --- /dev/null +++ b/app/wails/frontend/wailsjs/go/file/Support.js @@ -0,0 +1,19 @@ +// @ts-check +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT + +export function CopyN(arg1, arg2, arg3) { + return window['go']['file']['Support']['CopyN'](arg1, arg2, arg3); +} + +export function IsDir(arg1) { + return window['go']['file']['Support']['IsDir'](arg1); +} + +export function IsFile(arg1) { + return window['go']['file']['Support']['IsFile'](arg1); +} + +export function SaveJSONFile(arg1, arg2) { + return window['go']['file']['Support']['SaveJSONFile'](arg1, arg2); +} diff --git a/app/wails/frontend/wailsjs/go/hash/Support.d.ts b/app/wails/frontend/wailsjs/go/hash/Support.d.ts new file mode 100644 index 0000000..4f46d2d --- /dev/null +++ b/app/wails/frontend/wailsjs/go/hash/Support.d.ts @@ -0,0 +1,6 @@ +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT + +export function CalcMD5(arg1:string,arg2:number):Promise; + +export function CalcSHA1(arg1:string,arg2:number):Promise; diff --git a/app/wails/frontend/wailsjs/go/hash/Support.js b/app/wails/frontend/wailsjs/go/hash/Support.js new file mode 100644 index 0000000..e776d7e --- /dev/null +++ b/app/wails/frontend/wailsjs/go/hash/Support.js @@ -0,0 +1,11 @@ +// @ts-check +// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL +// This file is automatically generated. DO NOT EDIT + +export function CalcMD5(arg1, arg2) { + return window['go']['hash']['Support']['CalcMD5'](arg1, arg2); +} + +export function CalcSHA1(arg1, arg2) { + return window['go']['hash']['Support']['CalcSHA1'](arg1, arg2); +} diff --git a/app/wails/go.mod b/app/wails/go.mod index a594de4..60c1959 100644 --- a/app/wails/go.mod +++ b/app/wails/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 github.com/go-playground/validator/v10 v10.14.1 + github.com/goccy/go-json v0.10.2 github.com/gookit/goutil v0.6.11 github.com/mutecomm/go-sqlcipher/v4 v4.4.2 github.com/pelletier/go-toml/v2 v2.0.8 @@ -38,7 +39,6 @@ require ( github.com/go-openapi/spec v0.20.4 // indirect github.com/go-openapi/swag v0.19.15 // indirect github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect - github.com/goccy/go-json v0.10.2 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/uuid v1.3.0 // indirect diff --git a/app/wails/main.go b/app/wails/main.go index 8c2dc82..7999add 100644 --- a/app/wails/main.go +++ b/app/wails/main.go @@ -9,6 +9,9 @@ import ( "skapp/pkg/sdk/config" "skapp/pkg/sdk/dialog" "skapp/pkg/sdk/env" + fileSdk "skapp/pkg/sdk/file" + "skapp/pkg/sdk/file/duplicate" + "skapp/pkg/sdk/file/hash" "skapp/pkg/sdk/system" "skapp/pkg/sdk/utils" ) @@ -19,7 +22,9 @@ var assets embed.FS func main() { // Create an instance of the app structure app := core.NewApp() - + fileSupport := fileSdk.New() + hashSupport := hash.New(fileSupport) + duplicateSupport := duplicate.New(fileSupport, hashSupport) // Create application with options err := wails.Run(&options.App{ Title: "wails", @@ -40,6 +45,9 @@ func main() { &system.InfoUtils{}, dialog.New(app), &config.Support{}, + fileSupport, + hashSupport, + duplicateSupport, }, Debug: options.Debug{ OpenInspectorOnStartup: true, diff --git a/app/wails/pkg/sdk/file/duplicate/duplicate.go b/app/wails/pkg/sdk/file/duplicate/duplicate.go new file mode 100644 index 0000000..d2ecfb1 --- /dev/null +++ b/app/wails/pkg/sdk/file/duplicate/duplicate.go @@ -0,0 +1,86 @@ +package duplicate + +import ( + "bufio" + "io/fs" + "os" + "path/filepath" + "skapp/pkg/sdk/file/hash" + + "skapp/pkg/logger" + fileSdk "skapp/pkg/sdk/file" +) + +var log = logger.Log + +type Support struct { + fileSupport *fileSdk.Support + hash *hash.Support +} + +func New(fileSupport *fileSdk.Support, hash *hash.Support) *Support { + return &Support{fileSupport, hash} +} + +var ( + maxReadSize int64 = 1024 * 1024 * 5 + chunkSize = 1024 * 1024 +) + +// RecursiveScan 递归 目录下需要扫描的文件 +func (s *Support) RecursiveScan(dir string, addFile bool, addDir bool) []string { + //nodeModules, _ := regexp.Compile("node_modules") + //if nodeModules.Match([]byte(dir)) { + // return []string{} + //} + + files := make([]string, 0) + fileMap := make(map[string]bool) + //suffixReg, _ := regexp.Compile(".*[\\.j(t)s|\\.vue|\\.jsx|\\.tsx]$") + + absPath, _ := filepath.Abs(dir) + _ = filepath.Walk(absPath, func(file string, info fs.FileInfo, err error) error { + if info.Mode() == os.ModeSymlink { + file, err = os.Readlink(file) + s.RecursiveScan(file, addFile, addDir) + return err + } + + if addFile { + if s.fileSupport.IsFile(file) { + //if suffixReg.Match([]byte(file)) { + log.Infof("[扫描文件] 添加扫描文件 %s", file) + fileMap[file] = true + //} + } + } + + if addDir { + if s.fileSupport.IsDir(file) { + log.Infof("[扫描文件] 添加扫描文件夹 %s", file) + fileMap[file] = true + } + } + + return nil + }) + + files = make([]string, 0, len(fileMap)) + for file := range fileMap { + files = append(files, file) + } + + return files +} + +func Reader(file *os.File) *bufio.Reader { + return bufio.NewReaderSize(file, chunkSize) +} + +func (s *Support) CalcSHA1(path string) (sha1hex string, err error) { + return s.hash.CalcSHA1(path, maxReadSize) +} + +func (s *Support) CalcMD5(path string) (md5hex string, err error) { + return s.hash.CalcMD5(path, maxReadSize) +} diff --git a/app/wails/pkg/sdk/file/file.go b/app/wails/pkg/sdk/file/file.go new file mode 100644 index 0000000..9e5c6af --- /dev/null +++ b/app/wails/pkg/sdk/file/file.go @@ -0,0 +1,79 @@ +package file + +import ( + "errors" + "fmt" + "io" + "os" + "path/filepath" + "skapp/pkg/utils/json" +) + +type Support struct { +} + +func New() *Support { + return &Support{} +} + +func (s *Support) IsFile(path string) bool { + info, err := os.Stat(path) + if err != nil { + return false + } + + return !info.IsDir() +} + +func (s *Support) IsDir(path string) bool { + info, err := os.Stat(path) + if err != nil { + return false + } + + return info.IsDir() +} + +func (s *Support) CopyN(w io.Writer, path string, n int64) (int64, error) { + if s.IsDir(path) { + return 0, errors.New(fmt.Sprintf("%s 为文件夹", path)) + } + + file, err := os.Open(path) + defer func(file *os.File) { + _ = file.Close() + }(file) + + if err != nil { + return 0, err + } + + info, _ := file.Stat() + if n <= 0 { + return io.Copy(w, file) + } + + if info.Size() < n { + return io.Copy(w, file) + } else { + return io.CopyN(w, file, n) + } +} + +func (s *Support) SaveJSONFile(filePath string, data interface{}) error { + if !filepath.IsAbs(filePath) { + root, _ := os.Getwd() + filePath = filepath.Join(root, filePath) + } + + jsonFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + defer func(scmF *os.File) { + _ = scmF.Close() + }(jsonFile) + if err != nil { + return err + } + + _, err = jsonFile.WriteString(json.Json(data)) + return err +} diff --git a/app/wails/pkg/sdk/file/hash/hash.go b/app/wails/pkg/sdk/file/hash/hash.go new file mode 100644 index 0000000..2f969bf --- /dev/null +++ b/app/wails/pkg/sdk/file/hash/hash.go @@ -0,0 +1,58 @@ +package hash + +import ( + "crypto/md5" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/hex" + fileSdk "skapp/pkg/sdk/file" +) + +type Support struct { + fileSupport *fileSdk.Support +} + +func New(fileSupport *fileSdk.Support) *Support { + return &Support{fileSupport} +} + +func (s *Support) CalcSha512(path string, readSize int64) (hash string, err error) { + h := sha512.New() + _, err = s.fileSupport.CopyN(h, path, readSize) + if err != nil { + return "", err + } + + return hex.EncodeToString(h.Sum(nil)), nil +} + +func (s *Support) CalcSha256(path string, readSize int64) (hash string, err error) { + h := sha256.New() + _, err = s.fileSupport.CopyN(h, path, readSize) + if err != nil { + return "", err + } + + return hex.EncodeToString(h.Sum(nil)), nil +} + +func (s *Support) CalcSHA1(path string, readSize int64) (hash string, err error) { + h := sha1.New() + _, err = s.fileSupport.CopyN(h, path, readSize) + if err != nil { + return "", err + } + + return hex.EncodeToString(h.Sum(nil)), nil +} + +func (s *Support) CalcMD5(path string, readSize int64) (hash string, err error) { + h := md5.New() + _, err = s.fileSupport.CopyN(h, path, readSize) + if err != nil { + return "", err + } + + return hex.EncodeToString(h.Sum(nil)), nil +} diff --git a/app/wails/pkg/utils/json/json.go b/app/wails/pkg/utils/json/json.go new file mode 100644 index 0000000..e3e7c55 --- /dev/null +++ b/app/wails/pkg/utils/json/json.go @@ -0,0 +1,10 @@ +package json + +import ( + "github.com/goccy/go-json" +) + +func Json(data interface{}) string { + jsonBytes, _ := json.MarshalIndent(data, "", " ") + return string(jsonBytes) +}