Skip to content

Commit 2727891

Browse files
SFM
1 parent 33dfe8a commit 2727891

11 files changed

+388
-192
lines changed

Struct/file.go

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Struct
22

33
type GetFile struct {
4+
Err string `json:"err"`
45
Number int `json:"Number"`
56
Path string `json:"Path"`
67
Main []struct {
@@ -13,6 +14,7 @@ type GetFile struct {
1314
}
1415

1516
type SearchFile struct {
17+
Err string `json:"err"`
1618
KeyWord string `json:"KeyWord"`
1719
Number int `json:"Number"`
1820
Path string `json:"Path"`
@@ -34,3 +36,32 @@ type SandRemoveFile struct {
3436
} `json:"removename"`
3537
User `json:"user"`
3638
}
39+
40+
type SandSearchFile struct {
41+
Path string `json:"path"`
42+
KeyWord string `json:"keyword"`
43+
Type string `json:"type"`
44+
User `json:"user"`
45+
}
46+
47+
type SandGetFile struct {
48+
Path string `json:"path"`
49+
User `json:"user"`
50+
}
51+
52+
type SandRenameFile struct {
53+
Path string `json:"path"`
54+
Name string `json:"name"`
55+
Rename string `json:"rename"`
56+
User `json:"user"`
57+
}
58+
59+
type SandCopyFile struct {
60+
Path string `json:"path"`
61+
CopyName []struct {
62+
Name string `json:"name"`
63+
IsDir bool `json:"isdir"`
64+
} `json:"copyname"`
65+
ToPath string `json:"topath"`
66+
User `json:"user"`
67+
}

chief/File/CopyFile.go

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package File
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
"xiaowumin-SFM/Struct"
11+
)
12+
13+
func CopyFile(date Struct.SandCopyFile) error {
14+
if date.Path[len(date.Path)-1] != '/' {
15+
date.Path += "/"
16+
}
17+
if date.ToPath[len(date.ToPath)-1] != '/' {
18+
date.ToPath += "/"
19+
}
20+
fmt.Println(date)
21+
for i := 0; i < len(date.CopyName); i++ {
22+
if date.CopyName[i].IsDir {
23+
var content string
24+
if date.ToPath[len(date.ToPath)-1] == '/' {
25+
content = date.ToPath[:len(date.ToPath)-1]
26+
}
27+
fmt.Println("[dir]path:", date.Path+date.CopyName[i].Name, "topath:", content)
28+
err := CopyDir(date.Path+date.CopyName[i].Name, content)
29+
if err != nil {
30+
return err
31+
}
32+
content = ""
33+
} else {
34+
var content string
35+
if date.ToPath[len(date.ToPath)-1] != '/' {
36+
content = date.ToPath + "/"
37+
}
38+
fmt.Println("[file]path:", date.Path+date.CopyName[i].Name, "topath:", content)
39+
err := copyFile(date.Path+date.CopyName[i].Name, content, 0)
40+
if err != nil {
41+
return err
42+
}
43+
content = ""
44+
}
45+
}
46+
return nil
47+
}
48+
49+
// 使用os.Read()和os.Write()
50+
func copyFile(src, des string, bufSize int) (err error) {
51+
if bufSize <= 0 {
52+
bufSize = 1 * 1024 * 1024 //1M
53+
}
54+
buf := make([]byte, bufSize)
55+
56+
srcFile, err := os.Open(src)
57+
if err != nil {
58+
return err
59+
}
60+
defer srcFile.Close()
61+
62+
//获取源文件的权限
63+
fi, _ := srcFile.Stat()
64+
perm := fi.Mode()
65+
66+
desFile, err := os.OpenFile(des, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm)
67+
if err != nil {
68+
return err
69+
}
70+
defer desFile.Close()
71+
72+
count := 0
73+
for {
74+
n, err := srcFile.Read(buf)
75+
if err != nil && err != io.EOF {
76+
return err
77+
}
78+
79+
if n == 0 {
80+
break
81+
}
82+
83+
if wn, err := desFile.Write(buf[:n]); err != nil {
84+
return err
85+
} else {
86+
count += wn
87+
}
88+
}
89+
90+
return nil
91+
}
92+
93+
func CopyDir(srcPath, desPath string) error {
94+
//检查目录是否正确
95+
if srcInfo, err := os.Stat(srcPath); err != nil {
96+
return err
97+
} else {
98+
if !srcInfo.IsDir() {
99+
return errors.New("源路径不是一个正确的目录!")
100+
}
101+
}
102+
103+
if desInfo, err := os.Stat(desPath); err != nil {
104+
return err
105+
} else {
106+
if !desInfo.IsDir() {
107+
return errors.New("目标路径不是一个正确的目录!")
108+
}
109+
}
110+
111+
if strings.TrimSpace(srcPath) == strings.TrimSpace(desPath) {
112+
return errors.New("源路径与目标路径不能相同!")
113+
}
114+
115+
err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
116+
if f == nil {
117+
return err
118+
}
119+
120+
//复制目录是将源目录中的子目录复制到目标路径中,不包含源目录本身
121+
if path == srcPath {
122+
return nil
123+
}
124+
125+
//生成新路径
126+
destNewPath := strings.Replace(path, srcPath, desPath, -1)
127+
128+
if !f.IsDir() {
129+
copyFile(path, destNewPath, 0)
130+
} else {
131+
if !FileIsExisted(destNewPath) {
132+
return MakeDir(destNewPath)
133+
}
134+
}
135+
136+
return nil
137+
})
138+
139+
return err
140+
}
141+
142+
func FileIsExisted(filename string) bool {
143+
existed := true
144+
if _, err := os.Stat(filename); os.IsNotExist(err) {
145+
existed = false
146+
}
147+
return existed
148+
}
149+
150+
func MakeDir(dir string) error {
151+
if !FileIsExisted(dir) {
152+
if err := os.MkdirAll(dir, 0777); err != nil { //os.ModePerm
153+
fmt.Println("MakeDir failed:", err)
154+
return err
155+
}
156+
}
157+
return nil
158+
}

