/
Tags.go
171 lines (138 loc) · 5.15 KB
/
Tags.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
// Licensed to NASA JPL under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. NASA JPL licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package endpoints
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"github.com/pixlise/core/v3/api/filepaths"
"github.com/pixlise/core/v3/api/handlers"
"github.com/pixlise/core/v3/api/permission"
apiRouter "github.com/pixlise/core/v3/api/router"
"github.com/pixlise/core/v3/api/services"
"github.com/pixlise/core/v3/core/api"
"github.com/pixlise/core/v3/core/pixlUser"
tagModel "github.com/pixlise/core/v3/core/tagModel"
)
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tags - Object Tagging Service
type tagHandler struct {
svcs *services.APIServices
}
func registerTagHandler(router *apiRouter.ApiObjectRouter) {
const pathPrefix = "tags"
router.AddJSONHandler(handlers.MakeEndpointPath(pathPrefix, datasetIdentifier), apiRouter.MakeMethodPermission("GET", permission.PermPublic), tagList)
router.AddJSONHandler(handlers.MakeEndpointPath(pathPrefix, datasetIdentifier), apiRouter.MakeMethodPermission("POST", permission.PermWriteDataAnalysis), tagPost)
router.AddJSONHandler(handlers.MakeEndpointPath(pathPrefix, datasetIdentifier, idIdentifier), apiRouter.MakeMethodPermission("DELETE", permission.PermWriteDataAnalysis), tagDelete)
}
func tagList(params handlers.ApiHandlerParams) (interface{}, error) {
s3Path := filepaths.GetTagPath(pixlUser.ShareUserID)
tags := tagModel.TagLookup{}
err := tagModel.GetTags(params.Svcs, s3Path, &tags)
if err != nil {
return nil, err
}
// Read tag IDs in alphabetical order, else we randomly fail unit test
tagIDs := []string{}
for k := range tags {
tagIDs = append(tagIDs, k)
}
sort.Strings(tagIDs)
// Update Tag creator names/emails
for _, tagID := range tagIDs {
tag := tags[tagID]
updatedCreator, creatorErr := params.Svcs.Users.GetCurrentCreatorDetails(tag.Creator.UserID)
if creatorErr != nil {
params.Svcs.Log.Infof("Failed to lookup user details for ID: %v, creator name in file: %v (Tag listing). Error: %v", tag.Creator.UserID, tag.Creator.Name, creatorErr)
} else {
tag.Creator = updatedCreator
}
}
// Return the combined set
return &tags, nil
}
func createTag(params handlers.ApiHandlerParams, tag tagModel.Tag) (string, error) {
if tag.DatasetID == "" {
tag.DatasetID = params.PathParams[datasetIdentifier]
}
s3Path := filepaths.GetTagPath(pixlUser.ShareUserID)
tags := tagModel.TagLookup{}
err := tagModel.GetTags(params.Svcs, s3Path, &tags)
if err != nil {
return "", err
}
// Download the file
if err != nil && !params.Svcs.FS.IsNotFoundError(err) {
// Only return error if it's not about the file missing, because user may not have interacted with this dataset yet
return "", err
}
for _, currentTag := range tags {
if tag.Name == currentTag.Name {
return "", api.MakeBadRequestError(fmt.Errorf("tag name already used: %v", tag.Name))
}
}
// If saveID is empty, this means it's either new or we're not overwriting, so we generate a new one
saveID := params.Svcs.IDGen.GenObjectID()
_, exists := tags[saveID]
if exists {
return "", fmt.Errorf("failed to generate unique ID")
}
tag.ID = saveID
tag.Creator = params.UserInfo
tag.DateCreated = params.Svcs.TimeStamper.GetTimeNowSec()
tags[saveID] = tag
err = params.Svcs.FS.WriteJSON(params.Svcs.Config.UsersBucket, s3Path, tags)
return saveID, err
}
func tagPost(params handlers.ApiHandlerParams) (interface{}, error) {
// Read in body
body, err := ioutil.ReadAll(params.Request.Body)
if err != nil {
return nil, err
}
var tag tagModel.Tag
err = json.Unmarshal(body, &tag)
if err != nil {
return nil, api.MakeBadRequestError(err)
}
tagID, err := createTag(params, tag)
validTag := tagModel.TagID{}
validTag.ID = tagID
return validTag, err
}
func tagDelete(params handlers.ApiHandlerParams) (interface{}, error) {
tagID := params.PathParams[idIdentifier]
s3Path := filepaths.GetTagPath(pixlUser.ShareUserID)
tags := tagModel.TagLookup{}
err := tagModel.GetTags(params.Svcs, s3Path, &tags)
if err != nil {
return nil, err
}
item, ok := tags[tagID]
if !ok {
return nil, api.MakeNotFoundError(tagID)
}
// Only allow shared tags to be deleted if same user requested it
if item.Creator.UserID != params.UserInfo.UserID {
return nil, api.MakeStatusError(http.StatusUnauthorized, fmt.Errorf("%v not owned by %v", tagID, params.UserInfo.UserID))
}
delete(tags, tagID)
err = params.Svcs.FS.WriteJSON(params.Svcs.Config.UsersBucket, s3Path, tags)
return nil, err
}