/
path.go
166 lines (147 loc) · 3.83 KB
/
path.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package file
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/stratosnet/sds/pp/setting"
"github.com/stratosnet/sds/utils"
)
// getTmpFolderPath path to the tmp file folder
func getTmpFolderPath() string {
return filepath.Join(setting.GetRootPath(), TEMP_FOLDER)
}
// GetTmpFileFolderPath path to tmp file folder for specific file
func GetTmpFileFolderPath(fileHash string) string {
return filepath.Join(getTmpFolderPath(), fileHash)
}
// GetTmpDownloadPath path to the download tmp file folder
func GetTmpDownloadPath() string {
return filepath.Join(getTmpFolderPath(), "download")
}
func getDownloadTmpFolderPath(fileHash string) string {
return filepath.Join(GetTmpDownloadPath(), fileHash)
}
// GetDownloadTmpFilePath path to the download tmp file
func GetDownloadTmpFilePath(fileHash, fileName string) string {
return filepath.Join(getDownloadTmpFolderPath(fileHash), fileName+".tmp")
}
// GetDownloadTmpCsvPath get download CSV path
func GetDownloadTmpCsvPath(fileHash, fileName string) string {
return filepath.Join(getDownloadTmpFolderPath(fileHash), fileName+".csv")
}
// GetDownloadFileNameFromTmp fetch the first name in download tmp folder with the filehash, and generate the file name
func GetDownloadFileNameFromTmp(fileHash string) (string, error) {
files, err := os.ReadDir(getDownloadTmpFolderPath(fileHash))
if err != nil {
return "", errors.Wrap(err, "can't get download file name, ")
}
for _, file := range files {
fileName := file.Name()
if len(fileName) > 4 && fileName[len(fileName)-4:] == ".tmp" {
return fileName[:len(fileName)-4], nil
}
}
return "", errors.New("can't find cached files")
}
// GetDownloadFilePath path to a file as in download folder
func GetDownloadFilePath(fileName, savePath string) string {
return filepath.Join(setting.Config.Home.DownloadPath, savePath, fileName)
}
func getSlicePath(hash string) (string, error) {
if len(hash) < 10 {
return "", errors.New("wrong size of slice hash")
}
s1 := string([]rune(hash)[:8])
s2 := string([]rune(hash)[8:10])
path := filepath.Join(setting.Config.Home.StoragePath, s1, s2)
exist, err := PathExists(path)
if err != nil {
return "", errors.Wrap(err, "failed checking path")
}
if !exist {
if err = os.MkdirAll(path, os.ModePerm); err != nil {
return "", errors.Wrap(err, "failed creating dir")
}
}
return filepath.Join(path, hash), nil
}
// pathExists
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// IsFile checks if the path is a file or directory
func IsFile(f string) (bool, error) {
fi, e := os.Stat(f)
if e != nil {
return false, fmt.Errorf("IsFile: error open path %v ", e)
}
return !fi.IsDir(), nil
}
// EscapePath
func EscapePath(param []string) string {
operatingSystem := runtime.GOOS
newStr := ""
if operatingSystem == "linux" || operatingSystem == "darwin" {
for i := 0; i < len(param); i++ {
str := param[i]
if str != "" {
if str[len(str)-1:] == `\` {
str = str[0 : len(str)-1]
}
newStr += str
if i != len(param)-1 {
newStr += " "
}
} else {
newStr += " "
}
}
newStr = strings.Replace(newStr, `\`, "", -1)
} else {
// Windows
for i := 0; i < len(param); i++ {
str := param[i]
newStr += str
newStr += " "
}
}
for {
if len(newStr) == 0 {
return ""
}
if newStr[len(newStr)-1:] == " " {
newStr = newStr[0 : len(newStr)-1]
} else {
break
}
}
for {
if len(newStr) == 0 {
return ""
}
if newStr[:1] == " " {
newStr = newStr[1:]
} else {
break
}
}
utils.DebugLog("newStr", newStr)
if newStr[:1] == `"` {
newStr = newStr[1:]
}
if newStr[len(newStr)-1:] == `"` {
newStr = newStr[:len(newStr)-1]
}
utils.DebugLog("path ====== ", newStr)
return newStr
}