casdoor/object/oss.go
Kininaru 1908f528c8 feat: added avatar tailoring and uploading
Signed-off-by: Kininaru <shiftregister233@outlook.com>

fixed type errors

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fixed the wrong folder

Signed-off-by: Kininaru <shiftregister233@outlook.com>

rewrite login check logic, added unix time to avatar url

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fixed a bug about strconv

Signed-off-by: Kininaru <shiftregister233@outlook.com>

supported oss

Signed-off-by: Kininaru <shiftregister233@outlook.com>

disabled oss provide qiniu

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Fixed avatar url error

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Fixed avatar length bug

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Fixed avatar length bug

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fixed oss.conf

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Put uploading avatar into UserEditPage

Signed-off-by: Kininaru <shiftregister233@outlook.com>

removed avatar dir

Signed-off-by: Kininaru <shiftregister233@outlook.com>

removed avatar in main.go

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Made CropperDiv a reusable component, and updated README for OSS config

Signed-off-by: Kininaru <shiftregister233@outlook.com>

Convert ts to js

Signed-off-by: Kininaru <shiftregister233@outlook.com>

removed ts

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fix: set avatar link to string 255

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fix: updated yarn lock

Signed-off-by: Kininaru <shiftregister233@outlook.com>

add: Casbin license

Signed-off-by: Kininaru <shiftregister233@outlook.com>
2021-03-17 13:19:23 +08:00

124 lines
3.3 KiB
Go

// 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"
"github.com/qor/oss"
"github.com/qor/oss/aliyun"
awss3 "github.com/aws/aws-sdk-go/service/s3"
//"github.com/qor/oss/qiniu"
"github.com/qor/oss/s3"
"gopkg.in/ini.v1"
)
var storage oss.StorageInterface
func AliyunInit(section *ini.Section) string {
accessID := section.Key("accessid").String()
accessKey := section.Key("accesskey").String()
bucket := section.Key("bucket").String()
endpoint := section.Key("endpoint").String()
if accessID == "" || accessKey == "" || bucket == "" || endpoint == "" {
return "Config oss.conf wrong"
}
storage = aliyun.New(&aliyun.Config{
AccessID: accessID,
AccessKey: accessKey,
Bucket: bucket,
Endpoint: endpoint,
})
return ""
}
//func QiniuInit(section *ini.Section) string {
// accessID := section.Key("accessid").String()
// accessKey := section.Key("accesskey").String()
// bucket := section.Key("bucket").String()
// region := section.Key("region").String()
// endpoint := section.Key("endpoint").String()
// if accessID == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
// return "Config oss.conf wrong"
// }
// storage = qiniu.New(&qiniu.Config{
// AccessID: accessID,
// AccessKey: accessKey,
// Bucket: bucket,
// Region: region,
// Endpoint: endpoint,
// })
// return ""
//}
func Awss3Init(section *ini.Section) string {
accessID := section.Key("accessid").String()
accessKey := section.Key("accesskey").String()
bucket := section.Key("bucket").String()
region := section.Key("region").String()
endpoint := section.Key("endpoint").String()
if accessID == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
return "Config oss.conf wrong"
}
storage = s3.New(&s3.Config{
AccessID: accessID,
AccessKey: accessKey,
Region: region,
Bucket: bucket,
Endpoint: endpoint,
ACL: awss3.BucketCannedACLPublicRead,
})
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 {
return "https://" + storage.GetEndpoint() + "/casdoor/avatar/"
}