2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-08-21 23:17:33 +08:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-03-20 23:21:09 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/proxy"
|
2021-08-21 23:17:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultStorageProvider *Provider = nil
|
|
|
|
|
|
|
|
func InitDefaultStorageProvider() {
|
2022-03-20 23:21:09 +08:00
|
|
|
defaultStorageProviderStr := conf.GetConfigString("defaultStorageProvider")
|
2021-08-21 23:17:33 +08:00
|
|
|
if defaultStorageProviderStr != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
var err error
|
|
|
|
defaultStorageProvider, err = getProvider("admin", defaultStorageProviderStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-21 23:17:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadFile(url string) (*bytes.Buffer, error) {
|
|
|
|
httpClient := proxy.GetHttpClient(url)
|
|
|
|
|
|
|
|
resp, err := httpClient.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
fileBuffer := bytes.NewBuffer(nil)
|
|
|
|
_, err = io.Copy(fileBuffer, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileBuffer, nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getPermanentAvatarUrl(organization string, username string, url string, upload bool) (string, error) {
|
2022-07-03 19:31:12 +08:00
|
|
|
if url == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
return "", nil
|
2022-07-03 19:31:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-21 23:17:33 +08:00
|
|
|
if defaultStorageProvider == nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return "", nil
|
2021-08-21 23:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fullFilePath := fmt.Sprintf("/avatar/%s/%s.png", organization, username)
|
2022-11-27 17:32:15 +08:00
|
|
|
uploadedFileUrl, _ := GetUploadFileUrl(defaultStorageProvider, fullFilePath, false)
|
2021-08-21 23:17:33 +08:00
|
|
|
|
2022-10-20 14:47:08 +08:00
|
|
|
if upload {
|
2023-05-30 15:49:39 +08:00
|
|
|
if err := DownloadAndUpload(url, fullFilePath, "en"); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-20 14:47:08 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return uploadedFileUrl, nil
|
2022-10-20 14:47:08 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func DownloadAndUpload(url string, fullFilePath string, lang string) (err error) {
|
2021-08-21 23:17:33 +08:00
|
|
|
fileBuffer, err := downloadFile(url)
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return
|
2021-08-21 23:17:33 +08:00
|
|
|
}
|
|
|
|
|
2023-03-19 21:47:49 +08:00
|
|
|
_, _, err = UploadFileSafe(defaultStorageProvider, fullFilePath, fileBuffer, lang)
|
2021-08-21 23:17:33 +08:00
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return
|
2021-08-21 23:17:33 +08:00
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
|
|
|
return
|
2021-08-21 23:17:33 +08:00
|
|
|
}
|
2023-04-16 00:29:56 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getPermanentAvatarUrlFromBuffer(organization string, username string, fileBuffer *bytes.Buffer, ext string, upload bool) (string, error) {
|
2023-04-16 00:29:56 +08:00
|
|
|
if defaultStorageProvider == nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return "", nil
|
2023-04-16 00:29:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fullFilePath := fmt.Sprintf("/avatar/%s/%s%s", organization, username, ext)
|
|
|
|
uploadedFileUrl, _ := GetUploadFileUrl(defaultStorageProvider, fullFilePath, false)
|
|
|
|
|
|
|
|
if upload {
|
|
|
|
_, _, err := UploadFileSafe(defaultStorageProvider, fullFilePath, fileBuffer, "en")
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return "", err
|
2023-04-16 00:29:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
return uploadedFileUrl, nil
|
2023-04-16 00:29:56 +08:00
|
|
|
}
|