localizeMainJson.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/charmbracelet/log"
  5. jsoniter "github.com/json-iterator/go"
  6. "github.com/json-iterator/go/extra"
  7. "io"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. func init() {
  14. // 在init中设置一次即可
  15. extra.RegisterFuzzyDecoders()
  16. }
  17. func CovertJsonAndDownload(path string, dest string, ossUrl string) (err error) {
  18. dataFile := dest + "/someData.json"
  19. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  20. // Get the data
  21. resp, err := http.Get(path)
  22. if err != nil {
  23. return err
  24. }
  25. defer resp.Body.Close()
  26. bodyBytes, err := io.ReadAll(resp.Body)
  27. content := string(bodyBytes)
  28. //以字串过滤所有url到本地
  29. hasManage := ossUrl + "720yun_fd_manage/"
  30. hasEditData := ossUrl + "scene_edit_data/"
  31. if strings.Contains(content, hasManage) {
  32. content = strings.ReplaceAll(content, hasManage, "data/")
  33. }
  34. if strings.Contains(content, hasEditData) {
  35. content = strings.ReplaceAll(content, ossUrl, "data/")
  36. }
  37. target := &ScenesDataType{}
  38. err = json.NewDecoder(strings.NewReader(content)).Decode(&target)
  39. if err != nil {
  40. fmt.Println(err)
  41. return err
  42. }
  43. //补数据default
  44. for _, scene := range target.Scenes {
  45. if scene.InitVisual.Vlookatmin == nil {
  46. scene.InitVisual.Vlookatmin = -90
  47. }
  48. if scene.InitVisual.Vlookatmax == nil {
  49. scene.InitVisual.Vlookatmax = 90
  50. }
  51. //for _, hotspot := range scene.SomeData.Hotspots {
  52. //
  53. //}
  54. }
  55. file, err := json.Marshal(target)
  56. if err != nil {
  57. panic(err)
  58. }
  59. //log.Info("data json:", string(file))
  60. // Writer the body to file
  61. err = os.WriteFile(dataFile, file, 0644)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func DownloadMainXml(path string, dest string) (err error) {
  68. log.Info("DownloadMainXml", "url", fmt.Sprintf("%s", path))
  69. dataFile := dest + "/tour.xml"
  70. dir := filepath.Dir(dataFile)
  71. err = os.MkdirAll(dir, 0644)
  72. if err != nil {
  73. return err
  74. }
  75. // Get the data
  76. file, err := os.Create(dataFile)
  77. if err != nil {
  78. return err
  79. }
  80. resp, err := http.Get(path)
  81. if err != nil {
  82. return err
  83. }
  84. defer resp.Body.Close()
  85. _, err = io.Copy(file, resp.Body)
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }