feat: support storage provider to terms of use file (#221)

Signed-off-by: WindSpiritSR <simon343riley@gmail.com>
This commit is contained in:
WindSpiritSR
2021-08-10 10:43:33 +08:00
committed by Yang Luo
parent 718fc4df74
commit 8c6f0a31b6
9 changed files with 113 additions and 55 deletions

View File

@ -17,13 +17,15 @@ package object
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"strings"
"github.com/casbin/casdoor/storage"
"github.com/casbin/casdoor/util"
)
func UploadFile(provider *Provider, folder string, subFolder string, fileBytes []byte) (string, error) {
func UploadFile(provider *Provider, folder string, subFolder string, file multipart.File, suffix string) (string, error) {
storageProvider := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, provider.Endpoint)
if storageProvider == nil {
return "", fmt.Errorf("the provider type: %s is not supported", provider.Type)
@ -34,8 +36,12 @@ func UploadFile(provider *Provider, folder string, subFolder string, fileBytes [
UpdateProvider(provider.GetId(), provider)
}
path := fmt.Sprintf("%s/%s.png", util.UrlJoin(util.GetUrlPath(provider.Domain), folder), subFolder)
_, err := storageProvider.Put(path, bytes.NewReader(fileBytes))
path := fmt.Sprintf("%s/%s.%s", util.UrlJoin(util.GetUrlPath(provider.Domain), folder), subFolder, suffix)
fileBuf := bytes.NewBuffer(nil)
if _, err := io.Copy(fileBuf, file); err != nil {
return "", err
}
_, err := storageProvider.Put(path, fileBuf)
if err != nil {
return "", err
}