-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_file_path.go
281 lines (257 loc) · 7.31 KB
/
linux_file_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package easylinux
import (
"net/url"
. "github.com/SilentGopherLnx/easygolang"
)
var linux_mount_gvfs string = ""
var linux_mount_gvfs_len int = 0
var hide_file_protocol = true
func init() {
uid, _, _ := GetPC_UserUidLoginName()
linux_mount_gvfs = "/run/user/" + uid + "/gvfs/"
linux_mount_gvfs_len = len(linux_mount_gvfs)
}
type LinuxPath struct {
add_end_slash bool
path_url string // smb://127.0.0.1/demo%20folder/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9.txt
path_real string // /run/user/1000/gvfs/smb-share:server=127.0.0.1,share=demo%20folder/русский.txt
path_visual string // smb://127.0.0.1/demo folder/русский.txt
parse_error bool
}
func NewLinuxPath(add_end_slash bool) *LinuxPath {
return &LinuxPath{add_end_slash: add_end_slash, path_url: "file:///", path_real: "/", path_visual: "file:///"}
}
func (p *LinuxPath) SetUrl(path_url string) {
if p.add_end_slash {
p.path_url = FolderPathEndSlash(path_url)
} else {
p.path_url = path_url
}
p.path_visual = linuxFilePath_Unescape(p.path_url)
p.path_real, p.parse_error = linuxFilePathProtocol_VisualToReal(p.path_visual)
p.parse_error = !p.parse_error
if p.parse_error {
p.path_real = linux_mount_gvfs
}
}
func (p *LinuxPath) SetReal(path_real string) {
if p.add_end_slash {
p.path_real = FolderPathEndSlash(path_real)
} else {
p.path_real = path_real
}
p.path_visual = linuxFilePathProtocol_RealToVisual(p.path_real)
//Prln(linuxFilePath_Escape(p.path_real))
p.path_url = linuxFilePathProtocol_RealToVisual(linuxFilePath_Escape(p.path_real))
p.parse_error = false
}
func (p *LinuxPath) SetVisual(path_visual string) {
p.path_visual = path_visual
if StringFind(p.path_visual, "/") == 1 {
p.path_visual = "file://" + p.path_visual
}
if p.add_end_slash {
p.path_visual = FolderPathEndSlash(p.path_visual)
} else {
p.path_visual = p.path_visual
}
p.path_url = linuxFilePath_Escape(p.path_visual)
p.path_real, p.parse_error = linuxFilePathProtocol_VisualToReal(p.path_visual)
p.parse_error = !p.parse_error
if p.parse_error {
p.path_real = linux_mount_gvfs
}
}
func (p *LinuxPath) GetUrl() string {
return p.path_url
}
func (p *LinuxPath) GetReal() string {
return p.path_real
}
func (p *LinuxPath) GetVisual() string {
if hide_file_protocol {
return removeFileProtocol(p.path_visual)
}
return p.path_visual
}
func (p *LinuxPath) GetParseProblems() bool {
return p.parse_error
}
func (p *LinuxPath) GoUp() {
v := FolderPathEndSlash(p.path_visual)
if StringFind(v, SMB_SLASH2) == 1 {
if StringLength(v) > StringLength(SMB_SLASH2) {
strs := StringSplit(v, "/")
n := len(strs)
if n == 4 {
p.SetVisual(SMB_SLASH2)
return
}
if n == 5 {
p.SetVisual(SMB_SLASH2 + strs[2] + "/")
return
}
} else {
p.SetReal(linux_mount_gvfs)
return
}
}
p.SetReal(LinuxFileGetParent(p.path_real))
}
func (p *LinuxPath) GoDeeper(subfolder string) {
p.parse_error = false
v := FolderPathEndSlash(p.path_visual)
//Prln(v + "||" + subfolder)
v = FolderPathEndSlash(v + subfolder)
p.SetVisual(v)
}
func (p *LinuxPath) GetLastNode() string {
return LinuxFileNameFromPath(p.path_real)
}
func removeFileProtocol(path string) string {
ind := StringFind(path, "file://")
if ind != 1 {
return path
}
return StringPart(path, 8, 0)
}
//what is correct convertation?
func linuxFilePath_Escape(path string) string {
esc := UrlQueryEscape(path)
esc = StringReplace(esc, "+", "%20")
esc = StringReplace(esc, "%2B", "+")
esc = StringReplace(esc, "%3A", ":")
esc = StringReplace(esc, "%2F", "/")
//??
return linuxFilePath_EscapeArgs(esc)
}
func linuxFilePath_EscapeArgs(args string) string {
esc := args
esc = StringReplace(esc, "%2C", ",")
esc = StringReplace(esc, "%3D", "=")
//??
return esc
}
//what is correct convertation?
func linuxFilePath_Unescape(path string) string {
unesc := path
//unesc := UrlQueryUnescape(unesc)
// unesc = StringReplace(unesc, "/", "%2F")
// unesc = StringReplace(unesc, ",", "%2C")
// unesc = StringReplace(unesc, ":", "%3A")
// unesc = StringReplace(unesc, "=", "%3D")
unesc = StringReplace(unesc, "+", "%2B")
unesc = UrlQueryUnescape(unesc)
return unesc
}
func linuxFilePathProtocol_VisualToReal(path_visual string) (string, bool) {
path2 := path_visual
ind := StringFind(path2, "://")
protocol := StringPart(path2, 1, ind-1)
path2 = StringPart(path2, ind+3, 0)
path_arr := StringSplit(path2, "/")
//Prln(protocol)
switch protocol {
case "file":
return path2, true
case "smb":
if len(path_arr) > 2 {
return linux_mount_gvfs + "smb-share:server=" + UrlQueryEscape(path_arr[0]) + ",share=" + UrlQueryEscape(StringDown(path_arr[1])) + "/" + StringJoin(path_arr[2:], "/"), true
}
case "mtp", "gphoto2":
if len(path_arr) > 1 {
return linux_mount_gvfs + protocol + ":host=" + UrlQueryEscape(path_arr[0]) + "/" + StringJoin(path_arr[1:], "/"), true
}
case "dav", "ftp", "davs", "ftps":
pr := protocol
ssl := ""
if StringEnd(protocol, 1) == "s" {
ssl = ",ssl=true"
pr = StringPart(protocol, 1, StringLength(protocol)-1)
}
if len(path_arr) > 1 {
user := ""
host := ""
port := ""
ab := StringSplit(path_arr[0], "@")
if len(ab) == 2 {
host = ab[1]
user = ab[0]
ab = StringSplit(user, ":")
if len(ab) == 2 {
user = ab[0]
port = ab[1]
}
} else {
host = path_arr[0]
ab = StringSplit(host, ":")
if len(ab) == 2 {
host = ab[0]
port = ab[1]
}
}
if len(user) > 0 {
user = ",user=" + UrlQueryEscape(user)
}
if len(port) > 0 {
port = ",port=" + UrlQueryEscape(port)
}
host = UrlQueryEscape(host)
return linux_mount_gvfs + pr + ":host=" + host + ssl + user + port + "/" + StringJoin(path_arr[1:], "/"), true
}
}
return "/", false
}
func linuxFilePathProtocol_RealToVisual(path_real string) string {
path2 := path_real
protocol := ""
protocol_args := ""
var q url.Values
var qerr error = nil
ind := StringFind(path_real, linux_mount_gvfs)
if ind == 1 && len(path2) > linux_mount_gvfs_len {
path2 = StringPart(path_real, linux_mount_gvfs_len+1, 0)
ind2 := StringFind(path2, "/")
protocol_args = StringPart(path2, 1, ind2-1)
ind3 := StringFind(protocol_args, ":")
protocol = StringPart(protocol_args, 1, ind3-1)
protocol_args = StringPart(protocol_args, ind3+1, 0)
path2 = StringPart(path2, ind2+1, 0)
if path2 == "/" {
path2 = ""
}
//q, qerr = UrlQueryParse(StringReplace(StringReplace(protocol_args, ";", "#"), ",", ";"))
q, qerr = UrlQueryParse(StringReplace(protocol_args, ",", "&"))
//Prln(protocol)
//Prln(protocol_args)
if qerr == nil {
switch protocol {
case "smb-share":
servername := StringTrim(q.Get("server"))
foldershare := StringTrim(q.Get("share"))
return SMB_SLASH2 + servername + "/" + foldershare + "/" + path2
case "mtp", "gphoto2":
hostname := StringTrim(q.Get("host"))
return protocol + "://" + linuxFilePath_EscapeArgs(hostname) + "/" + path2
case "dav", "ftp":
hostname := StringTrim(q.Get("host"))
username := StringTrim(q.Get("user"))
port := StringTrim(q.Get("port"))
ssl := ""
if StringFind(protocol_args, ",ssl=true,") > 0 {
ssl = "s"
}
if len(username) > 0 {
username += "@"
}
if len(port) > 0 {
port = ":" + port
}
return protocol + ssl + "://" + username + hostname + port + "/" + path2
}
}
} else {
return "file://" + path2
}
return "file:///"
}