mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-02 11:20:18 +08:00
feat: support storage provider to terms of use file (#221)
Signed-off-by: WindSpiritSR <simon343riley@gmail.com>
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user