You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.5 KiB
Go

10 months ago
package logic
import (
"chain-dci/pb/dci"
"chain-dci/pkg/app"
errCommon "chain-dci/pkg/err"
10 months ago
"chain-dci/pkg/msg"
10 months ago
"chain-dci/pkg/utils"
"encoding/base64"
9 months ago
"errors"
10 months ago
"fmt"
10 months ago
bccrClient "github.com/antchain-openapi-sdk-go/bccr/client"
"github.com/jinzhu/copier"
"strings"
10 months ago
"time"
10 months ago
"unicode"
)
type IFile interface {
Upload(request *dci.GetUploadUrlRequest) error
}
type File struct {
}
/*
API
GetUploadUrl
访 oss, 使 utf-8 URL
*/
func GetUploadUrl(req *bccrClient.GetUploadurlRequest) (result *bccrClient.GetUploadurlResponse) {
result, err := app.ModuleClients.BccrClient.GetUploadurl(req)
if err != nil {
errCommon.NoReturnError(err, "调用 蚂蚁链获取 oss url 错误:")
return
}
8 months ago
errCommon.NoReturnInfo(req, "获取 DCI GetUploadurl 返回结果:")
10 months ago
return
}
/*
API
Upload
*/
func (f *File) Upload(req *dci.GetUploadUrlRequest) (res *dci.GetUploadUrlResponse, err error) {
9 months ago
fmt.Println("+++++++++++++++ GetUploadUrlRequest =================")
9 months ago
fmt.Printf("GetUploadUrlRequest is : %+v\n", req)
9 months ago
fmt.Println("+++++++++++++++ GetUploadUrlRequest =================")
8 months ago
errCommon.NoReturnInfo(req, "上传文件 参数记录:")
/*// 记录 上传的文件
//nil := app.ModuleClients.DciDB.Begin()
fileInfo := new(model.FileInfo)
fileInfo.FileUrl = req.FileName
_ = dao.CreateFileInfo(nil, fileInfo)*/
10 months ago
isChinese := false
getUploadUrlRequest := new(bccrClient.GetUploadurlRequest)
var fileName string
// 拆分 文件名 和 文件链接
fileArr := strings.Split(req.FileName, "/")
10 months ago
fileName = fileArr[len(fileArr)-1]
9 months ago
fmt.Println("+++++++++++++++ no encode fileName =================")
10 months ago
fmt.Println("no encode fileName is :", fileArr[len(fileArr)-1])
9 months ago
fmt.Println("+++++++++++++++ no encode fileName =================")
10 months ago
// 如果文件名包含 中文 则需要对 fileName 进行 utf-8 字符集进行 URL编码
for _, c := range fileName {
if unicode.Is(unicode.Scripts["Han"], c) {
isChinese = true
}
}
if isChinese {
10 months ago
fileName = base64.URLEncoding.EncodeToString([]byte(fileName))
10 months ago
}
10 months ago
getUploadUrlRequest.SetFileName(fileName)
10 months ago
10 months ago
clientToken, err := createToken(time.Now().UnixMilli(), fileName, app.ModuleClients.SfNode.Generate().Base64())
10 months ago
if err != nil {
return nil, errCommon.ReturnError(err, msg.ErrCreateClientToken, "创建clientToken 失败: ")
}
10 months ago
getUploadUrlRequest.SetClientToken(clientToken)
9 months ago
fmt.Println("+++++++++++++++ encode fileName =================")
10 months ago
fmt.Println("encode fileName is :", fileName)
9 months ago
fmt.Println("+++++++++++++++ encode fileName =================")
10 months ago
10 months ago
getUploadUrlResponse := GetUploadUrl(getUploadUrlRequest)
res = new(dci.GetUploadUrlResponse)
8 months ago
_ = copier.CopyWithOption(&res, getUploadUrlResponse, copier.Option{DeepCopy: false})
10 months ago
9 months ago
fmt.Println("+++++++++++++++ GetUploadUrl =================")
fmt.Printf("GetUploadUrl is : %+v\n", res)
fmt.Println("+++++++++++++++ GetUploadUrl =================")
9 months ago
fmt.Println("====== =========== =================== 1 ===============")
9 months ago
if res.ResultCode != "OK" {
8 months ago
/* fileInfo.ReqMsgId = res.ReqMsgId
fileInfo.ResultCode = res.ResultCode
fileInfo.ResultMsg = res.ResultMsg
fileInfo.Url = res.Url
fileInfo.FileId = res.FileId
err = dao.UpdateFileInfo(nil, fileInfo)*/
9 months ago
errCommon.NoReturnError(errors.New(res.ResultMsg), "获取授权访问OSS链接 错误:")
return res, nil
9 months ago
}
9 months ago
fmt.Println("====== =========== =================== 2 ===============")
9 months ago
if res.Url == "" || res.FileId == "" {
8 months ago
/*fileInfo.ReqMsgId = res.ReqMsgId
fileInfo.ResultCode = res.ResultCode
fileInfo.ResultMsg = res.ResultMsg
fileInfo.Url = res.Url
fileInfo.FileId = res.FileId
_ = dao.UpdateFileInfo(nil, fileInfo)*/
9 months ago
errCommon.NoReturnError(errors.New(res.ResultMsg), "获取授权访问OSS链接 错误:")
return res, nil
10 months ago
}
9 months ago
fmt.Println("====== =========== =================== 3 ===============")
9 months ago
code, result := utils.PutFromFileUrlWithStream(res.Url, fileName, req.FileName)
10 months ago
if code != 200 {
8 months ago
err = errCommon.ReturnError(errors.New(result), result, "上传文件 错误:")
//fileInfo.IsUpload = 3 // 上传失败
} else {
errCommon.NoReturnInfo(result, "上传文件 最终结果:")
//fileInfo.IsUpload = 2 // 上传成功
10 months ago
}
8 months ago
/*fileInfo.ReqMsgId = res.ReqMsgId
fileInfo.ResultCode = res.ResultCode
fileInfo.ResultMsg = res.ResultMsg
fileInfo.Url = res.Url
fileInfo.FileId = res.FileId
_ = dao.UpdateFileInfo(nil, fileInfo)*/
return res, err
10 months ago
}