添加 file hash duplicate 支持

This commit is contained in:
Shikong 2023-07-24 10:40:12 +08:00
parent ac62099b73
commit 9eb5d2bd96
12 changed files with 313 additions and 2 deletions

View File

@ -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<string>;
export function CalcSHA1(arg1:string):Promise<string>;
export function RecursiveScan(arg1:string,arg2:boolean,arg3:boolean):Promise<Array<string>>;

View File

@ -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);
}

View File

@ -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<number>;
export function IsDir(arg1:string):Promise<boolean>;
export function IsFile(arg1:string):Promise<boolean>;
export function SaveJSONFile(arg1:string,arg2:any):Promise<void>;

View File

@ -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);
}

View File

@ -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<string>;
export function CalcSHA1(arg1:string,arg2:number):Promise<string>;

View File

@ -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);
}

View File

@ -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

View File

@ -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,

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1,10 @@
package json
import (
"github.com/goccy/go-json"
)
func Json(data interface{}) string {
jsonBytes, _ := json.MarshalIndent(data, "", " ")
return string(jsonBytes)
}