osm_sdk.go 14 KB

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