2021-03-14 15:50:36 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-04-27 19:00:34 +08:00
|
|
|
"fmt"
|
2021-04-27 18:11:41 +08:00
|
|
|
|
|
|
|
awss3 "github.com/aws/aws-sdk-go/service/s3"
|
2021-03-14 15:50:36 +08:00
|
|
|
"github.com/qor/oss"
|
|
|
|
"github.com/qor/oss/aliyun"
|
|
|
|
//"github.com/qor/oss/qiniu"
|
|
|
|
"github.com/qor/oss/s3"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var storage oss.StorageInterface
|
2021-04-27 19:00:34 +08:00
|
|
|
var domain string
|
2021-03-14 15:50:36 +08:00
|
|
|
|
|
|
|
func AliyunInit(section *ini.Section) string {
|
2021-04-27 19:00:34 +08:00
|
|
|
endpoint := section.Key("endpoint").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
accessId := section.Key("accessId").String()
|
|
|
|
accessKey := section.Key("accessKey").String()
|
2021-04-27 19:00:34 +08:00
|
|
|
domain = section.Key("domain").String()
|
2021-03-14 15:50:36 +08:00
|
|
|
bucket := section.Key("bucket").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" {
|
2021-03-14 15:50:36 +08:00
|
|
|
return "Config oss.conf wrong"
|
|
|
|
}
|
|
|
|
storage = aliyun.New(&aliyun.Config{
|
2021-04-27 18:11:41 +08:00
|
|
|
AccessID: accessId,
|
2021-03-14 15:50:36 +08:00
|
|
|
AccessKey: accessKey,
|
2021-04-27 18:11:41 +08:00
|
|
|
Bucket: bucket,
|
|
|
|
Endpoint: endpoint,
|
2021-03-14 15:50:36 +08:00
|
|
|
})
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
//func QiniuInit(section *ini.Section) string {
|
2021-04-27 19:00:34 +08:00
|
|
|
// endpoint := section.Key("endpoint").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
// accessId := section.Key("accessId").String()
|
|
|
|
// accessKey := section.Key("accessKey").String()
|
2021-04-27 19:00:34 +08:00
|
|
|
// domain = section.Key("domain").String()
|
2021-03-14 15:50:36 +08:00
|
|
|
// bucket := section.Key("bucket").String()
|
|
|
|
// region := section.Key("region").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
// if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
|
2021-03-14 15:50:36 +08:00
|
|
|
// return "Config oss.conf wrong"
|
|
|
|
// }
|
|
|
|
// storage = qiniu.New(&qiniu.Config{
|
2021-04-27 18:11:41 +08:00
|
|
|
// AccessID: accessId,
|
2021-03-14 15:50:36 +08:00
|
|
|
// AccessKey: accessKey,
|
|
|
|
// Bucket: bucket,
|
|
|
|
// Region: region,
|
|
|
|
// Endpoint: endpoint,
|
|
|
|
// })
|
|
|
|
// return ""
|
|
|
|
//}
|
|
|
|
|
|
|
|
func Awss3Init(section *ini.Section) string {
|
2021-04-27 19:00:34 +08:00
|
|
|
endpoint := section.Key("endpoint").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
accessId := section.Key("accessId").String()
|
|
|
|
accessKey := section.Key("accessKey").String()
|
2021-04-27 19:00:34 +08:00
|
|
|
domain = section.Key("domain").String()
|
2021-03-14 15:50:36 +08:00
|
|
|
bucket := section.Key("bucket").String()
|
|
|
|
region := section.Key("region").String()
|
2021-04-27 18:11:41 +08:00
|
|
|
if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
|
2021-03-14 15:50:36 +08:00
|
|
|
return "Config oss.conf wrong"
|
|
|
|
}
|
|
|
|
storage = s3.New(&s3.Config{
|
2021-04-27 18:11:41 +08:00
|
|
|
AccessID: accessId,
|
2021-03-14 15:50:36 +08:00
|
|
|
AccessKey: accessKey,
|
2021-04-27 18:11:41 +08:00
|
|
|
Region: region,
|
|
|
|
Bucket: bucket,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
ACL: awss3.BucketCannedACLPublicRead,
|
2021-03-14 15:50:36 +08:00
|
|
|
})
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitOssClient() {
|
|
|
|
if storage != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ossConf, err := ini.Load("./conf/oss.conf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
aliyunSection, _ := ossConf.GetSection("aliyun")
|
|
|
|
qiniuSection, _ := ossConf.GetSection("qiniu")
|
|
|
|
awss3Section, _ := ossConf.GetSection("s3")
|
|
|
|
if aliyunSection != nil {
|
|
|
|
AliyunInit(aliyunSection)
|
|
|
|
} else if qiniuSection != nil {
|
|
|
|
//QiniuInit(qiniuSection)
|
|
|
|
} else {
|
|
|
|
Awss3Init(awss3Section)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UploadAvatar(username string, avatar []byte) string {
|
|
|
|
if storage == nil {
|
|
|
|
InitOssClient()
|
|
|
|
if storage == nil {
|
|
|
|
return "oss error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := storage.Put("/casdoor/avatar/" + username + ".png", bytes.NewReader(avatar))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
return "oss error"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAvatarPath() string {
|
2021-04-27 19:00:34 +08:00
|
|
|
return fmt.Sprintf("https://%s/casdoor/avatar/", domain)
|
2021-03-14 15:50:36 +08:00
|
|
|
}
|