Jelajahi Sumber

添加 创建图片

wangbo 1 Minggu lalu
induk
melakukan
082a6cdd32
2 mengubah file dengan 68 tambahan dan 0 penghapusan
  1. 8 0
      .idea/.gitignore
  2. 60 0
      osm_sdk.go

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 60 - 0
osm_sdk.go

@@ -1,11 +1,14 @@
 package osm_sdk
 
 import (
+	"bytes"
 	"encoding/json"
 	"git.sxidc.com/go-framework/baize/framework/core/api/response"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"git.sxidc.com/service-supports/dapr_api/invoke"
 	"github.com/pkg/errors"
+	"io"
+	"mime/multipart"
 	"time"
 )
 
@@ -503,6 +506,63 @@ func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
 
 }
 
+func CreateImage(reader io.Reader, objName string) (string, error) {
+	return createImage(reader, objName)
+}
+
+func CreateImageWithPrefix(reader io.Reader, objName string) (string, error) {
+	return createImage(reader, prefix+objName)
+}
+
+func createImage(reader io.Reader, objName string) (string, error) {
+	if strutils.IsStringEmpty(objName) {
+		return "", nil
+	}
+	bodyBuffer := &bytes.Buffer{}
+	multipartWriter := multipart.NewWriter(bodyBuffer)
+	// 添加字段
+	fieldWriter, err := multipartWriter.CreateFormField("objName")
+	if err != nil {
+		return "", err
+	}
+	_, err = fieldWriter.Write([]byte(objName))
+	if err != nil {
+		return "", err
+	}
+
+	// 添加文件
+	fileWriter, err := multipartWriter.CreateFormFile("file", objName)
+	if err != nil {
+		return "", err
+	}
+	_, err = io.Copy(fileWriter, reader)
+	if err != nil {
+		return "", err
+	}
+
+	bodyByte, err := io.ReadAll(bodyBuffer)
+	if err != nil {
+		return "", err
+	}
+	responseBytes, err := invokeAPI.Post(servicePrefix+"/operation/obj/create/image", map[string]string{
+		"Content-Type": multipartWriter.FormDataContentType()}, bodyByte)
+	if err != nil {
+		return "", err
+	}
+
+	resp := new(response.InfoResponse[string])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return "", err
+	}
+
+	if !resp.Success {
+		return "", errors.New(resp.Msg)
+	}
+
+	return resp.Info, nil
+}
+
 //func Cp(f, t string) error {
 //	return CpOrigin(prefix+f, prefix+t)
 //}