| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package utils
- import (
- "encoding/json"
- "fmt"
- "github.com/charmbracelet/log"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- )
- func CovertJsonAndDownload(path string, dest string, ossUrl string) (err error) {
- log.Info("CovertJsonAndDownload", "url", fmt.Sprintf("%s", path))
- dataFile := dest + "/someData.json"
- // Get the data
- resp, err := http.Get(path)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- bodyBytes, err := io.ReadAll(resp.Body)
- content := string(bodyBytes)
- //以字串过滤所有url到本地
- if strings.Contains(content, ossUrl+"/720yun_fd_manage/") {
- content = strings.ReplaceAll(content, "https://4dkk.4dage.com/720yun_fd_manage/", "data/")
- }
- if strings.Contains(content, ossUrl+"/scene_edit_data/") {
- content = strings.ReplaceAll(content, "https://4dkk.4dage.com/", "data/")
- }
- target := &ScenesDataType{}
- err = json.NewDecoder(strings.NewReader(content)).Decode(&target)
- if err != nil {
- fmt.Println(err)
- return err
- }
- //补数据default
- for _, Scenes := range target.Scenes {
- if Scenes.InitVisual.Vlookatmin == nil {
- d := json.Number("-90")
- Scenes.InitVisual.Vlookatmin = &d
- }
- if Scenes.InitVisual.Vlookatmax == nil {
- m := json.Number("90")
- Scenes.InitVisual.Vlookatmax = &m
- }
- //fmt.Println("InitVisual %s\n", Scenes.InitVisual)
- }
- file, err := json.Marshal(target)
- if err != nil {
- panic(err)
- }
- //log.Info("data json:", string(file))
- // Writer the body to file
- err = os.WriteFile(dataFile, file, 0644)
- if err != nil {
- return err
- }
- return nil
- }
- func DownloadMainXml(path string, dest string) (err error) {
- log.Info("DownloadMainXml", "url", fmt.Sprintf("%s", path))
- dataFile := dest + "/tour.xml"
- dir := filepath.Dir(dataFile)
- err = os.MkdirAll(dir, 0644)
- if err != nil {
- return err
- }
- // Get the data
- file, err := os.Create(dataFile)
- if err != nil {
- return err
- }
- resp, err := http.Get(path)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- _, err = io.Copy(file, resp.Body)
- if err != nil {
- return err
- }
- return nil
- }
|