123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- package osm_sdk
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-tools/utils/http_client"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/pkg/errors"
- "io"
- "time"
- )
- const TimeoutSec = 30
- var timeoutDuration = TimeoutSec * time.Second
- const servicePrefix = "/osm/api/v1"
- var prefix string
- var namespace string
- var name string
- var baseUrl string
- var httpClient *http_client.Client
- func Destroy() {
- if httpClient == nil {
- return
- }
- http_client.Destroy(httpClient)
- httpClient = nil
- }
- func Init(configPrefix, configNamespace, configName, configBaseUrl string) error {
- prefix = configPrefix
- namespace = configNamespace
- name = configName
- baseUrl = configBaseUrl + servicePrefix
- if httpClient == nil {
- httpClient = http_client.New()
- }
- return nil
- }
- func GetUrl(objName string) (string, error) {
- return getUrl(objName)
- }
- func GetUrlWithPrefix(objName string) (string, error) {
- return getUrl(prefix + objName)
- }
- func getUrl(objName string) (string, error) {
- if strutils.IsStringEmpty(objName) {
- return "", nil
- }
- getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Get(baseUrl+"/operation/url/get",
- http_client.WithRequestQueryParams(map[string]string{
- "namespace": namespace,
- "name": name,
- "objName": objName,
- }))
- if err != nil {
- return "", err
- }
- resp := new(response.InfoResponse[string])
- err = getResponse.Json(resp)
- if err != nil {
- return "", err
- }
- if !resp.Success {
- return "", errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
- func ZoomUrl(objName, profess string) (string, error) {
- return zoomUrl(objName, profess)
- }
- func ZoomUrlWithPrefix(objName, profess string) (string, error) {
- return zoomUrl(prefix+objName, profess)
- }
- func zoomUrl(objName, process string) (string, error) {
- if strutils.IsStringEmpty(objName) {
- return "", nil
- }
- postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Post(baseUrl+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
- ConfigKey: ConfigKey{
- Namespace: namespace,
- Name: name,
- },
- ObjName: objName,
- Process: process,
- RequireInfos: nil,
- })
- if err != nil {
- return "", err
- }
- resp := new(response.InfoResponse[string])
- err = postResponse.Json(resp)
- if err != nil {
- return "", err
- }
- if !resp.Success {
- return "", errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
- func Exist(objName string) (bool, error) {
- return exist(objName)
- }
- func ExistWithPrefix(objName string) (bool, error) {
- return exist(prefix + objName)
- }
- func exist(objName string) (bool, error) {
- if strutils.IsStringEmpty(objName) {
- return false, nil
- }
- getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/obj/check", http_client.WithRequestQueryParams(map[string]string{
- "namespace": namespace,
- "name": name,
- "objName": objName}))
- if err != nil {
- return false, err
- }
- resp := new(response.InfoResponse[bool])
- err = getResponse.Json(resp)
- if err != nil {
- return false, err
- }
- if !resp.Success {
- return false, errors.New(resp.Msg)
- }
- return true, nil
- }
- func DeleteMulti(objNames ...string) error {
- return deleteMulti(objNames...)
- }
- func DeleteMultiWithPrefix(userId string, objNames ...string) error {
- for _, objName := range objNames {
- objName = prefix + objName
- }
- return deleteMulti(objNames...)
- }
- func deleteMulti(objNames ...string) error {
- if len(objNames) == 0 {
- return nil
- }
- postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Post(baseUrl+"/operation/obj/deleteMulti", DeleteMultiObjJsonBody{
- ConfigKey: ConfigKey{
- Namespace: namespace,
- Name: name,
- },
- Prefix: prefix,
- ObjNames: objNames,
- })
- if err != nil {
- return err
- }
- resp := new(response.MsgResponse)
- err = postResponse.Json(resp)
- if err != nil {
- return err
- }
- if !resp.Success {
- return errors.New(resp.Msg)
- }
- return nil
- }
- func ListDir(dirPath string) ([]string, error) {
- return listDir(dirPath)
- }
- func ListDirWithPrefix(dirPath string) ([]string, error) {
- return listDir(prefix + dirPath)
- }
- func listDir(dirPath string) ([]string, error) {
- if strutils.IsStringEmpty(dirPath) {
- return nil, nil
- }
- getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/objPath/list", http_client.WithRequestQueryParams(map[string]string{
- "namespace": namespace,
- "name": name,
- "dirPath": dirPath,
- }))
- if err != nil {
- return nil, err
- }
- resp := new(response.InfoResponse[[]string])
- err = getResponse.Json(resp)
- if err != nil {
- return nil, err
- }
- if !resp.Success {
- return nil, errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
- func LsDetails(dirPath string) ([]ObjectInfo, error) {
- return lsDetails(dirPath)
- }
- func LsDetailsWithPrefix(dirPath string) ([]ObjectInfo, error) {
- return lsDetails(prefix + dirPath)
- }
- type ObjectInfo struct {
- Key string
- Type string
- Size int64
- }
- func lsDetails(dirPath string) ([]ObjectInfo, error) {
- if strutils.IsStringEmpty(dirPath) {
- return []ObjectInfo{}, nil
- }
- getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Get(baseUrl+"/operation/objPath/listDetail", http_client.WithRequestQueryParams(map[string]string{
- "namespace": namespace,
- "name": name,
- "dirPath": dirPath,
- }))
- if err != nil {
- return nil, err
- }
- resp := new(response.InfoResponse[[]ObjectInfo])
- err = getResponse.Json(resp)
- if err != nil {
- return nil, err
- }
- if !resp.Success {
- return nil, errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
- func Move(srcObjName, dstObjName string) (string, error) {
- return dstObjName, move(srcObjName, dstObjName, nil)
- }
- func MoveWithPrefix(srcObjName, dstObjName string) (string, error) {
- return prefix + dstObjName, move(prefix+srcObjName, prefix+dstObjName, nil)
- }
- func move(srcObjName, dstObjName string, requireInfos []string) error {
- if strutils.IsStringEmpty(srcObjName) {
- return nil
- }
- if strutils.IsStringEmpty(dstObjName) {
- return nil
- }
- putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Put(baseUrl+"/operation/obj/move", MoveJsonBody{
- ConfigKey: ConfigKey{
- Namespace: namespace,
- Name: name,
- },
- SrcObjName: srcObjName,
- DstObjName: dstObjName,
- RequireInfos: requireInfos,
- Prefix: prefix,
- })
- if err != nil {
- return err
- }
- resp := new(response.InfoResponse[any])
- err = putResponse.Json(resp)
- if err != nil {
- return err
- }
- if !resp.Success {
- return errors.New(resp.Msg)
- }
- return nil
- }
- func Copy(srcObjName, dstObjName string) (string, error) {
- return dstObjName, copying(srcObjName, dstObjName, nil)
- }
- func CopyWithPrefix(srcObjName, dstObjName string) (string, error) {
- return prefix + dstObjName, copying(prefix+srcObjName, prefix+dstObjName, nil)
- }
- func copying(srcObjName, dstObjName string, requireInfos []string) error {
- if strutils.IsStringEmpty(srcObjName) {
- return nil
- }
- if strutils.IsStringEmpty(dstObjName) {
- return nil
- }
- postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Post(baseUrl+"/operation/obj/copy", CopyJsonBody{
- ConfigKey: ConfigKey{
- Namespace: namespace,
- Name: name,
- },
- SrcObjName: srcObjName,
- DstObjName: dstObjName,
- RequireInfos: requireInfos,
- Prefix: prefix,
- })
- if err != nil {
- return err
- }
- resp := new(response.InfoResponse[any])
- err = postResponse.Json(resp)
- if err != nil {
- return err
- }
- if !resp.Success {
- return errors.New(resp.Msg)
- }
- return nil
- }
- func CopyPublic(srcObjName, dstObjName string) (string, error) {
- return dstObjName, copyPublic(srcObjName, dstObjName, nil)
- }
- func CopyPublicWithPrefix(srcObjName, dstObjName string) (string, error) {
- return prefix + dstObjName, copyPublic(prefix+srcObjName, prefix+dstObjName, nil)
- }
- func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
- if strutils.IsStringEmpty(srcObjName) {
- return nil
- }
- if strutils.IsStringEmpty(dstObjName) {
- return nil
- }
- postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
- postResponse, err := postRequest.Post(baseUrl+"/operation/obj/copyPublic", CopyJsonBody{
- ConfigKey: ConfigKey{
- Namespace: namespace,
- Name: name,
- },
- SrcObjName: srcObjName,
- DstObjName: dstObjName,
- RequireInfos: requireInfos,
- Prefix: prefix,
- })
- if err != nil {
- return err
- }
- resp := new(response.InfoResponse[any])
- err = postResponse.Json(resp)
- if err != nil {
- return err
- }
- if !resp.Success {
- return errors.New(resp.Msg)
- }
- return nil
- }
- 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
- }
- postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Post(baseUrl+"/operation/obj/create/image", nil,
- http_client.WithRequestFileReader("file", objName, reader),
- http_client.WithRequestFormData(map[string]string{
- "namespace": namespace,
- "name": name,
- "objName": objName,
- }))
- if err != nil {
- return "", err
- }
- resp := new(response.InfoResponse[string])
- err = postResponse.Json(resp)
- if err != nil {
- return "", err
- }
- if !resp.Success {
- return "", errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
- func GetUrlWithMetaData(objName string) (UrlWithMetaData, error) {
- return getUrlWithMetaData(objName)
- }
- func GetUrlUrlWithMetaDataWithPrefix(objName string) (UrlWithMetaData, error) {
- return getUrlWithMetaData(prefix + objName)
- }
- func getUrlWithMetaData(objName string) (UrlWithMetaData, error) {
- var errResponse UrlWithMetaData
- if strutils.IsStringEmpty(objName) {
- return errResponse, nil
- }
- getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
- Get(baseUrl+"/operation/url/metaData/get",
- http_client.WithRequestQueryParams(map[string]string{
- "namespace": namespace,
- "name": name,
- "objName": objName,
- }))
- if err != nil {
- return errResponse, err
- }
- resp := new(response.InfoResponse[UrlWithMetaData])
- err = getResponse.Json(resp)
- if err != nil {
- return errResponse, err
- }
- if !resp.Success {
- return errResponse, errors.New(resp.Msg)
- }
- return resp.Info, nil
- }
|