chief/File/GetFile.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package File
22

33
import (
4-
"encoding/json"
4+
//"encoding/json"
55
"fmt"
66
"os"
77
"time"
@@ -10,7 +10,7 @@ import (
1010

1111
var gf = Struct.GetFile{} // 将gf的定义移出函数之外,使其成为全局变量
1212

13-
func GetFile(dirname string) []byte {
13+
func GetFile(dirname string) (*Struct.GetFile, error) {
1414
if dirname[len(dirname)-1] != '/' {
1515
dirname += "/"
1616
}
@@ -21,7 +21,7 @@ func GetFile(dirname string) []byte {
2121
entries, err := os.ReadDir(dirname)
2222
if err != nil {
2323
fmt.Println("读取目录失败:", err)
24-
return nil
24+
return nil, err
2525
}
2626
var GetFilenum int
2727
GetFilenum = 0
@@ -58,13 +58,13 @@ func GetFile(dirname string) []byte {
5858
}
5959
gf.Number = GetFilenum
6060
gf.Path = dirname
61-
jsonData, err := json.Marshal(gf)
62-
if err != nil {
63-
fmt.Println(err)
64-
return nil
65-
}
61+
//jsonData, err := json.Marshal(gf)
62+
//if err != nil {
63+
// fmt.Println(err)
64+
// return nil
65+
//}
6666
//fmt.Println(string(jsonData))
67-
return jsonData
67+
return &gf, nil
6868
}
6969

7070
func formatFileSize(fileSize int64) (size string) {

chief/File/RenameFile.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package File
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"xiaowumin-SFM/Struct"
7+
)
8+
9+
func RenameFile(date Struct.SandRenameFile) error {
10+
if date.Path[len(date.Path)-1] != '/' {
11+
date.Path += "/"
12+
}
13+
fmt.Println(date)
14+
err := os.Rename(date.Path+date.Name, date.Path+date.Rename)
15+
if err != nil {
16+
return err
17+
}
18+
return nil
19+
}

chief/File/SearchFile.go

+8-23
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
package File
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
76
"path/filepath"
87
"strings"
98
"time"
109
"xiaowumin-SFM/Struct"
1110
"xiaowumin-SFM/chief"
12-
"xiaowumin-SFM/chief/ToJson"
1311
)
1412

15-
func SearchFile(path string, name string, all string) []byte {
13+
func SearchFile(path string, name string, all string) (*Struct.SearchFile, error) {
1614
var sf = Struct.SearchFile{}
17-
var jsonData []byte
15+
1816
if path[len(path)-1] != '/' {
1917
path += "/"
2018
}
2119
if all == "AtPresent" {
2220
entries, err := os.ReadDir(path)
2321
if err != nil {
2422
fmt.Println("读取目录失败:", err)
25-
23+
return nil, err
2624
}
2725
var SearchFilenum int
2826
SearchFilenum = 0
@@ -35,7 +33,7 @@ func SearchFile(path string, name string, all string) []byte {
3533
fileInfo, err := entry.Info()
3634
if err != nil {
3735
fmt.Println("无法获取文件信息:", err)
38-
36+
return nil, err
3937
}
4038

4139
modTime := fileInfo.ModTime()
@@ -68,19 +66,15 @@ func SearchFile(path string, name string, all string) []byte {
6866
sf.Path = path
6967
sf.Type = all
7068
sf.Number = SearchFilenum
71-
jsonData, err = json.Marshal(sf)
72-
if err != nil {
73-
fmt.Println(err)
7469

75-
}
7670
//fmt.Println(string(jsonData))
7771

7872
} else if all == "All" {
7973
if path == "" {
80-
decodedPerson, err := ToJson.ConfJson(chief.Config())
74+
decodedPerson, err := chief.Config()
8175
if err != nil {
8276
fmt.Println("解码错误:", err)
83-
77+
return nil, err
8478
}
8579
var SearchFilenum int
8680
SearchFilenum = 0
@@ -138,12 +132,6 @@ func SearchFile(path string, name string, all string) []byte {
138132
sf.Path = path
139133
sf.Type = all
140134
sf.Number = SearchFilenum
141-
jsonData, err = json.Marshal(sf)
142-
if err != nil {
143-
fmt.Println(err)
144-
145-
}
146-
//fmt.Println(string(jsonData))
147135
} else {
148136

149137
root := path // 替换为你要遍历的目录的路径
@@ -191,20 +179,17 @@ func SearchFile(path string, name string, all string) []byte {
191179
})
192180
if err != nil {
193181
fmt.Println(err)
182+
return nil, err
194183
}
195184

196185
sf.KeyWord = name
197186
sf.Path = path
198187
sf.Type = all
199188
sf.Number = SearchFilenum
200-
jsonData, err = json.Marshal(sf)
201-
if err != nil {
202-
fmt.Println(err)
203189

204-
}
205190
//fmt.Println(string(jsonData))
206191
}
207192

208193
}
209-
return jsonData
194+
return &sf, nil
210195
}

0 commit comments

Comments
 (0)