Files
zmkgC/api/v1/common/source/clt/clt.go

134 lines
3.3 KiB
Go
Raw Normal View History

2025-07-07 20:11:59 +08:00
package clt
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/tiger1103/gfast/v3/api/v1/common/globe"
"github.com/tiger1103/gfast/v3/api/v1/common/tool"
"github.com/tiger1103/gfast/v3/database"
"github.com/tiger1103/gfast/v3/database/sqlite"
"io"
"net/http"
"os"
"path"
"strings"
)
func InitCltData(group *ghttp.RouterGroup) {
group.GET("/data/tileset/{source_id}/*.action", cltCallback)
group.GET("/data/bim/{source_id}/*.action", cltCallback)
}
func GetTile(sourceid, p string) []byte {
md5 := tool.Md5V(p)
tile := globe.Tile{}
database.GetSourceDB(sourceid).DB.Select("tile").Where("md5=?", md5).First(&tile)
// 创建一个字节缓冲区,并将压缩数据写入其中
buf := bytes.NewBuffer(tile.Tile)
// 创建一个gzip.Reader对象用于解压缩数据
reader, _ := gzip.NewReader(buf)
defer reader.Close()
// 读取解压缩后的数据
decompressedData, _ := io.ReadAll(reader)
return decompressedData
}
func cltCallback(request *ghttp.Request) {
sourceId := request.Get("source_id").String()
cltObj := database.GetSourceDB(sourceId)
if cltObj.DB == nil {
request.Response.WriteStatus(http.StatusNotFound)
return
}
argcs := strings.Split(request.RequestURI, "/")
argcs = argcs[7:]
md5 := tool.Md5V(strings.Join(argcs, "/"))
tile := globe.Tile{}
RowsAffected := cltObj.DB.Select("tile").Where("md5=?", md5).Find(&tile).RowsAffected
if RowsAffected == 0 {
request.Response.WriteStatus(http.StatusNotFound)
return
}
suffix := path.Ext(request.RequestURI)
if suffix == ".json" {
request.Response.Header().Set("content-type", "application/json")
} else {
request.Response.Header().Set("content-type", "application/octet-stream")
}
if cltObj.Gzip {
request.Response.Header().Set("Content-Encoding", "gzip")
}
globe.RenderData(request, tile.Tile)
}
type Info struct {
Params string `json:"params"`
}
type parseIsZip struct {
Zip bool `json:"zip"`
}
type IsJct struct {
Jct bool `json:"jct"`
}
func OpenClt(cltPath string, sourceID string) (error, *database.SourceObj) {
//if !globe.IS_OFFLINE_VERSION {
// //网络版事 需要拼接数据地址,方便服务器迁移
if !tool.PathExists(cltPath) {
getwd, err := os.Getwd()
if err != nil {
return err, nil
}
cltPath = path.Join(getwd, cltPath)
}
//}
fmt.Println(cltPath)
if tool.PathExists(cltPath) {
db, err := sqlite.OpenDB(cltPath)
if err != nil {
return err, nil
}
var obj database.SourceObj
obj.DB = db
var info []Info
db.Model(&Info{}).Find(&info)
p := parseIsZip{}
errs := json.Unmarshal([]byte(info[0].Params), &p)
if errs == nil {
obj.Gzip = p.Zip
}
suffix := path.Ext(cltPath)
if suffix == globe.CLT {
obj.Type = globe.TILESET
obj.Url = "/zm/api/v1/data/tileset/" + sourceID + "/tileset.json"
} else {
obj.Type = globe.BIM
obj.Url = "/zm/api/v1/data/bim/" + sourceID + "/tileset.json"
if len(info) < 2 {
//非jct资源
globe.CloseDB(db)
return globe.GetErrors("非jct资源"), nil
}
isjct := IsJct{}
errs := json.Unmarshal([]byte(info[1].Params), &isjct)
if errs != nil {
globe.CloseDB(db)
return globe.GetErrors("jct资源检测失败"), nil
}
}
database.SetSourceDB(sourceID, obj)
return err, &obj
}
fmt.Println("资源不存在:" + cltPath)
return globe.GetErrors("资源不存在:" + cltPath), nil
}