Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix(server): transform asset url in datacatalogv3
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 11, 2024
1 parent 72dbf28 commit 827057a
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 37 deletions.
113 changes: 113 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions server/datacatalog/datacatalogv3/conv_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package datacatalogv3

import (
"fmt"
"net/url"
"path"
"strings"

"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/plateauapi"
Expand Down Expand Up @@ -160,3 +162,60 @@ func newAdmin(id string, stage stage, cmsurl string) any {

return a
}

func assetURLFromFormat(u string, f plateauapi.DatasetFormat) string {
u2, err := url.Parse(u)
if err != nil {
return u
}

dir := path.Dir(u2.Path)
ext := path.Ext(u2.Path)
base := path.Base(u2.Path)
name := strings.TrimSuffix(base, ext)
isArchive := ext == ".zip" || ext == ".7z"

u2.Path = assetRootPath(u2.Path)
if f == plateauapi.DatasetFormatCesium3dtiles {
if !isArchive {
// not CMS asset
return u
}

u2.Path = path.Join(u2.Path, "tileset.json")
return u2.String()
} else if f == plateauapi.DatasetFormatTiles || f == plateauapi.DatasetFormatMvt {
us := ""
if !isArchive {
// not CMS asset
us = u
} else {
ext := ""
if f == plateauapi.DatasetFormatMvt {
ext = "mvt"
} else {
ext = "png"
}

u2.Path = path.Join(u2.Path, "{z}/{x}/{y}."+ext)
us = u2.String()
}

return strings.ReplaceAll(strings.ReplaceAll(us, "%7B", "{"), "%7D", "}")
} else if f == plateauapi.DatasetFormatTms {
if !isArchive {
// not CMS asset
return u
}
return u2.String()
} else if (f == plateauapi.DatasetFormatCzml) && isArchive {
u2.Path = path.Join(dir, name, fmt.Sprintf("%s.%s", name, strings.ToLower(string(f))))
return u2.String()
}
return u
}

func assetRootPath(p string) string {
fn := strings.TrimSuffix(path.Base(p), path.Ext(p))
return path.Join(path.Dir(p), fn)
}
2 changes: 1 addition & 1 deletion server/datacatalog/datacatalogv3/conv_dataset_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (i *GenericItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType
return &plateauapi.GenericDatasetItem{
ID: plateauapi.NewID(datum.ID, plateauapi.TypeDatasetItem),
Name: firstNonEmptyValue(datum.Name, fmt.Sprintf("%s%s", i.Name, inds)),
URL: url,
URL: assetURLFromFormat(url, f),
Format: f,
Layers: layerNamesFrom(datum.LayerName),
ParentID: id,
Expand Down
13 changes: 7 additions & 6 deletions server/datacatalog/datacatalogv3/conv_dataset_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ func Test_GenericItem_ToDatasets(t *testing.T) {
DataFormat: "3D Tiles",
},
{
ID: "id2",
Data: "url2",
DataURL: "https://example.com/{z}/{x}/{y}.mvt",
Desc: "desc2",
LayerName: "layer1, layer2",
ID: "id2",
Data: "url2",
DataFormat: "MVT",
DataURL: "https://example.com/a.zip",
Desc: "desc2",
LayerName: "layer1, layer2",
},
// invalid item
{
Expand Down Expand Up @@ -63,7 +64,7 @@ func Test_GenericItem_ToDatasets(t *testing.T) {
{
ID: plateauapi.NewID("id2", plateauapi.TypeDatasetItem),
Name: "name 2",
URL: "https://example.com/{z}/{x}/{y}.mvt",
URL: "https://example.com/a/{z}/{x}/{y}.mvt",
Format: plateauapi.DatasetFormatMvt,
Layers: []string{"layer1", "layer2"},
ParentID: plateauapi.NewID("id", plateauapi.TypeDataset),
Expand Down
7 changes: 1 addition & 6 deletions server/datacatalog/datacatalogv3/conv_dataset_plateau.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,11 @@ func seedToDataset(seed plateauDatasetSeed) (res *plateauapi.PlateauDataset, war
}

func seedToDatasetItem(i plateauDatasetItemSeed, parentID string) *plateauapi.PlateauDatasetItem {
f := datasetFormatFromOrDetect(i.Format, i.URL)
if f == "" {
return nil
}

return &plateauapi.PlateauDatasetItem{
ID: plateauapi.NewID(i.GetID(parentID), plateauapi.TypeDatasetItem),
Name: i.GetName(),
URL: i.URL,
Format: f,
Format: i.Format,
Lod: i.LOD,
Texture: textureFrom(i.NoTexture),
ParentID: plateauapi.NewID(parentID, plateauapi.TypeDataset),
Expand Down
38 changes: 28 additions & 10 deletions server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ type plateauDatasetItemSeed struct {
ID string
Name string
URL string
Format string
Format plateauapi.DatasetFormat
LOD *int
NoTexture *bool
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func plateauDatasetItemSeedFrom(seed plateauDatasetSeed) (items []plateauDataset

switch {
case assetName.Ex.Normal != nil:
item = plateauDatasetItemSeedFromNormal(url, assetName.Ex.Normal)
item, w = plateauDatasetItemSeedFromNormal(url, assetName.Ex.Normal, cityCode)
case assetName.Ex.Urf != nil:
item, w = plateauDatasetItemSeedFromUrf(url, assetName.Ex.Urf, seed.Dic, cityCode)
case assetName.Ex.Fld != nil:
Expand All @@ -287,17 +287,29 @@ func plateauDatasetItemSeedFrom(seed plateauDatasetSeed) (items []plateauDataset
return
}

func plateauDatasetItemSeedFromNormal(url string, ex *AssetNameExNormal) *plateauDatasetItemSeed {
func plateauDatasetItemSeedFromNormal(url string, ex *AssetNameExNormal, cityCode string) (res *plateauDatasetItemSeed, w []string) {
format := datasetFormatFrom(ex.Format)
if format == "" {
w = append(w, fmt.Sprintf("plateau %s %s: invalid format: %s", cityCode, ex.Type, ex.Format))
return
}

return &plateauDatasetItemSeed{
Name: "", // use default
URL: url,
Format: ex.Format,
URL: assetURLFromFormat(url, format),
Format: format,
LOD: lo.ToPtr(ex.LOD),
NoTexture: lo.ToPtr(ex.NoTexture),
}
}, nil
}

func plateauDatasetItemSeedFromUrf(url string, ex *AssetNameExUrf, dic Dic, cityCode string) (_ *plateauDatasetItemSeed, w []string) {
format := datasetFormatFrom(ex.Format)
if format == "" {
w = append(w, fmt.Sprintf("plateau %s %s: unknown format: %s", cityCode, ex.Type, ex.Format))
return
}

entry, found := dic.FindEntryOrDefault(ex.Type, ex.DicKey())
if !found {
w = append(w, fmt.Sprintf("plateau %s %s: unknown dic key: %s", cityCode, ex.Type, ex.DicKey()))
Expand All @@ -313,14 +325,20 @@ func plateauDatasetItemSeedFromUrf(url string, ex *AssetNameExUrf, dic Dic, city

return &plateauDatasetItemSeed{
Name: entry.Description,
URL: url,
Format: ex.Format,
URL: assetURLFromFormat(url, format),
Format: format,
LOD: toPtrIfPresent(ex.LOD),
NoTexture: notexture,
}, w
}

func plateauDatasetItemSeedFromFld(url string, ex *AssetNameExFld, dic Dic, cityCode string) (_ *plateauDatasetItemSeed, w []string) {
format := datasetFormatFrom(ex.Format)
if format == "" {
w = append(w, fmt.Sprintf("plateau %s %s: unknown format: %s", cityCode, ex.Type, ex.Format))
return
}

key := ex.Key()
entry, found := dic.FindEntryOrDefault(ex.Type, ex.DicKey())
if !found {
Expand All @@ -333,8 +351,8 @@ func plateauDatasetItemSeedFromFld(url string, ex *AssetNameExFld, dic Dic, city
return &plateauDatasetItemSeed{
ID: key,
Name: entry.Scale,
URL: url,
Format: ex.Format,
URL: assetURLFromFormat(url, format),
Format: format,
NoTexture: &ex.NoTexture,
}, w
}
24 changes: 12 additions & 12 deletions server/datacatalog/datacatalogv3/conv_dataset_plateau_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
ID: plateauapi.NewID("11112_bldg_lod1", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "LOD1",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod1.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod1/tileset.json",
ParentID: plateauapi.NewID("11112_bldg", plateauapi.TypeDataset),
Lod: lo.ToPtr(1),
Texture: lo.ToPtr(plateauapi.TextureTexture),
Expand All @@ -112,7 +112,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
ID: plateauapi.NewID("11112_bldg_lod1_no_texture", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "LOD1(テクスチャなし)",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod1_no_texture.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod1_no_texture/tileset.json",
ParentID: plateauapi.NewID("11112_bldg", plateauapi.TypeDataset),
Lod: lo.ToPtr(1),
Texture: lo.ToPtr(plateauapi.TextureNone),
Expand All @@ -121,7 +121,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
ID: plateauapi.NewID("11112_bldg_lod2", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "LOD2",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod2.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11112_hoge-ku_lod2/tileset.json",
ParentID: plateauapi.NewID("11112_bldg", plateauapi.TypeDataset),
Lod: lo.ToPtr(2),
Texture: lo.ToPtr(plateauapi.TextureTexture),
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
ID: plateauapi.NewID("11113_bldg_lod1", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "LOD1",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11113_foo-ku_lod1.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_bldg_3dtiles_11113_foo-ku_lod1/tileset.json",
ParentID: plateauapi.NewID("11113_bldg", plateauapi.TypeDataset),
Lod: lo.ToPtr(1),
Texture: lo.ToPtr(plateauapi.TextureTexture),
Expand Down Expand Up @@ -255,15 +255,15 @@ func TestPlateauDataset_ToDatasets_Tnm(t *testing.T) {
ID: plateauapi.NewID("11111_tnm_AAA", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "AAA!",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_AAA_3dtiles.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_AAA_3dtiles/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_tnm_AAA", plateauapi.TypeDataset),
},
{
ID: plateauapi.NewID("11111_tnm_AAA_no_texture", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "AAA!(テクスチャなし)",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_AAA_3dtiles_no_texture.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_AAA_3dtiles_no_texture/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureNone),
ParentID: plateauapi.NewID("11111_tnm_AAA", plateauapi.TypeDataset),
},
Expand All @@ -286,7 +286,7 @@ func TestPlateauDataset_ToDatasets_Tnm(t *testing.T) {
ID: plateauapi.NewID("11111_tnm_BBB", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "BBB!",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_BBB_3dtiles.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_tnm_BBB_3dtiles/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_tnm_BBB", plateauapi.TypeDataset),
},
Expand Down Expand Up @@ -392,23 +392,23 @@ func TestPlateauDataset_ToDatasets_Fld(t *testing.T) {
ID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa_l1", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "計画規模",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l1.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l1/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa", plateauapi.TypeDataset),
},
{
ID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa_l1_no_texture", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "計画規模(テクスチャなし)",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l1_no_texture.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l1_no_texture/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureNone),
ParentID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa", plateauapi.TypeDataset),
},
{
ID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa_l2", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "想定最大規模",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l2.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yabegawa_haegawa_3dtiles_l2/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_fld_natl_yabegawa_haegawa", plateauapi.TypeDataset),
},
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestPlateauDataset_ToDatasets_Fld(t *testing.T) {
ID: plateauapi.NewID("11111_fld_natl_yodogawa_ujigawa_l1", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "計画規模",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yodogawa_ujigawa_3dtiles_l1.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_natl_yodogawa_ujigawa_3dtiles_l1/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_fld_natl_yodogawa_ujigawa", plateauapi.TypeDataset),
},
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestPlateauDataset_ToDatasets_Fld(t *testing.T) {
ID: plateauapi.NewID("11111_fld_pref_yodogawa_ujigawa_l1", plateauapi.TypeDatasetItem),
Format: plateauapi.DatasetFormatCesium3dtiles,
Name: "計画規模",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_pref_yodogawa_ujigawa_3dtiles_l1.zip",
URL: "https://example.com/11111_bar-shi_city_2023_citygml_1_op_fld_pref_yodogawa_ujigawa_3dtiles_l1/tileset.json",
Texture: lo.ToPtr(plateauapi.TextureTexture),
ParentID: plateauapi.NewID("11111_fld_pref_yodogawa_ujigawa", plateauapi.TypeDataset),
},
Expand Down
2 changes: 1 addition & 1 deletion server/datacatalog/datacatalogv3/conv_dataset_related.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (i *RelatedItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType
ID: plateauapi.NewID(sid, plateauapi.TypeDatasetItem),
Format: format,
Name: ftname,
URL: seed.URL,
URL: assetURLFromFormat(seed.URL, format),
ParentID: id,
},
},
Expand Down
24 changes: 24 additions & 0 deletions server/datacatalog/datacatalogv3/conv_dataset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package datacatalogv3

import (
"testing"

"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/plateauapi"
"github.com/stretchr/testify/assert"
)

func TestAssetURLFromFormat(t *testing.T) {
assert.Equal(t, "https://example.com/1111/a/tileset.json", assetURLFromFormat("https://example.com/1111/a.zip", plateauapi.DatasetFormatCesium3dtiles))
assert.Equal(t, "https://example.com/1111/a/tileset.json", assetURLFromFormat("https://example.com/1111/a.7z", plateauapi.DatasetFormatCesium3dtiles))
assert.Equal(t, "https://example.com/1111/a", assetURLFromFormat("https://example.com/1111/a", plateauapi.DatasetFormatCesium3dtiles))
assert.Equal(t, "https://example.com/1111/a/{z}/{x}/{y}.mvt", assetURLFromFormat("https://example.com/1111/a.zip", plateauapi.DatasetFormatMvt))
assert.Equal(t, "https://example.com/1111/a/{z}/{x}/{y}.mvt", assetURLFromFormat("https://example.com/1111/a.7z", plateauapi.DatasetFormatMvt))
assert.Equal(t, "https://example.com/1111/a/{z}/{x}/{y}.mvt", assetURLFromFormat("https://example.com/1111/a/%7Bz%7D/%7Bx%7D/%7By%7D.mvt", plateauapi.DatasetFormatMvt))
assert.Equal(t, "https://example.com/1111/a.zip", assetURLFromFormat("https://example.com/1111/a.zip", "hoge"))
assert.Equal(t, "https://example.com/1111/a/a.czml", assetURLFromFormat("https://example.com/1111/a.zip", plateauapi.DatasetFormatCzml))
assert.Equal(t, "https://example.com/1111/a", assetURLFromFormat("https://example.com/1111/a.zip", plateauapi.DatasetFormatTms))
}

func TestAssetRootPath(t *testing.T) {
assert.Equal(t, "/example.com/1111/a", assetRootPath("/example.com/1111/a.zip"))
}
2 changes: 1 addition & 1 deletion server/datacatalog/datacatalogv3/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestRepos(t *testing.T) {
{
ID: plateauapi.ID("di_" + cityCode + "_bldg_lod1"),
Format: plateauapi.DatasetFormatCesium3dtiles,
URL: "https://example.com/00000_hoge_city_2023_citygml_1_op_bldg_3dtiles_lod1.zip",
URL: "https://example.com/00000_hoge_city_2023_citygml_1_op_bldg_3dtiles_lod1/tileset.json",
Name: "LOD1",
ParentID: plateauapi.ID("d_" + cityCode + "_bldg"),
Lod: lo.ToPtr(1),
Expand Down

0 comments on commit 827057a

Please sign in to comment.