qinxiaodong 1 ヶ月 前
コミット
440df85e7e
1 ファイル変更648 行追加0 行削除
  1. 648 0
      osm_sdk.go

+ 648 - 0
osm_sdk.go

@@ -0,0 +1,648 @@
+package osm_sdk
+
+import (
+	"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"
+	"time"
+)
+
+const TimeoutSec = 30
+const servicePrefix = "/osm/api/v1"
+
+var prefix string
+var namespace string
+var name string
+var invokeAPI *invoke.API
+
+type (
+	ConfigKey struct {
+		Namespace string `json:"namespace" form:"namespace" binding:"required"`
+		Name      string `json:"name" form:"name" binding:"required"`
+	}
+
+	DeleteMultiObjJsonBody struct {
+		ConfigKey
+		ObjNames         []string `json:"objNames" binding:"required"`
+		UserID           string   `json:"userId"`
+		OperatorUserName string   `json:"operatorUserName" assign:"toField:OperatorUserName"`
+	}
+	CopyJsonBody struct {
+		ConfigKey
+		SrcObjName       string   `json:"srcObjName" binding:"required"`
+		DstObjName       string   `json:"dstObjName" binding:"required"`
+		RequireInfos     []string `json:"requireInfos"`
+		CreateUserID     string   `json:"createUserId" assign:"toField:CreateUserID"`
+		OperatorUserName string   `json:"operatorUserName" assign:"toField:OperatorUserName"`
+	}
+	MoveJsonBody struct {
+		ConfigKey
+		SrcObjName       string   `json:"srcObjName" binding:"required"`
+		DstObjName       string   `json:"dstObjName" binding:"required"`
+		RequireInfos     []string `json:"requireInfos"`
+		UpdateUserID     string   `json:"updateUserId" assign:"toField:LastUpdateUserID"`
+		OperatorUserName string   `json:"operatorUserName" assign:"toField:OperatorUserName"`
+	}
+	ZoomUrlJsonBody struct {
+		ConfigKey
+		ObjName          string   `json:"srcObjName" binding:"required"`
+		Process          string   `json:"dstObjName" binding:"required"`
+		RequireInfos     []string `json:"requireInfos"`
+		CreateUserID     string   `json:"createUserId" assign:"toField:CreateUserID"`
+		OperatorUserName string   `json:"operatorUserName" assign:"toField:OperatorUserName"`
+	}
+)
+
+func Destroy() {
+	if invokeAPI == nil {
+		return
+	}
+	invoke.DestroyAPI(invokeAPI)
+	invokeAPI = nil
+}
+
+func Init(configPrefix, configNamespace, configName, baseUrl string) error {
+	prefix = configPrefix
+	namespace = configNamespace
+	name = configName
+
+	if invokeAPI == nil {
+		invokeAPI = invoke.NewAPI(baseUrl, TimeoutSec*time.Second)
+	}
+
+	return nil
+}
+
+//func Touch(reader io.Reader, to string) error {
+//	return TouchPrefix(reader, to, true)
+//}
+
+//func TouchPrefix(reader io.Reader, to string, addPfx bool) error {
+//	if addPfx {
+//		return Bkt.PutObject(prefix+to, reader)
+//	}
+//	return Bkt.PutObject(to, reader)
+//}
+
+func GetUrl(objName, userId string) (string, error) {
+	return getUrl(objName, userId)
+}
+
+func GetUrlWithPrefix(objName, userId string) (string, error) {
+	return getUrl(prefix+objName, userId)
+}
+
+func getUrl(objName, userId string) (string, error) {
+	if strutils.IsStringEmpty(objName) {
+		return "", nil
+	}
+
+	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/url/get", map[string]string{
+		"namespace": namespace,
+		"name":      name,
+		"objName":   objName,
+		"userId":    userId,
+	})
+	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 ZoomUrl(obj, s string) (string, error) {
+//	if strutils.HasBlank(obj) {
+//		log.Println("oss发现违规参数")
+//		return "", nil
+//	}
+//	return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
+//}
+//
+//func ZoomUrlPfx(obj, s string, addPfx bool) (string, error) {
+//	if strutils.HasBlank(obj) {
+//		log.Println("oss发现违规参数")
+//		return "", nil
+//	}
+//
+//	if addPfx {
+//		return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
+//	}
+//
+//	return Bkt.SignURL(obj, oss.HTTPGet, 60, oss.Process(s))
+//
+//}
+
+func ZoomUrl(objName, profess string) (string, error) {
+	return zoomlUrl(objName, profess)
+}
+
+func ZoomUrlWithPrefix(objName, profess string) (string, error) {
+	return zoomlUrl(prefix+objName, profess)
+}
+
+func zoomlUrl(objName, process string) (string, error) {
+	if strutils.IsStringEmpty(objName) {
+		return "", nil
+	}
+
+	requestJson, err := json.Marshal(ZoomUrlJsonBody{
+		ConfigKey: ConfigKey{
+			Namespace: namespace,
+			Name:      name,
+		},
+		ObjName:      objName,
+		Process:      process,
+		RequireInfos: nil,
+	})
+	if err != nil {
+		return "", err
+	}
+
+	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/zoomUrl", requestJson, nil)
+	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 Exist(obj string) (bool, error) {
+//	if strutils.HasBlank(obj) {
+//		return false, nil
+//	}
+//
+//	return Bkt.IsObjectExist(prefix + obj)
+//}
+
+func Exist(objName, userId string) (bool, error) {
+	return exist(objName, userId)
+}
+
+func ExistWithPrefix(objName, userId string) (bool, error) {
+	return exist(prefix+objName, userId)
+}
+
+func exist(objName, userId string) (bool, error) {
+	if strutils.IsStringEmpty(objName) {
+		return false, nil
+	}
+	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/obj/check", map[string]string{
+		"namespace": namespace,
+		"name":      name,
+		"objName":   objName,
+		"userId":    userId,
+	})
+	if err != nil {
+		return false, err
+	}
+
+	resp := new(response.InfoResponse[bool])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return false, err
+	}
+
+	if !resp.Success {
+		return false, errors.New(resp.Msg)
+	}
+
+	return true, nil
+}
+
+func DeleteMulti(userId string, objNames ...string) error {
+	return deleteMulti(userId, objNames...)
+}
+
+func DeleteMultiWithPrefix(userId string, objNames ...string) error {
+	for _, objName := range objNames {
+		objName = prefix + objName
+	}
+
+	return deleteMulti(userId, objNames...)
+}
+
+func deleteMulti(userId string, objNames ...string) error {
+	if len(objNames) == 0 {
+		return nil
+	}
+
+	requestJson, err := json.Marshal(DeleteMultiObjJsonBody{
+		ConfigKey: ConfigKey{
+			Namespace: namespace,
+			Name:      name,
+		},
+		UserID:   userId,
+		ObjNames: objNames,
+	})
+	if err != nil {
+		return err
+	}
+
+	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/deleteMulti", requestJson, nil)
+	if err != nil {
+		return err
+	}
+
+	resp := new(response.MsgResponse)
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return err
+	}
+
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+
+	return nil
+}
+
+func ListDir(dirPath, userId string) ([]string, error) {
+	return listDir(dirPath, userId)
+}
+
+func ListDirWithPrefix(dirPath, userId string) ([]string, error) {
+	return listDir(prefix+dirPath, userId)
+}
+
+func listDir(dirPath, userId string) ([]string, error) {
+	if strutils.IsStringEmpty(dirPath) {
+		return nil, nil
+	}
+
+	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/objPath/list", map[string]string{
+		"namespace": namespace,
+		"name":      name,
+		"dirPath":   dirPath,
+		"userId":    userId,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	resp := new(response.InfoResponse[[]string])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return nil, err
+	}
+
+	if !resp.Success {
+		return nil, errors.New(resp.Msg)
+	}
+
+	return resp.Info, nil
+}
+
+//func LsWithoutPrefix(dir string) ([]string, error) {
+//	if strutils.HasBlank(dir) {
+//		return []string{}, nil
+//	}
+//
+//	v2, err := Bkt.ListObjectsV2(oss.Prefix(dir))
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	ks := make([]string, len(v2.Objects))
+//	for i, obj := range v2.Objects {
+//		ks[i] = obj.Key
+//	}
+//
+//	return ks, nil
+//}
+
+type ObjectInfo struct {
+	Key  string
+	Type string
+	Size int64
+}
+
+//func LsDetails(dir string) ([]ObjectInfo, error) {
+//	if strutils.HasBlank(dir) {
+//		return []ObjectInfo{}, nil
+//	}
+//
+//	v2, err := Bkt.ListObjectsV2(oss.Prefix(prefix + dir))
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	ks := make([]ObjectInfo, len(v2.Objects))
+//	for i, obj := range v2.Objects {
+//		ks[i] = ObjectInfo{
+//			Key:  obj.Key,
+//			Type: obj.Type,
+//			Size: obj.Size,
+//		}
+//	}
+//
+//	return ks, nil
+//}
+
+func LsDetails(dirPath, userId string) ([]ObjectInfo, error) {
+	return lsDetails(dirPath, userId)
+}
+
+func LsDetailsWithPrefix(dirPath, userId string) ([]ObjectInfo, error) {
+	return lsDetails(prefix+dirPath, userId)
+}
+
+func lsDetails(dirPath, userId string) ([]ObjectInfo, error) {
+	if strutils.IsStringEmpty(dirPath) {
+		return []ObjectInfo{}, nil
+	}
+
+	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/objPath/listDetail", map[string]string{
+		"namespace": namespace,
+		"name":      name,
+		"dirPath":   dirPath,
+		"userId":    userId,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	resp := new(response.InfoResponse[[]ObjectInfo])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return nil, err
+	}
+
+	if !resp.Success {
+		return nil, errors.New(resp.Msg)
+	}
+
+	return resp.Info, nil
+
+}
+
+//func Mv(f, t string) error {
+//	if err := Cp(f, t); err != nil {
+//		return err
+//	}
+//
+//	return Bkt.DeleteObject(prefix + f)
+//}
+
+func Move(srcObjName, dstObjName, userId string, requireInfos []string) error {
+	return move(srcObjName, dstObjName, userId, requireInfos)
+}
+
+func MoveWithPrefix(srcObjName, dstObjName, userId string, requireInfos []string) error {
+	return move(prefix+srcObjName, prefix+dstObjName, userId, requireInfos)
+}
+
+func moves(srcObjName, dstObjName, userId string, requireInfos []string) error {
+	if strutils.IsStringEmpty(srcObjName) {
+		return nil
+	}
+	if strutils.IsStringEmpty(dstObjName) {
+		return nil
+	}
+	err := copying(srcObjName, dstObjName, requireInfos)
+	if err != nil {
+		return err
+	}
+	return deleteMulti(userId, srcObjName)
+
+}
+
+func move(srcObjName, dstObjName, userId string, requireInfos []string) error {
+	if strutils.IsStringEmpty(srcObjName) {
+		return nil
+	}
+	if strutils.IsStringEmpty(dstObjName) {
+		return nil
+	}
+	requestJson, err := json.Marshal(MoveJsonBody{
+		ConfigKey: ConfigKey{
+			Namespace: namespace,
+			Name:      name,
+		},
+		SrcObjName:   srcObjName,
+		DstObjName:   dstObjName,
+		RequireInfos: requireInfos,
+	})
+	if err != nil {
+		return err
+	}
+	responseBytes, err := invokeAPI.PutJSON(servicePrefix+"/operation/obj/move", requestJson, nil)
+	if err != nil {
+		return err
+	}
+	resp := new(response.InfoResponse[any])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return err
+	}
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+
+	return nil
+}
+
+//func CpOrigin(f, t string) error {
+//	_, err := Bkt.CopyObject(f, t)
+//	return err
+//}
+
+func Copy(srcObjName, dstObjName string, requireInfos []string) error {
+	return copying(srcObjName, dstObjName, requireInfos)
+}
+
+func CopyWithPrefix(srcObjName, dstObjName string, requireInfos []string) error {
+	return copying(prefix+srcObjName, prefix+dstObjName, requireInfos)
+}
+
+func copying(srcObjName, dstObjName string, requireInfos []string) error {
+	if strutils.IsStringEmpty(srcObjName) {
+		return nil
+	}
+	if strutils.IsStringEmpty(dstObjName) {
+		return nil
+	}
+
+	requestJson, err := json.Marshal(CopyJsonBody{
+		ConfigKey: ConfigKey{
+			Namespace: namespace,
+			Name:      name,
+		},
+		SrcObjName:   srcObjName,
+		DstObjName:   dstObjName,
+		RequireInfos: requireInfos,
+	})
+	if err != nil {
+		return err
+	}
+
+	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/copy", requestJson, nil)
+	if err != nil {
+		return err
+	}
+
+	resp := new(response.InfoResponse[any])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return err
+	}
+
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+
+	return nil
+}
+
+func CopyPublic(srcObjName, dstObjName string, requireInfos []string) error {
+	return copyPublic(srcObjName, dstObjName, requireInfos)
+}
+
+func CopyPublicWithPrefix(srcObjName, dstObjName string, requireInfos []string) error {
+	return copyPublic(prefix+srcObjName, prefix+dstObjName, requireInfos)
+}
+
+func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
+	if strutils.IsStringEmpty(srcObjName) {
+		return nil
+	}
+	if strutils.IsStringEmpty(dstObjName) {
+		return nil
+	}
+	requestJson, err := json.Marshal(CopyJsonBody{
+		ConfigKey: ConfigKey{
+			Namespace: namespace,
+			Name:      name,
+		},
+		SrcObjName:   srcObjName,
+		DstObjName:   dstObjName,
+		RequireInfos: requireInfos,
+	})
+	if err != nil {
+		return err
+	}
+
+	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/copyPublic", requestJson, nil)
+	if err != nil {
+		return err
+	}
+
+	resp := new(response.InfoResponse[any])
+	err = json.Unmarshal(responseBytes, resp)
+	if err != nil {
+		return err
+	}
+
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+
+	return nil
+
+}
+
+//func Cp(f, t string) error {
+//	return CpOrigin(prefix+f, prefix+t)
+//}
+//
+//func TouchFormatImg(reader io.Reader, to string) error {
+//	if reader == nil || strutils.HasBlank(to) {
+//		log.Println("oss上传发现违规参数")
+//		return nil
+//	}
+//
+//	return Bkt.PutObject(prefix+to, reader, oss.ContentType("image/jpg"))
+//}
+
+//func GetObjectBytes(obj string) ([]byte, error) {
+//	if strutils.HasBlank(obj) {
+//		return nil, errors.New("参数错误")
+//	}
+//	buf := new(bytes.Buffer)
+//	body, err := Bkt.GetObject(prefix + obj)
+//	if err != nil {
+//		return nil, err
+//	}
+//	_, err = io.Copy(buf, body)
+//	if err != nil {
+//		return nil, err
+//	}
+//	err = body.Close()
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	return buf.Bytes(), nil
+//}
+
+//func getObjectBytes(objName, userId string) ([]byte, error) {
+//	if strutils.IsStringEmpty(objName) {
+//		return nil, nil
+//	}
+//	if strutils.IsStringEmpty(objName) {
+//		return nil, nil
+//	}
+//	buf := new(bytes.Buffer)
+//
+//	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/obj/getContent", map[string]string{
+//		"namespace": namespace,
+//		"name":      name,
+//		"objName":   objName,
+//		"userId":    userId,
+//	})
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	resp := new(response.InfoResponse[[]byte])
+//	err = json.Unmarshal(responseBytes, resp)
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	if !resp.Success {
+//		return nil, errors.New(resp.Msg)
+//	}
+//
+//	//return resp.Info, nil
+//	_, err = io.Copy(buf, resp.Info)
+//	if err != nil {
+//		return nil, err
+//	}
+//	err = body.Close()
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	return buf.Bytes(), nil
+//}
+
+//func MvWithoutPrefix(f, t string) error {
+//	if _, err := Bkt.CopyObject(f, t); err != nil {
+//		return err
+//	}
+//
+//	return Bkt.DeleteObject(f)
+//}