-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemp.go
90 lines (83 loc) · 1.58 KB
/
temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"io"
"log"
"os"
"path/filepath"
"strings"
)
var TempDirPath string
func destroyTemp(path string) error {
if strings.Contains(path, "temp") {
}
//err := filepath.WalkDir(path, func(path string, fi os.DirEntry, err error) error {
err := filepath.Walk(path, func(path string, fi os.FileInfo, err error) error {
if nil == fi {
return err
}
if !fi.IsDir() {
err := os.Remove(path)
if err != nil {
return err
}
return nil
}
err = os.RemoveAll(path)
if err != nil {
return err
}
return nil
})
return err
}
func DestroyTempDir() {
err := os.RemoveAll(TempDirPath)
if err != nil {
log.Println("删除缓存目录错误:", err)
}
}
func CreateTmpDir() (string, error) {
file, err := os.MkdirTemp(os.TempDir(), "too-gin")
if err != nil {
return file, err
}
TempDirPath = file
return file, err
}
func CreateTmpFiles(name string) {
tempDir, err := CreateTmpDir()
if err != nil {
return
}
dir, err := local.ReadDir(name)
if err != nil {
return
}
tempDir = tempDir + string(filepath.Separator)
for _, fileInfo := range dir {
fileName := fileInfo.Name()
_, err := os.Stat(tempDir + fileName)
if err == nil || os.IsExist(err) { // 如果文件存在
_ = os.Remove(name)
}
file, err := local.Open(name + "/" + fileName)
if err != nil {
continue
}
bytes, err := io.ReadAll(file)
if err != nil {
continue
}
tempFile, err := os.Create(tempDir + fileName)
if err == nil {
_, err := tempFile.Write(bytes)
if err != nil {
return
}
}
err = tempFile.Close()
if err != nil {
return
}
}
}