/
mappings_handler.go
304 lines (259 loc) · 7.71 KB
/
mappings_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package handlers
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/pkg/errors"
"github.com/decentraland/content-service/data"
"github.com/decentraland/content-service/storage"
. "github.com/decentraland/content-service/utils"
"github.com/go-redis/redis"
log "github.com/sirupsen/logrus"
)
type ParcelContent struct {
ParcelID string `json:"parcel_id"`
Contents []*ContentElement `json:"contents"`
RootCID string `json:"root_cid"`
Publisher string `json:"publisher"`
}
type SceneContent struct {
RootCID string `json:"root_cid"`
SceneCID string `json:"scene_cid"`
Content *ParcelContent `json:"content"`
}
type Scene struct {
ParcelId string `json:"parcel_id"`
RootCID string `json:"root_cid"`
SceneCID string `json:"scene_cid"`
}
type ContentElement struct {
File string `json:"file"`
Cid string `json:"hash"`
}
type StringPair struct {
A string
B string
}
// Logic layer
type MappingsHandler interface {
GetMappings(c *gin.Context)
GetScenes(c *gin.Context)
GetParcelInformation(parcelId string) (*ParcelContent, error)
GetInfo(c *gin.Context)
}
type mappingsHandlerImpl struct {
RedisClient data.RedisClient
Dcl data.Decentraland
Storage storage.Storage
Log *log.Logger
}
func NewMappingsHandler(client data.RedisClient, dcl data.Decentraland, storage storage.Storage, l *log.Logger) MappingsHandler {
return &mappingsHandlerImpl{
RedisClient: client,
Dcl: dcl,
Storage: storage,
Log: l,
}
}
type getMappingsParams struct {
Nw string `form:"nw" binding:"required"`
Se string `form:"se" binding:"required"`
}
func (sa *getMappingsParams) NwCoord() (int, int, error) {
return parseCoordinates(sa.Nw)
}
func (sa *getMappingsParams) SeCoord() (int, int, error) {
return parseCoordinates(sa.Se)
}
func parseCoordinates(coord string) (int, int, error) {
tkns := strings.Split(coord, ",")
if len(tkns) != 2 {
return 0, 0, errors.New("invalid coordinate")
}
x, err := strconv.Atoi(tkns[0])
if err != nil {
return 0, 0, errors.New("invalid coordinate")
}
y, err := strconv.Atoi(tkns[0])
if err != nil {
return 0, 0, errors.New("invalid coordinate")
}
return x, y, nil
}
func (ms *mappingsHandlerImpl) GetMappings(c *gin.Context) {
var params getMappingsParams
err := c.ShouldBindWith(¶ms, binding.Query)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid query params"})
return
}
x1, y1, err := params.NwCoord()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid query params"})
return
}
x2, y2, err := params.SeCoord()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid query params"})
return
}
parcels := RectToParcels(x1, y1, x2, y2, 200)
if parcels == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "too many parcels requested"})
return
}
mapContents := []ParcelContent{}
for _, pid := range parcels {
content, err := ms.GetParcelInformation(pid)
if err != nil {
c.Error(err)
ms.Log.WithError(err).Error("fail to retrieve parcel")
c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{"error": "unexpected error, try again later"})
return
}
if content != nil {
mapContents = append(mapContents, *content)
}
}
c.JSON(http.StatusOK, mapContents)
}
type getScenesParams struct {
X1 *int `form:"x1" binding:"exists,min=-150,max=150"`
Y1 *int `form:"y1" binding:"exists,min=-150,max=150"`
X2 *int `form:"x2" binding:"exists,min=-150,max=150"`
Y2 *int `form:"y2" binding:"exists,min=-150,max=150"`
}
func (ms *mappingsHandlerImpl) GetScenes(c *gin.Context) {
var p getScenesParams
err := c.ShouldBindWith(&p, binding.Query)
if err != nil {
println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid query params"})
return
}
pids := RectToParcels(*p.X1, *p.Y1, *p.X2, *p.Y2, 200)
if pids == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "too many parcels requested"})
return
}
cids := make(map[string]bool, len(pids))
for _, pid := range pids {
cid, err := ms.RedisClient.GetParcelCID(pid)
if cid == "" {
continue
}
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "unexpected error, try again later"})
return
}
validParcel, err := ms.RedisClient.ProcessedParcel(pid)
if err != nil {
log.Errorf("error when checking validity of parcel %s", pid)
// skip on error
}
if !validParcel {
continue
}
cids[cid] = true
}
ret := make([]*Scene, 0, len(cids))
for cid, _ := range cids {
parcels, err := ms.RedisClient.GetSceneParcels(cid)
if err != nil && err != redis.Nil {
ms.Log.WithError(err).Error("error reading scene from redis")
_ = c.Error(err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "unexpected error, try again later"})
return
}
sceneCID, err := ms.RedisClient.GetSceneCid(cid)
if err != nil && err != redis.Nil {
log.Errorf("error reading scene cid for cid %s", cid)
// we just use the empty string in this case
}
for _, p := range parcels {
ret = append(ret, &Scene{
SceneCID: sceneCID,
RootCID: cid,
ParcelId: p,
})
}
}
c.JSON(http.StatusOK, gin.H{"data": ret})
}
/**
Retrieves the consolidated information of a given Parcel <ParcelContent>
if the parcel does not exists, the ParcelContent.Contents will be nil
*/
func (ms *mappingsHandlerImpl) GetParcelInformation(parcelId string) (*ParcelContent, error) {
content, err := ms.RedisClient.GetParcelContent(parcelId)
if content == nil || err != nil {
return nil, err
}
var elements []*ContentElement
for name, cid := range content {
elements = append(elements, &ContentElement{File: name, Cid: cid})
}
metadata, err := ms.RedisClient.GetParcelMetadata(parcelId)
if metadata == nil || err != nil {
return nil, err
}
return &ParcelContent{ParcelID: parcelId, Contents: elements, RootCID: metadata["root_cid"].(string), Publisher: metadata["pubkey"].(string)}, nil
}
func (ms *mappingsHandlerImpl) GetInfo(c *gin.Context) {
cidsParam := c.Query("cids")
if len(cidsParam) <= 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid params"})
return
}
cids := strings.Split(cidsParam, ",")
parcels := make(map[string]*StringPair, len(cids))
for _, cid := range cids {
ps, err := ms.RedisClient.GetSceneParcels(cid)
if err != nil && err != redis.Nil {
ms.Log.WithError(err).Error("error reading scene from redis")
_ = c.Error(err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "unexpected error, try again later"})
return
}
sceneCID := ""
rootCID := ""
if ps == nil || len(ps) == 0 {
// Maybe the parameter is not the root cid, but the scene cid, which we will be eventually support better
rootCID, err = ms.RedisClient.GetRootCid(cid)
if err != nil && err != redis.Nil {
log.Errorf("error when getting rootcid for hash %s with error %s", cid, err)
continue
}
ps, err = ms.RedisClient.GetSceneParcels(rootCID)
if err != nil && err != redis.Nil {
log.Errorf("error when reading parcels for cid %s with error %s", rootCID, err)
continue
}
if ps == nil || len(ps) == 0 {
continue
}
sceneCID = cid
}
if sceneCID == "" {
sceneCID, _ = ms.RedisClient.GetSceneCid(cid)
rootCID = cid
}
parcels[rootCID] = &StringPair{A: ps[0], B: sceneCID}
}
ret := make([]*SceneContent, 0, len(cids))
for k, v := range parcels {
content, err := ms.GetParcelInformation(v.A)
if err != nil {
log.Errorf("error getting information for parcel %s with error %s", v.A, err)
continue
}
ret = append(ret, &SceneContent{
RootCID: k,
SceneCID: v.B,
Content: content,
})
}
c.JSON(http.StatusOK, gin.H{"data": ret})
}