|
@@ -5,11 +5,13 @@ import (
|
|
|
"errors"
|
|
|
"eta/eta_mini_crm_ht/utils"
|
|
|
"fmt"
|
|
|
- "time"
|
|
|
-
|
|
|
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
-
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
|
|
|
+ "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
+ "github.com/gabriel-vasile/mimetype"
|
|
|
+ "io"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type STSToken struct {
|
|
@@ -26,8 +28,8 @@ type STSToken struct {
|
|
|
type OssClient interface {
|
|
|
UploadFile(string, string, string) (string, error)
|
|
|
GetUploadToken() (OssToken, error)
|
|
|
- GetFile(filePath, savePath string) (err error)
|
|
|
-
|
|
|
+ GetFile(filePath, savePath string) (path string, err error)
|
|
|
+ GetExt(string) (string, error)
|
|
|
MultiUploadFile() error
|
|
|
}
|
|
|
|
|
@@ -163,6 +165,27 @@ func NewSTSToken() (item *STSToken, err error) {
|
|
|
|
|
|
type AliOss struct{}
|
|
|
|
|
|
+func (m *AliOss) GetExt(ossPath string) (ext string, err error) {
|
|
|
+ if utils.AccessKeyId == `` {
|
|
|
+ return "0", errors.New("阿里云信息未配置")
|
|
|
+ }
|
|
|
+ client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bucket, err := client.Bucket(utils.Bucketname)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ objectKey := ossPath[strings.Index(ossPath, utils.RESOURCE_DIR):]
|
|
|
+ metaInfo, err := bucket.GetObjectMeta(objectKey)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ext = GetFileExtensionFromContentType(metaInfo["Content-Type"][0])
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// UploadFile 上传文件
|
|
|
func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
|
|
|
if utils.AccessKeyId == `` {
|
|
@@ -208,6 +231,71 @@ func (m *AliOss) GetUploadToken() (token OssToken, err error) {
|
|
|
func (m *AliOss) MultiUploadFile() (err error) {
|
|
|
return
|
|
|
}
|
|
|
-func (m *AliOss) GetFile(filePath, savePath string) (err error) {
|
|
|
+func (m *AliOss) GetFile(filePath, savePath string) (path string, err error) {
|
|
|
+ if utils.AccessKeyId == `` {
|
|
|
+ return "", errors.New("阿里云信息未配置")
|
|
|
+ }
|
|
|
+ client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bucket, err := client.Bucket(utils.Bucketname)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ objectKey := filePath[strings.Index(filePath, utils.RESOURCE_DIR):]
|
|
|
+ reader, err := bucket.GetObject(objectKey)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ buffer := make([]byte, 512)
|
|
|
+ n, err := reader.Read(buffer)
|
|
|
+ if err != nil && err != io.EOF {
|
|
|
+ return "", fmt.Errorf("Error reading object: %s", err)
|
|
|
+ }
|
|
|
+ mime := mimetype.Detect(buffer[:n])
|
|
|
+ fileExt := mime.Extension()
|
|
|
+ path = savePath + fileExt
|
|
|
+ defer reader.Close()
|
|
|
+ // 打开或创建本地文件
|
|
|
+ localFile, err := os.Create(path)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("处理原始图片信息失败:Error create local dir: %s", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer localFile.Close()
|
|
|
+ // 将io.ReadCloser中的数据复制到本地文件
|
|
|
+ _, err = localFile.Write(buffer[:n])
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("处理原始图片信息失败:Error writing to local file: %s", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ _, err = io.Copy(localFile, reader)
|
|
|
return
|
|
|
}
|
|
|
+func GetFileExtensionFromContentType(contentType string) string {
|
|
|
+ switch contentType {
|
|
|
+ case "image/jpeg":
|
|
|
+ return ".jpg"
|
|
|
+ case "image/png":
|
|
|
+ return ".png"
|
|
|
+ case "application/pdf":
|
|
|
+ return ".pdf"
|
|
|
+ case "application/msword":
|
|
|
+ return ".doc"
|
|
|
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
|
|
+ return ".docx"
|
|
|
+ case "application/vnd.ms-excel":
|
|
|
+ return ".xls"
|
|
|
+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
|
|
+ return ".xlsx"
|
|
|
+ case "text/plain":
|
|
|
+ return ".txt"
|
|
|
+ case "application/json":
|
|
|
+ return ".json"
|
|
|
+ case "application/octet-stream":
|
|
|
+ return ".bin"
|
|
|
+ default:
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+}
|