8
0

osm.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package osm
  2. import (
  3. ossDomain "ecos/application/domain/oss"
  4. "ecos/config"
  5. "ecos/utils"
  6. "git.sxidc.com/service-supports/fslog"
  7. "git.sxidc.com/service-supports/osm_sdk"
  8. "github.com/pkg/errors"
  9. "strings"
  10. )
  11. func ExistWithoutPrefix(obj string) (bool, error) {
  12. if utils.HasBlank(obj) {
  13. return false, nil
  14. }
  15. return osm_sdk.Exist(obj)
  16. }
  17. func UrlWithoutPrefix(obj string) (string, string, error) {
  18. if utils.HasBlank(obj) {
  19. fslog.Warn("oss获取Url发现违规参数")
  20. return "", "", nil
  21. }
  22. metaData, err := osm_sdk.GetUrlWithMetaData(obj)
  23. if err != nil {
  24. return "", "", err
  25. }
  26. println(metaData.Url, metaData.FileType, metaData.FileName, metaData.MetaData.FileName, metaData.MetaData.FileType)
  27. return metaData.Url, metaData.FileName, nil
  28. }
  29. func MultipleFileUrl(filePath string) ([]ossDomain.FileObject, error) {
  30. filePathArr := strings.Split(filePath, ",")
  31. picture := make([]ossDomain.FileObject, 0)
  32. for _, file := range filePathArr {
  33. exist, err := ExistWithoutPrefix(file)
  34. if err != nil {
  35. fslog.Error(err)
  36. picture = append(picture, ossDomain.FileObject{
  37. Url: file,
  38. Temp: "",
  39. })
  40. continue
  41. }
  42. if !exist {
  43. fslog.Error("文件【" + file + "】不存在")
  44. picture = append(picture, ossDomain.FileObject{
  45. Url: file,
  46. Temp: "",
  47. })
  48. continue
  49. }
  50. url, fileName, err := UrlWithoutPrefix(file)
  51. if err != nil {
  52. fslog.Error("文件【" + file + "】链接生成失败")
  53. picture = append(picture, ossDomain.FileObject{
  54. Url: file,
  55. Temp: "",
  56. })
  57. continue
  58. }
  59. picture = append(picture, ossDomain.FileObject{
  60. Url: file,
  61. Name: fileName,
  62. Temp: url,
  63. })
  64. }
  65. return picture, nil
  66. }
  67. func MultipleFileMv(filePath string) (string, error) {
  68. endPicture := make([]string, 0)
  69. files := strings.Split(filePath, ",")
  70. for _, file := range files {
  71. //文件名
  72. tmpFx := config.GetConfig().OsmConfig.TmpPrefix
  73. pfx := config.GetConfig().OsmConfig.Prefix
  74. if utils.HasText(file) && strings.Contains(file, "/temp/") {
  75. fileName, found := strings.CutPrefix(file, tmpFx)
  76. if !found {
  77. return "", errors.New("文件名处理失败")
  78. }
  79. err := MvWithoutPrefix(file, pfx+fileName)
  80. if err != nil {
  81. return "", errors.New("文件移动失败")
  82. }
  83. endPicture = append(endPicture, pfx+fileName)
  84. continue
  85. }
  86. endPicture = append(endPicture, file)
  87. }
  88. return strings.Join(endPicture, ","), nil
  89. }
  90. func MvWithoutPrefix(f, t string) error {
  91. if _, err := osm_sdk.Copy(f, t); err != nil {
  92. return err
  93. }
  94. return osm_sdk.DeleteMulti(f)
  95. }