-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
148 lines (140 loc) · 4.26 KB
/
utils.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
// 公共方法实现
package web
import (
"../en"
"fmt"
_ "github.com/eventials/go-tus"
slog "github.com/sjqzhang/seelog"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"strconv"
"strings"
)
// 获取服务状态信息 [处理状态信息 state.json]
func (hs *HttpServer) getStat() []en.StatDateFileInfo {
var (
err error
min int64
max int64
rows []en.StatDateFileInfo
total en.StatDateFileInfo
)
min = 20190101
max = 20190101
var i int64
for k := range hs.s.GetStatMap().Get() {
ks := strings.Split(k, "_")
if len(ks) == 2 {
if i, err = strconv.ParseInt(ks[0], 10, 64); err != nil {
continue
}
if i >= max {
max = i
}
if i < min {
min = i
}
}
}
for i := min; i <= max; i++ {
s := fmt.Sprintf("%d", i)
if v, ok := hs.s.GetStatMap().GetValue(s + "_" + CONST_STAT_FILE_TOTAL_SIZE_KEY); ok {
var info en.StatDateFileInfo
info.Date = s
switch v.(type) {
case int64:
info.TotalSize = v.(int64)
total.TotalSize = total.TotalSize + v.(int64)
}
if v, ok := hs.s.GetStatMap().GetValue(s + "_" + CONST_STAT_FILE_COUNT_KEY); ok {
switch v.(type) {
case int64:
info.FileCount = v.(int64)
total.FileCount = total.FileCount + v.(int64)
}
}
rows = append(rows, info)
}
}
total.Date = "all"
rows = append(rows, total)
return rows
}
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// 跨域访问配置 [https://blog.csdn.net/yanzisu_congcong/article/details/80552155]
func CrossOrigin(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Expose-Headers", "Authorization")
}
// 下载文件, 设置下载响应 Header
func SetDownloadHeader(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment")
}
// HTTP响应错误信息, 必须使用正确的节点IP地址访问资源
func GetClusterNotPermitMessage(r *http.Request) string {
var (
message string
)
message = fmt.Sprintf("Can only be called by the cluster ip or 127.0.0.1 or admin_ips(cfg.json),current ip:%s", util.GetClientIp(r))
return message
}
// 根据请求IP, 判断访问是否是 集权节点 (发起请求客户端, 必须是集权中的节点)
func IsPeer(r *http.Request) bool {
// 判断是否是本地节点
ip := util.GetClientIp(r)
realIp := os.Getenv(GO_FASTDFS_IP)
fmt.Printf("访问者IP => %s , 环境配置的真实IP => %s", ip, realIp)
fmt.Printf("访问者HOST => %s", r.Host)
if realIp == "" {
realIp = util.GetPulicIP()
}
if strings.Contains(r.Host, "localhost") || ip == "127.0.0.1" || ip == realIp {
return true
}
// 判断访问者是否为管理主机 IP
if util.Contains(ip, adminIps) {
return true
}
// 判断是否是非本地集群节点 (Peers 手动配置的及集群节点列表)
ip = "http://" + ip
flag := false
for _, peer := range peers {
if strings.HasPrefix(peer, ip) {
flag = true
break
}
}
return flag
}
// 请求数据中, 获取文件(下载/保存)路径(目录) [根据请求URL. 分析文件路径完整路径]
func analyseFilePathFromRequest(w http.ResponseWriter, r *http.Request) (string, string) {
var (
err error
smallPath string
prefix string
)
fullPath := r.RequestURI[1:]
// 分组名(下载路径跟路径), 去除分组信息, 替换为本地存储目录
if strings.HasPrefix(r.RequestURI, "/"+group+"/") {
fullPath = r.RequestURI[len(group)+2 : len(r.RequestURI)]
}
fullPath = strings.Split(fullPath, "?")[0] // just path
fullPath = DOCKER_DIR + STORE_DIR_NAME + "/" + fullPath
prefix = "/" + LARGE_DIR_NAME + "/"
if supportGroupManage {
prefix = "/" + group + "/" + LARGE_DIR_NAME + "/"
}
if strings.HasPrefix(r.RequestURI, prefix) {
smallPath = fullPath //notice order
fullPath = strings.Split(fullPath, ",")[0]
}
if fullPath, err = url.PathUnescape(fullPath); err != nil {
_ = slog.Error(err)
}
return fullPath, smallPath
}