-
Notifications
You must be signed in to change notification settings - Fork 30
/
netfs.go
58 lines (51 loc) · 1.29 KB
/
netfs.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
package blink
//#include "netfs.h"
import "C"
import (
"github.com/mushroomsir/mimetypes"
"io/ioutil"
"net/http"
urlLib "net/url"
"path/filepath"
"unsafe"
)
var netFileSystems = make(map[string]http.FileSystem)
func RegisterFileSystem(domain string, fs http.FileSystem) {
netFileSystems[domain] = fs
}
func UnregisterFileSystem(domain string) {
delete(netFileSystems, domain)
}
//export goGetNetFSData
func goGetNetFSData(window C.wkeWebView, url *C.char) (result C.int, mimeType *C.char, data unsafe.Pointer, length C.int) {
//解析url
u, err := urlLib.Parse(C.GoString(url))
if err != nil {
return C.int(-1), nil, nil, C.int(0)
}
//只响应http
if u.Scheme != "http" {
//无需解析
return C.int(1), nil, nil, C.int(0)
}
//响应指定的域名
if fs, isExist := netFileSystems[u.Host]; isExist {
//获取文件
f, err := fs.Open(u.Path)
if err != nil {
return C.int(-1), nil, nil, C.int(0)
}
defer f.Close()
//读取二进制数据
binData, err := ioutil.ReadAll(f)
if err != nil {
return C.int(-1), nil, nil, C.int(0)
}
//获取mimetype
mime := mimetypes.Lookup(filepath.Ext(u.Path))
return C.int(0), C.CString(mime), unsafe.Pointer(C.CString(string(binData))), C.int(len(binData))
} else {
//无需解析
return C.int(1), nil, nil, C.int(0)
}
}