-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.go
90 lines (85 loc) · 2.24 KB
/
base.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 static
import "strings"
type Config struct {
Endpoint string
KeyId string
Secret string
BucketName string
BaseUrl string
Type string
}
// 上传文件到云存储
func UploadFile(config *Config, fileUrl string) error {
localFileUrl, cloudFileUrl := InitFileUrl(fileUrl, config)
if config.Type == "Local" {
st := Local{
BaseUrl: config.BaseUrl,
}
return st.UploadFile(cloudFileUrl)
} else if config.Type == "AliOSS" {
st := AliOSS{
Endpoint: config.Endpoint,
AccessKeyId: config.KeyId,
AccessKeySecret: config.Secret,
BucketName: config.BucketName,
BaseUrl: config.BaseUrl,
}
return st.UploadFile(cloudFileUrl, localFileUrl)
} else if config.Type == "TencentCOS" {
st := TencentCOS{
BucketURL: config.Endpoint,
SecretId: config.KeyId,
SecretKey: config.Secret,
BaseUrl: config.BaseUrl,
}
return st.UploadFile(cloudFileUrl, localFileUrl)
}
return nil
}
// 上传文件到云存储
func RemoveFile(config *Config, fileUrl string, removeLocalFile bool) error {
localFileUrl, cloudFileUrl := InitFileUrl(fileUrl, config)
if config.Type == "Local" {
st := Local{
BaseUrl: config.BaseUrl,
}
return st.RemoveFile(localFileUrl)
} else if config.Type == "AliOSS" {
st := AliOSS{
Endpoint: config.Endpoint,
AccessKeyId: config.KeyId,
AccessKeySecret: config.Secret,
BucketName: config.BucketName,
BaseUrl: config.BaseUrl,
}
return st.RemoveFile(cloudFileUrl, localFileUrl, true)
} else if config.Type == "TencentCOS" {
st := TencentCOS{
BucketURL: config.Endpoint,
SecretId: config.KeyId,
SecretKey: config.Secret,
BaseUrl: config.BaseUrl,
}
return st.RemoveFile(cloudFileUrl, localFileUrl, true)
}
return nil
}
// 规划图片文件路径
func InitFileUrl(fileUrl string, config *Config) (string, string) {
localUrl := ""
cloudUrl := ""
if fileUrl[0:2] == "./" {
localUrl = fileUrl
cloudUrl = fileUrl[2:]
} else if fileUrl[0:1] == "/" {
localUrl = "." + fileUrl
cloudUrl = fileUrl[1:]
} else if fileUrl[0:4] == "http" {
cloudUrl = strings.ReplaceAll(fileUrl, config.BaseUrl, "")
localUrl = "./" + cloudUrl
} else {
localUrl = fileUrl
cloudUrl = fileUrl
}
return localUrl, cloudUrl
}