osm_sdk.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. package osm_sdk
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "git.sxidc.com/service-supports/osm_sdk/engine"
  7. "github.com/pkg/errors"
  8. "io"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. const TimeoutSec = 30
  15. var timeoutDuration = TimeoutSec * time.Second
  16. const servicePrefix = "/osm/api/v1"
  17. var prefix string
  18. var namespace string
  19. var name string
  20. var baseUrl string
  21. var httpClient *http_client.Client
  22. func Destroy() {
  23. if httpClient == nil {
  24. return
  25. }
  26. http_client.Destroy(httpClient)
  27. httpClient = nil
  28. }
  29. func Init(configPrefix string, configNamespace string, configName string, configBaseUrl string, duration time.Duration) error {
  30. if duration != 0 {
  31. timeoutDuration = duration
  32. }
  33. if strutils.IsStringEmpty(configNamespace) {
  34. return errors.New("namespace is empty")
  35. }
  36. if strutils.IsStringEmpty(configName) {
  37. return errors.New("name is empty")
  38. }
  39. if strutils.IsStringEmpty(configBaseUrl) {
  40. return errors.New("baseUrl is empty")
  41. }
  42. prefix = configPrefix
  43. namespace = configNamespace
  44. name = configName
  45. baseUrl = configBaseUrl + servicePrefix
  46. if httpClient == nil {
  47. httpClient = http_client.New()
  48. }
  49. return nil
  50. }
  51. func ExtractObjNameByUrl(urlStr string) (string, error) {
  52. if strutils.IsStringEmpty(urlStr) {
  53. return "", nil
  54. }
  55. parsed, err := url.Parse(urlStr)
  56. if err != nil {
  57. return "", err
  58. }
  59. objName, err := url.PathUnescape(parsed.Path)
  60. if err != nil {
  61. return "", err
  62. }
  63. return strings.TrimLeft(objName, "/"), nil
  64. }
  65. func GetUrl(objName string) (string, error) {
  66. return getUrl(objName)
  67. }
  68. func GetUrlWithPrefix(objName string) (string, error) {
  69. return getUrl(prefix + objName)
  70. }
  71. func getUrl(objName string) (string, error) {
  72. if strutils.IsStringEmpty(objName) {
  73. return "", nil
  74. }
  75. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  76. Get(baseUrl+"/operation/url/get",
  77. http_client.WithRequestQueryParams(map[string]string{
  78. "namespace": namespace,
  79. "name": name,
  80. "objName": objName,
  81. }))
  82. if err != nil {
  83. return "", err
  84. }
  85. resp := new(response.InfoResponse[string])
  86. err = getResponse.Json(resp)
  87. if err != nil {
  88. return "", err
  89. }
  90. if !resp.Success {
  91. return "", errors.New(resp.Msg)
  92. }
  93. return resp.Info, nil
  94. }
  95. func GenPolicy(dirPath string) (map[string]any, error) {
  96. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  97. Post(baseUrl+"/operation/obj/policy", GenPolicyJsonBody{
  98. ConfigKey: ConfigKey{
  99. Namespace: namespace,
  100. Name: name,
  101. },
  102. GenPolicyJsonBody: engine.GenPolicyJsonBody{
  103. Dir: dirPath,
  104. },
  105. })
  106. if err != nil {
  107. return nil, err
  108. }
  109. resp := new(response.InfoResponse[map[string]any])
  110. err = postResponse.Json(resp)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if !resp.Success {
  115. return nil, errors.New(resp.Msg)
  116. }
  117. return resp.Info, nil
  118. }
  119. func ZoomUrl(objName, profess string) (string, error) {
  120. return zoomUrl(objName, profess)
  121. }
  122. func ZoomUrlWithPrefix(objName, profess string) (string, error) {
  123. return zoomUrl(prefix+objName, profess)
  124. }
  125. func zoomUrl(objName, process string) (string, error) {
  126. if strutils.IsStringEmpty(objName) {
  127. return "", nil
  128. }
  129. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  130. Post(baseUrl+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
  131. ConfigKey: ConfigKey{
  132. Namespace: namespace,
  133. Name: name,
  134. },
  135. ObjName: objName,
  136. Process: process,
  137. })
  138. if err != nil {
  139. return "", err
  140. }
  141. resp := new(response.InfoResponse[string])
  142. err = postResponse.Json(resp)
  143. if err != nil {
  144. return "", err
  145. }
  146. if !resp.Success {
  147. return "", errors.New(resp.Msg)
  148. }
  149. return resp.Info, nil
  150. }
  151. func Exist(objName string) (bool, error) {
  152. return exist(objName)
  153. }
  154. func ExistWithPrefix(objName string) (bool, error) {
  155. return exist(prefix + objName)
  156. }
  157. func exist(objName string) (bool, error) {
  158. if strutils.IsStringEmpty(objName) {
  159. return false, nil
  160. }
  161. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/obj/check", http_client.WithRequestQueryParams(map[string]string{
  162. "namespace": namespace,
  163. "name": name,
  164. "objName": objName}))
  165. if err != nil {
  166. return false, err
  167. }
  168. resp := new(response.InfoResponse[bool])
  169. err = getResponse.Json(resp)
  170. if err != nil {
  171. return false, err
  172. }
  173. if !resp.Success {
  174. return false, errors.New(resp.Msg)
  175. }
  176. return true, nil
  177. }
  178. func DeleteMulti(objNames ...string) error {
  179. return deleteMulti(objNames...)
  180. }
  181. func DeleteMultiWithPrefix(userId string, objNames ...string) error {
  182. for i := range objNames {
  183. objNames[i] = prefix + objNames[i]
  184. }
  185. return deleteMulti(objNames...)
  186. }
  187. func deleteMulti(objNames ...string) error {
  188. if len(objNames) == 0 {
  189. return nil
  190. }
  191. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Post(baseUrl+"/operation/obj/deleteMulti", DeleteMultiObjJsonBody{
  192. ConfigKey: ConfigKey{
  193. Namespace: namespace,
  194. Name: name,
  195. },
  196. ObjNames: objNames,
  197. })
  198. if err != nil {
  199. return err
  200. }
  201. resp := new(response.MsgResponse)
  202. err = postResponse.Json(resp)
  203. if err != nil {
  204. return err
  205. }
  206. if !resp.Success {
  207. return errors.New(resp.Msg)
  208. }
  209. return nil
  210. }
  211. func ListDir(dirPath string) ([]string, error) {
  212. return listDir(dirPath)
  213. }
  214. func ListDirWithPrefix(dirPath string) ([]string, error) {
  215. return listDir(prefix + dirPath)
  216. }
  217. func listDir(dirPath string) ([]string, error) {
  218. if strutils.IsStringEmpty(dirPath) {
  219. return nil, nil
  220. }
  221. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/objPath/list", http_client.WithRequestQueryParams(map[string]string{
  222. "namespace": namespace,
  223. "name": name,
  224. "dirPath": dirPath,
  225. }))
  226. if err != nil {
  227. return nil, err
  228. }
  229. resp := new(response.InfoResponse[[]string])
  230. err = getResponse.Json(resp)
  231. if err != nil {
  232. return nil, err
  233. }
  234. if !resp.Success {
  235. return nil, errors.New(resp.Msg)
  236. }
  237. return resp.Info, nil
  238. }
  239. func LsDetails(dirPath string) ([]ObjectInfo, error) {
  240. return lsDetails(dirPath)
  241. }
  242. func LsDetailsWithPrefix(dirPath string) ([]ObjectInfo, error) {
  243. return lsDetails(prefix + dirPath)
  244. }
  245. type ObjectInfo struct {
  246. Key string
  247. Type string
  248. Size int64
  249. }
  250. func lsDetails(dirPath string) ([]ObjectInfo, error) {
  251. if strutils.IsStringEmpty(dirPath) {
  252. return []ObjectInfo{}, nil
  253. }
  254. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  255. Get(baseUrl+"/operation/objPath/listDetail", http_client.WithRequestQueryParams(map[string]string{
  256. "namespace": namespace,
  257. "name": name,
  258. "dirPath": dirPath,
  259. }))
  260. if err != nil {
  261. return nil, err
  262. }
  263. resp := new(response.InfoResponse[[]ObjectInfo])
  264. err = getResponse.Json(resp)
  265. if err != nil {
  266. return nil, err
  267. }
  268. if !resp.Success {
  269. return nil, errors.New(resp.Msg)
  270. }
  271. return resp.Info, nil
  272. }
  273. func Move(srcObjName, dstObjName string) (string, error) {
  274. return dstObjName, move(srcObjName, dstObjName, nil)
  275. }
  276. func MoveWithPrefix(srcObjName, dstObjName string) (string, error) {
  277. return prefix + dstObjName, move(prefix+srcObjName, prefix+dstObjName, nil)
  278. }
  279. func move(srcObjName, dstObjName string, requireInfos []string) error {
  280. if strutils.IsStringEmpty(srcObjName) {
  281. return nil
  282. }
  283. if strutils.IsStringEmpty(dstObjName) {
  284. return nil
  285. }
  286. putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  287. Put(baseUrl+"/operation/obj/move", MoveJsonBody{
  288. ConfigKey: ConfigKey{
  289. Namespace: namespace,
  290. Name: name,
  291. },
  292. SrcObjName: srcObjName,
  293. DstObjName: dstObjName,
  294. RequireInfos: requireInfos,
  295. })
  296. if err != nil {
  297. return err
  298. }
  299. resp := new(response.InfoResponse[any])
  300. err = putResponse.Json(resp)
  301. if err != nil {
  302. return err
  303. }
  304. if !resp.Success {
  305. return errors.New(resp.Msg)
  306. }
  307. return nil
  308. }
  309. func Copy(srcObjName, dstObjName string) (string, error) {
  310. return dstObjName, copying(srcObjName, dstObjName, nil)
  311. }
  312. func CopyWithPrefix(srcObjName, dstObjName string) (string, error) {
  313. return prefix + dstObjName, copying(prefix+srcObjName, prefix+dstObjName, nil)
  314. }
  315. func copying(srcObjName, dstObjName string, requireInfos []string) error {
  316. if strutils.IsStringEmpty(srcObjName) {
  317. return nil
  318. }
  319. if strutils.IsStringEmpty(dstObjName) {
  320. return nil
  321. }
  322. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  323. Post(baseUrl+"/operation/obj/copy", CopyJsonBody{
  324. ConfigKey: ConfigKey{
  325. Namespace: namespace,
  326. Name: name,
  327. },
  328. SrcObjName: srcObjName,
  329. DstObjName: dstObjName,
  330. RequireInfos: requireInfos,
  331. Prefix: prefix,
  332. })
  333. if err != nil {
  334. return err
  335. }
  336. resp := new(response.InfoResponse[any])
  337. err = postResponse.Json(resp)
  338. if err != nil {
  339. return err
  340. }
  341. if !resp.Success {
  342. return errors.New(resp.Msg)
  343. }
  344. return nil
  345. }
  346. func CopyPublic(srcObjName, dstObjName string) (string, error) {
  347. return dstObjName, copyPublic(srcObjName, dstObjName, nil)
  348. }
  349. func CopyPublicWithPrefix(srcObjName, dstObjName string) (string, error) {
  350. return prefix + dstObjName, copyPublic(prefix+srcObjName, prefix+dstObjName, nil)
  351. }
  352. func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
  353. if strutils.IsStringEmpty(srcObjName) {
  354. return nil
  355. }
  356. if strutils.IsStringEmpty(dstObjName) {
  357. return nil
  358. }
  359. postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
  360. postResponse, err := postRequest.Post(baseUrl+"/operation/obj/copyPublic", CopyJsonBody{
  361. ConfigKey: ConfigKey{
  362. Namespace: namespace,
  363. Name: name,
  364. },
  365. SrcObjName: srcObjName,
  366. DstObjName: dstObjName,
  367. RequireInfos: requireInfos,
  368. Prefix: prefix,
  369. })
  370. if err != nil {
  371. return err
  372. }
  373. resp := new(response.InfoResponse[any])
  374. err = postResponse.Json(resp)
  375. if err != nil {
  376. return err
  377. }
  378. if !resp.Success {
  379. return errors.New(resp.Msg)
  380. }
  381. return nil
  382. }
  383. func CreateImage(reader io.Reader, objName string) (string, error) {
  384. return createImage(reader, objName)
  385. }
  386. func CreateImageWithPrefix(reader io.Reader, objName string) (string, error) {
  387. return createImage(reader, prefix+objName)
  388. }
  389. func createImage(reader io.Reader, objName string) (string, error) {
  390. if strutils.IsStringEmpty(objName) {
  391. return "", nil
  392. }
  393. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  394. Post(baseUrl+"/operation/obj/create/image", nil,
  395. http_client.WithRequestFileReader("file", objName, reader),
  396. http_client.WithRequestFormData(map[string]string{
  397. "namespace": namespace,
  398. "name": name,
  399. "objName": objName,
  400. }))
  401. if err != nil {
  402. return "", err
  403. }
  404. resp := new(response.InfoResponse[string])
  405. err = postResponse.Json(resp)
  406. if err != nil {
  407. return "", err
  408. }
  409. if !resp.Success {
  410. return "", errors.New(resp.Msg)
  411. }
  412. return resp.Info, nil
  413. }
  414. func CreateFile(reader io.Reader, objName string, contentType string) (string, error) {
  415. return createFile(reader, objName, contentType)
  416. }
  417. func CreateFileWithPrefix(reader io.Reader, objName string, contentType string) (string, error) {
  418. return createFile(reader, prefix+objName, contentType)
  419. }
  420. func createFile(reader io.Reader, objName string, contentType string) (string, error) {
  421. if strutils.IsStringEmpty(objName) {
  422. return "", nil
  423. }
  424. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  425. Post(baseUrl+"/operation/obj/create/file", nil,
  426. http_client.WithRequestFileReader("file", objName, reader),
  427. http_client.WithRequestFormData(map[string]string{
  428. "namespace": namespace,
  429. "name": name,
  430. "objName": objName,
  431. "contentType": contentType,
  432. }))
  433. if err != nil {
  434. return "", err
  435. }
  436. resp := new(response.InfoResponse[string])
  437. err = postResponse.Json(resp)
  438. if err != nil {
  439. return "", err
  440. }
  441. if !resp.Success {
  442. return "", errors.New(resp.Msg)
  443. }
  444. return resp.Info, nil
  445. }
  446. func GetUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  447. return getUrlWithMetaData(objName)
  448. }
  449. func GetUrlUrlWithMetaDataWithPrefix(objName string) (UrlWithMetaData, error) {
  450. return getUrlWithMetaData(prefix + objName)
  451. }
  452. func getUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  453. var errResponse UrlWithMetaData
  454. if strutils.IsStringEmpty(objName) {
  455. return errResponse, nil
  456. }
  457. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  458. Get(baseUrl+"/operation/url/metaData/get",
  459. http_client.WithRequestQueryParams(map[string]string{
  460. "namespace": namespace,
  461. "name": name,
  462. "objName": objName,
  463. }))
  464. if err != nil {
  465. return errResponse, err
  466. }
  467. resp := new(response.InfoResponse[UrlWithMetaData])
  468. err = getResponse.Json(resp)
  469. if err != nil {
  470. return errResponse, err
  471. }
  472. if !resp.Success {
  473. return errResponse, errors.New(resp.Msg)
  474. }
  475. resp.Info.Size, err = strconv.ParseInt(resp.Info.SizeStr, 10, 64)
  476. if err != nil {
  477. return errResponse, errors.New("osm_sdk参数返回错误")
  478. }
  479. return resp.Info, nil
  480. }
  481. func GetContent(objName string) ([]byte, error) {
  482. return getContent(objName)
  483. }
  484. func GetContentWithPrefix(objName string) ([]byte, error) {
  485. return getContent(prefix + objName)
  486. }
  487. func getContent(objName string) ([]byte, error) {
  488. var errResponse []byte
  489. if strutils.IsStringEmpty(objName) {
  490. return errResponse, nil
  491. }
  492. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  493. Get(baseUrl+"/operation/obj/getContent",
  494. http_client.WithRequestQueryParams(map[string]string{
  495. "namespace": namespace,
  496. "name": name,
  497. "objName": objName,
  498. }))
  499. if err != nil {
  500. return errResponse, err
  501. }
  502. resp := new(response.InfoResponse[[]byte])
  503. err = getResponse.Json(resp)
  504. if err != nil {
  505. return errResponse, err
  506. }
  507. if !resp.Success {
  508. return errResponse, errors.New(resp.Msg)
  509. }
  510. return resp.Info, nil
  511. }