liwei 2 anos atrás
pai
commit
d15d151f98
2 arquivos alterados com 177 adições e 1 exclusões
  1. 176 0
      3dshow-supplier/api/asset.go
  2. 1 1
      3dshow-supplier/app.yaml

+ 176 - 0
3dshow-supplier/api/asset.go

@@ -4,10 +4,16 @@ import (
 	"3dshow-supplier/conf"
 	"3dshow-supplier/db/model"
 	"3dshow-supplier/db/repo"
+	"3dshow-supplier/log"
+	"bytes"
 	"context"
+	"crypto/tls"
 	"errors"
 	"fmt"
 	"image"
+	"io"
+	"io/ioutil"
+	"net/http"
 	"os"
 	"os/exec"
 	"path"
@@ -76,6 +82,45 @@ func AssetsConv(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	oPath := fmt.Sprintf("%s/%s/%s/spin_360", "images", id, groupId)
 
 	// todo下载图片到oPth
+	currAsset := &model.Asset360Fake3d{}
+	ok, _ = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionAssets,
+		Query:       repo.Map{"_id": id},
+	}, currAsset)
+	if !ok {
+		return nil, fmt.Errorf("当前资产不存在")
+	}
+
+	groupPath := ""
+	for _, item := range currAsset.Groups {
+		if item.Id == groupId {
+			groupPath = item.ImagesPath
+			break
+		}
+	}
+	if len(groupPath) < 1 {
+		return nil, fmt.Errorf("当前素材才尚未上传")
+	}
+
+	_getImageIndex := func(i int) string {
+		if i < 10 {
+			return fmt.Sprintf("00_0%d.png", i)
+		}
+
+		return fmt.Sprintf("00_%d.png", i)
+	}
+
+	index := 0
+	for {
+		name := _getImageIndex(index)
+		remoteUrl := fmt.Sprintf("%s/%s", groupPath, name)
+
+		//下载文件
+		err := DownLoadFile(remoteUrl, fmt.Sprintf("%s/%s", oPath, name))
+		if err != nil {
+			return nil, fmt.Errorf("文件下载失败!")
+		}
+	}
 
 	// 获取图片
 	files, _ := os.ReadDir(oPath)
@@ -298,3 +343,134 @@ func CreateObsClient() (*obs.ObsClient, error) {
 	obsConf := conf.AppConfig.Obs
 	return obs.New(obsConf.AccessKeyId, obsConf.SecrateKey, obsConf.Endpoint)
 }
+
+func uploadImage(fpath string, obsDir string) (string, error) {
+	client, err := CreateObsClient()
+	if err != nil {
+		return "", err
+	}
+	obsConf := conf.AppConfig.Obs
+
+	return UploadFile(client, obsConf.Bucket, fpath, obsDir)
+}
+
+func UploadFile(obsClient *obs.ObsClient, bucketName string, fpath string, obsDir string) (string, error) {
+	_, obsname := path.Split(fpath)
+	keyFormat := "%s/%s"
+	if strings.HasSuffix(obsDir, "/") {
+		keyFormat = "%s%s"
+	}
+	obsKey := fmt.Sprintf(keyFormat, obsDir, obsname)
+
+	input := &obs.PutFileInput{}
+	input.Bucket = bucketName
+	input.Key = obsKey
+	input.SourceFile = fpath
+
+	fpathExt := path.Ext(fpath)
+	isGzFile := fpathExt == ".gz"
+	if isGzFile {
+		input.ContentType = "text/plain"
+		input.Metadata = map[string]string{"Content-Encoding": "gzip"}
+	}
+	result, err := obsClient.PutFile(input)
+
+	isUploadOk := true
+
+	if err != nil {
+		log.Errorf("upload obs fail %s", fpath)
+		log.Error(err)
+		isUploadOk = false
+	}
+
+	if result.StatusCode != 200 {
+		isUploadOk = false
+	}
+
+	if !isUploadOk {
+		result, err = obsClient.PutFile(input)
+
+		if err != nil {
+			log.Errorf("upload obs fail2 %s", fpath)
+			log.Error(err)
+			return "", err
+		}
+		if result.StatusCode != 200 {
+			return "", fmt.Errorf("上传失败!")
+		}
+	}
+
+	if isGzFile {
+		metaRet, err := obsClient.SetObjectMetadata(&obs.SetObjectMetadataInput{
+			Bucket:          bucketName,
+			Key:             obsKey,
+			ContentEncoding: "gzip",
+			ContentType:     "text/plain",
+		})
+		fmt.Println(metaRet, err)
+
+		if err != nil {
+			metaRet, err = obsClient.SetObjectMetadata(&obs.SetObjectMetadataInput{
+				Bucket:          bucketName,
+				Key:             obsKey,
+				ContentEncoding: "gzip",
+				ContentType:     "text/plain",
+			})
+			fmt.Println(metaRet, err)
+		}
+	}
+	obsConf := conf.AppConfig.Obs
+
+	return fmt.Sprintf("//%s.%s/%s", bucketName, obsConf.Endpoint, obsKey), nil
+}
+
+func DownLoadFile(src string, target string) error {
+	out, err := os.Create(target)
+	if err != nil {
+		return err
+	}
+
+	if strings.HasPrefix(src, "//") {
+		src = fmt.Sprintf("http:%s", src)
+	}
+
+	timeout := time.Duration(180 * time.Second)
+	client := &http.Client{
+		Timeout: timeout,
+		Transport: &http.Transport{
+			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+		},
+	}
+	resp, err := client.Get(src)
+	if err != nil {
+		return err
+	}
+	defer resp.Body.Close()
+
+	pix, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return err
+	}
+	_, err = io.Copy(out, bytes.NewReader(pix))
+	if err != nil {
+		return err
+	}
+	out.Close()
+
+	return nil
+}
+
+func isExist(path string) bool {
+	_, err := os.Stat(path)
+	if err != nil {
+		if os.IsExist(err) {
+			return true
+		}
+		if os.IsNotExist(err) {
+			return false
+		}
+		fmt.Println(err)
+		return false
+	}
+	return true
+}

+ 1 - 1
3dshow-supplier/app.yaml

@@ -27,4 +27,4 @@ obs:
   bucket: show3d
   accessKeyId: "RA1D7CFVCVKUFX1FHHPB"
   secrateKey: "cDrR2NgAF2KaA4MRkcK19r93P3P6hLKOPhHvPu9p"
-  endpoint: "obs.cn-east-3.myhuaweicloud.com"
+  endpoint: "show3d.cn-east-3.myhuaweicloud.com"