-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_collections.go
515 lines (437 loc) · 18.8 KB
/
handler_collections.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package main
import (
"fmt"
"github.com/gazebo-web/fuel-server/bundles/collections"
"github.com/gazebo-web/fuel-server/bundles/models"
"github.com/gazebo-web/fuel-server/bundles/users"
"github.com/gazebo-web/fuel-server/bundles/worlds"
"github.com/gazebo-web/fuel-server/globals"
"github.com/gazebo-web/gz-go/v7"
"github.com/jinzhu/gorm"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"
"time"
)
// CollectionList returns the list of collections from a team/user. The returned
// value will be of type "collections.Collections"
// It follows the func signature defined by type "searchHandler".
// You can request this method with the following curl request:
//
// curl -k -X GET --url https://localhost:4430/1.0/collections
//
// or curl -k -X GET --url https://localhost:4430/1.0/collections.json
// or curl -k -X GET --url https://localhost:4430/1.0/{username}/collections with all the
// above format variants.
// func CollectionList(tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
func CollectionList(p *gz.PaginationRequest, owner *string, order, search string,
user *users.User, tx *gorm.DB, w http.ResponseWriter,
r *http.Request) (interface{}, *gz.PaginationResult, *gz.ErrMsg) {
var extend bool
// Check if we need to only return collections that the user can extend
v, ok := r.URL.Query()["extend"]
if ok {
extend, _ = strconv.ParseBool(v[0])
}
s := &collections.Service{}
return s.CollectionList(p, tx, owner, order, search, extend, user)
}
// CollectionIndex returns a single Collection. The returned value will be of
// type "collections.Collection".
// You can request this method with the following curl request:
//
// curl -k -H "Content-Type: application/json" -X GET https://localhost:4430/1.0/{username}/collections/{name}
func CollectionIndex(owner, name string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
s := &collections.Service{}
return s.GetCollection(tx, owner, name, user)
}
// CollectionRemove removes a Collection based on owner and name
// You can request this method with the following curl request:
//
// curl -k -X DELETE --url https://localhost:4430/1.0/{username}/collections/{name}
func CollectionRemove(owner, name string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
if em := (&collections.Service{}).RemoveCollection(tx, owner, name, user); em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbDelete, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil, nil
}
// createCollectionFn is a callback func that "creation handlers" will pass to
// doCreateCollection. It is expected that createFn will have the real logic for
// the Collection creation.
type createCollectionFn func(tx *gorm.DB, jwtUser *users.User, w http.ResponseWriter,
r *http.Request) (*collections.Collection, *gz.ErrMsg)
// doCreateCollection provides the pre and post steps needed to create a collection.
// Handlers should invoke this function and pass a createCollectionFn callback.
func doCreateCollection(tx *gorm.DB, cb createCollectionFn, w http.ResponseWriter,
r *http.Request) (*collections.Collection, *gz.ErrMsg) {
// Extract the owner from the request.
jwtUser, ok, errMsg := getUserFromJWT(tx, r)
if !ok {
return nil, &errMsg
}
// invoke the actual createCollectionFn (the callback function)
col, em := cb(tx, jwtUser, w, r)
if em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorNoDatabase, err)
}
infoStr := "A new collection has been created:" +
"\n\t name: " + *col.Name +
"\n\t owner: " + *col.Owner +
"\n\t creator: " + *col.Creator +
"\n\t uuid: " + *col.UUID +
"\n\t CreationDate: " + col.CreatedAt.UTC().Format(time.RFC3339)
gz.LoggerFromRequest(r).Info(infoStr)
// TODO: we should NOT be returning the DB record (including ID) to users.
return col, nil
}
// CollectionCreate creates a new collection based on input form. It returns a
// collections.Collection or an error.
// You can request this method with the following cURL request:
//
// curl -k -H "Content-Type: application/json" -X POST -d '{"name":"my collection",
// "description":"a super cool collection", owner:"name"}'
// https://localhost:4430/1.0/collections
// --header 'authorization: Bearer <your-jwt-token-here>'
func CollectionCreate(tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var cc collections.CreateCollection
if em := ParseStruct(&cc, r, false); em != nil {
return nil, em
}
createFn := func(tx *gorm.DB, jwtUser *users.User, w http.ResponseWriter,
r *http.Request) (*collections.Collection, *gz.ErrMsg) {
// Create the collection via the Collections Service
s := &collections.Service{}
col, em := s.CreateCollection(r.Context(), tx, cc, jwtUser)
if em != nil {
return nil, em
}
return col, nil
}
return doCreateCollection(tx, createFn, w, r)
}
// CollectionUpdate modifies an existing collection.
// You can request this method with the following cURL request:
//
// curl -k -X PATCH -F description="New Description"
// -F file=@<full-path-to-file;filename=aFileName>
// https://localhost:4430/1.0/{username}/collections/{name}
// -H 'Authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func CollectionUpdate(owner, name string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
err := r.ParseMultipartForm(0)
if err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorUnexpected, err)
}
// Delete temporary files from r.ParseMultipartForm(0)
defer func(form *multipart.Form) {
err := form.RemoveAll()
if err != nil {
log.Println("Failed to close form:", err)
}
}(r.MultipartForm)
var uc collections.UpdateCollection
if errMsg := ParseStruct(&uc, r, true); errMsg != nil {
return nil, errMsg
}
bFiles := r.MultipartForm != nil && len(getRequestFiles(r)) > 0
if uc.IsEmpty() && !bFiles {
return nil, gz.NewErrorMessage(gz.ErrorFormInvalidValue)
}
// If the user has also sent files, then update them
var newFilesPath *string
if bFiles {
// first, populate files into tmp dir to avoid overriding original
// files in case of error.
tmpDir, err := os.MkdirTemp("", name)
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
gz.LoggerFromContext(r.Context()).Error("Unable to remove directory: ", tmpDir)
}
}()
if err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorRepo, err)
}
if _, errMsg := populateTmpDir(r, false, tmpDir); errMsg != nil {
return nil, errMsg
}
newFilesPath = &tmpDir
}
col, em := (&collections.Service{}).UpdateCollection(r.Context(), tx, owner,
name, uc.Description, newFilesPath, uc.Private, user)
if em != nil {
return nil, em
}
infoStr := "Collection has been updated:" +
"\n\t name: " + *col.Name +
"\n\t owner: " + *col.Owner +
"\n\t uuid: " + *col.UUID +
"\n\t location: " + *col.Location +
"\n\t CreatedAt: " + col.CreatedAt.UTC().Format(time.RFC3339) +
"\n\t UpdatedAt: " + col.UpdatedAt.UTC().Format(time.RFC3339)
gz.LoggerFromRequest(r).Info(infoStr)
return &col, nil
}
// CollectionModelsList returns the list of models of a collection.
// You can request this method with the following cURL request:
//
// curl -k https://localhost:4430/1.0/{username}/collections/{col_name}/models
func CollectionModelsList(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetList(colOwner, colName, collections.TModel, user, tx, w, r)
}
// CollectionModelAdd associates a model to a collection.
// You can request this method with the following cURL request:
//
// curl -k -d '{"name":"model name", owner:"model owner"}'
// -X POST https://localhost:4430/1.0/{username}/collections/{col_name}/models
// --header 'authorization: Bearer <your-jwt-token-here>'
func CollectionModelAdd(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetAdd(colOwner, colName, collections.TModel, user, tx, w, r)
}
// CollectionModelRemove removes a model from a collection.
// You can request this method with the following cURL request:
//
// curl -k -d '{"name":"model name", owner:"model owner"}'
// -X DELETE https://localhost:4430/1.0/{username}/collections/{col_name}/models
// --header 'authorization: Bearer <your-jwt-token-here>'
func CollectionModelRemove(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetRemove(colOwner, colName, collections.TModel, user, tx, w, r)
}
// CollectionWorldAdd associates a world to a collection.
// You can request this method with the following cURL request:
//
// curl -k -d '{"name":"world name", owner:"world owner"}'
// -X POST https://localhost:4430/1.0/{username}/collections/{col_name}/worlds
// --header 'authorization: Bearer <your-jwt-token-here>'
func CollectionWorldAdd(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetAdd(colOwner, colName, collections.TWorld, user, tx, w, r)
}
// CollectionWorldRemove removes a world from a collection.
// You can request this method with the following cURL request:
//
// curl -k -d '{"name":"world name", owner:"world owner"}'
// -X DELETE https://localhost:4430/1.0/{username}/collections/{col_name}/worlds
// --header 'authorization: Bearer <your-jwt-token-here>'
func CollectionWorldRemove(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetRemove(colOwner, colName, collections.TWorld, user, tx, w, r)
}
// CollectionWorldsList returns the list of worlds of a collection.
// You can request this method with the following cURL request:
//
// curl -k https://localhost:4430/1.0/{username}/collections/{col_name}/worlds
func CollectionWorldsList(colOwner, colName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
return collectionAssetList(colOwner, colName, collections.TWorld, user, tx, w, r)
}
// collectionAssetAdd associates an asset to a collection. It requires the
// asset type as mandatory argument.
func collectionAssetAdd(colOwner, colName, assetType string, user *users.User,
tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var no collections.NameOwnerPair
if em := ParseStruct(&no, r, false); em != nil {
return nil, em
}
if _, em := (&collections.Service{}).AddAsset(r.Context(), tx, colOwner, colName,
no, assetType, user); em != nil {
return nil, em
}
// Update elastic search with the new collection association information.
if assetType == collections.TModel {
model, em := (&models.Service{Storage: globals.Storage}).GetModel(tx, no.Owner, no.Name, user)
if em != nil {
return nil, em
}
models.ElasticSearchUpdateModel(r.Context(), tx, *model)
} else if assetType == collections.TWorld {
world, em := (&worlds.Service{Storage: globals.Storage}).GetWorld(tx, no.Owner, no.Name, user)
if em != nil {
return nil, em
}
worlds.ElasticSearchUpdateWorld(r.Context(), *world)
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil, nil
}
// collectionAssetRemove deletes an asset from a collection. It requires the
// asset type as mandatory argument.
func collectionAssetRemove(colOwner, colName, assetType string, user *users.User,
tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var no collections.NameOwnerPair
// Read the name and owner from URL query. DELETE does not allow body.
no.Owner = r.URL.Query().Get("o")
no.Name = r.URL.Query().Get("n")
// Validate struct values
if em := ValidateStruct(&no); em != nil {
return nil, em
}
if _, em := (&collections.Service{}).RemoveAsset(r.Context(), tx, colOwner, colName,
no, assetType, user); em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbDelete, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil, nil
}
// collectionAssetList returns the list of assets associated to a collection.
// The returned value will be paginated and will be of
// type "collections.CollectionAssets".
// The assetType argument can be used filter assets by type, eg: model|world.
func collectionAssetList(colOwner, colName, assetType string, user *users.User,
tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Prepare pagination
pr, em := gz.NewPaginationRequest(r)
if em != nil {
return nil, em
}
s := &collections.Service{}
assets, pagination, em := s.GetCollectionAssets(pr, tx, colOwner, colName,
assetType, user)
if em != nil {
return nil, em
}
err := gz.WritePaginationHeaders(*pagination, w, r)
if err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorUnexpected, err)
}
return assets, nil
}
// ModelCollections returns the list of collections associated to a given model.
// You can request this method with the following cURL request:
//
// curl -k https://localhost:4430/1.0/{username}/models/{model_name}/collections
func ModelCollections(owner, modelName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
no := collections.NameOwnerPair{Name: modelName, Owner: owner}
return associatedCollectionsList(collections.TModel, no, user, tx, w, r)
}
// WorldCollections returns the list of collections associated to a given world.
// You can request this method with the following cURL request:
//
// curl -k https://localhost:4430/1.0/{username}/worlds/{world_name}/collections
func WorldCollections(owner, worldName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
no := collections.NameOwnerPair{Name: worldName, Owner: owner}
return associatedCollectionsList(collections.TWorld, no, user, tx, w, r)
}
// associatedCollectionsList returns the list of collections to which an asset, ie.
// a model or world, belongs to.
func associatedCollectionsList(assetType string, no collections.NameOwnerPair,
user *users.User, tx *gorm.DB, w http.ResponseWriter,
r *http.Request) (interface{}, *gz.ErrMsg) {
// Prepare pagination
pr, em := gz.NewPaginationRequest(r)
if em != nil {
return nil, em
}
s := &collections.Service{}
cols, pagination, em := s.GetAssociatedCollections(pr, tx, no, assetType, user)
if em != nil {
return nil, em
}
err := gz.WritePaginationHeaders(*pagination, w, r)
if err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorUnexpected, err)
}
return cols, nil
}
// CollectionIndividualFileDownload downloads an individual file from a collection.
// You can request this method with the following curl request:
//
// curl -k -X GET --url https://localhost:4430/1.0/{username}/collections/{name}/{version}/files/{file-path}
//
// eg. curl -k -X GET --url https://localhost:4430/1.0/{username}/collections/{name}/tip/files/thumbnails/logo.png
func CollectionIndividualFileDownload(owner, name string, user *users.User,
tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
s := &collections.Service{}
return IndividualFileDownload(s, owner, name, user, tx, w, r)
}
// CollectionClone clones a collection.
// You can request this method with the following curl request:
//
// curl -k -X POST --url http://localhost:3000/1.0/{other-username}/collections/{collection-name}/clone
// --header 'Private-Token: <your-private-token-here>'
func CollectionClone(sourceCollectionOwner, sourceCollectionName string,
ignored *users.User, tx *gorm.DB, w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Parse information about the collection to clone
var cloneData collections.CloneCollection
if em := ParseStruct(&cloneData, r, false); em != nil {
return nil, em
}
createFn := func(tx *gorm.DB, jwtUser *users.User, w http.ResponseWriter, r *http.Request) (*collections.Collection, *gz.ErrMsg) {
// Ask the Models Service to clone the model
cs := &collections.Service{}
clone, em := cs.CloneCollection(r.Context(), tx, sourceCollectionOwner, sourceCollectionName, cloneData, jwtUser)
if em != nil {
return nil, em
}
return clone, nil
}
return doCreateCollection(tx, createFn, w, r)
}
// CollectionTransfer transfer ownership of a collection to an organization.
// The source owner must have write permissions on the destination organization
//
// curl -k -X POST -H "Content-Type: application/json" http://localhost:8000/1.0/{username}/collections/{collectionname}/transfer --header "Private-Token: {private-token}" -d '{"destOwner":"destination_owner_name"}'
//
// \todo Support transfer of collection to owners other users and organizations.
// This will require some kind of email notifcation to the destination and
// acceptance form.
func CollectionTransfer(sourceOwner, collectionName string, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Read the request and check permissions.
transferAsset, em := processTransferRequest(sourceOwner, tx, r)
if em != nil {
return nil, em
}
// Get the collection
cs := &collections.Service{}
collection, em := cs.GetCollection(tx, sourceOwner, collectionName, user)
if em != nil {
extra := fmt.Sprintf("Collection [%s] not found", collectionName)
return nil, gz.NewErrorMessageWithArgs(gz.ErrorNameNotFound, em.BaseError, []string{extra})
}
if em := transferMoveResource(tx, collection, sourceOwner, transferAsset.DestOwner); em != nil {
return nil, em
}
tx.Save(&collection)
return &collection, nil
}