-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
217 lines (207 loc) · 5.78 KB
/
helper.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
package resolver
import (
"context"
"emperror.dev/errors"
"fmt"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/gin-gonic/gin"
"github.com/je4/revcat/v2/pkg/sourcetype"
"github.com/je4/revcat/v2/tools/graph/model"
"regexp"
)
var nestedRegexp = regexp.MustCompile(`^\[([^\[\]]+)\]\.(.*)$`)
func createFilterQuery(filter *model.InFilter) (*types.Query, error) {
if filter.BoolTerm != nil && len(filter.BoolTerm.Values) > 0 {
var query = &types.Query{Bool: &types.BoolQuery{}}
qList := []types.Query{}
matches := nestedRegexp.FindStringSubmatch(filter.BoolTerm.Field)
for _, val := range filter.BoolTerm.Values {
if len(matches) == 3 {
qList = append(qList, types.Query{
Nested: &types.NestedQuery{
Path: matches[1],
Query: &types.Query{
Term: map[string]types.TermQuery{
fmt.Sprintf("%s.%s", matches[1], matches[2]): types.TermQuery{
Value: val,
},
},
},
},
})
} else {
qList = append(qList, types.Query{Term: map[string]types.TermQuery{
filter.BoolTerm.Field: types.TermQuery{
Value: val,
},
},
})
}
}
if len(qList) > 0 {
if filter.BoolTerm.And {
query.Bool.Must = qList
} else {
query.Bool.Should = qList
query.Bool.MinimumShouldMatch = 1
}
}
return query, nil
}
// return types.Query{}, errors.Errorf("unknown filter type")
return nil, nil
}
func stringsFromContext(ctx context.Context, key string) ([]string, error) {
stringsAny := ctx.Value(key)
if stringsAny == nil {
//return nil, errors.Errorf("no value for '%s' found in context", key)
return []string{}, nil
}
strs, ok := stringsAny.([]string)
if !ok {
return nil, errors.Errorf("invalid value type for '%s' found in context", key)
}
return strs, nil
}
func stringFromContext(ctx context.Context, key string) (string, error) {
stringAny := ctx.Value(key)
if stringAny == nil {
//return nil, errors.Errorf("no value for '%s' found in context", key)
return "", nil
}
str, ok := stringAny.(string)
if !ok {
return "", errors.Errorf("invalid value type for '%s' found in context", key)
}
return str, nil
}
func ginContextFromContext(ctx context.Context) (*gin.Context, error) {
ginContext := ctx.Value("GinContextKey")
if ginContext == nil {
err := errors.Errorf("could not retrieve gin.Context")
return nil, err
}
gc, ok := ginContext.(*gin.Context)
if !ok {
err := errors.Errorf("gin.Context has wrong type")
return nil, err
}
return gc, nil
}
func sourceMediaToMedia(m *sourcetype.Media) *model.Media {
if m == nil {
return nil
}
media := &model.Media{
Name: m.Name,
Mimetype: m.Mimetype,
// Pronom: &m.,
Type: m.Type,
URI: m.Uri,
Orientation: int(m.Orientation),
Fulltext: &m.Fulltext,
Width: int(m.Width),
Height: int(m.Height),
Length: int(m.Duration),
}
return media
}
func sourceToMediathekBaseEntry(src *sourcetype.SourceData) *model.MediathekBaseEntry {
entry := &model.MediathekBaseEntry{
ID: src.ID,
Signature: src.Signature,
SignatureOriginal: src.SignatureOriginal,
CollectionTitle: &src.CollectionTitle,
Source: src.Source,
Title: []*model.MultiLangString{}, //src.Title,
Series: &src.Series,
Place: &src.Place,
Date: &src.Date,
Person: []*model.Person{},
Category: src.Category,
Tags: src.Tags,
URL: &src.Url,
Publisher: &src.Publisher,
Rights: &src.Rights,
License: &src.License,
Type: &src.Type,
References: make([]*model.Reference, 0),
Poster: sourceMediaToMedia(src.Poster),
}
for _, person := range src.Persons {
p := &model.Person{
Name: person.Name,
}
if person.Role != "" {
p.Role = &person.Role
}
entry.Person = append(entry.Person, p)
}
for _, lang := range src.Title.GetNativeLanguages() {
entry.Title = append(entry.Title, &model.MultiLangString{
Lang: lang.String(),
Value: src.Title.Get(lang),
Translated: false,
})
}
for _, lang := range src.Title.GetTranslatedLanguages() {
entry.Title = append(entry.Title, &model.MultiLangString{
Lang: lang.String(),
Value: src.Title.Get(lang),
Translated: true,
})
}
return entry
}
func sourceToMediathekFullEntry(src *sourcetype.SourceData) *model.MediathekFullEntry {
entry := &model.MediathekFullEntry{
ID: src.ID,
Base: sourceToMediathekBaseEntry(src),
Notes: []*model.Note{},
Abstract: []*model.MultiLangString{}, //&src.Abstract,
ReferencesFull: []*model.MediathekBaseEntry{},
Extra: []*model.KeyValue{},
Media: []*model.MediaList{},
}
for _, lang := range src.Abstract.GetNativeLanguages() {
entry.Abstract = append(entry.Abstract, &model.MultiLangString{
Lang: lang.String(),
Value: src.Abstract.Get(lang),
Translated: false,
})
}
for _, lang := range src.Abstract.GetTranslatedLanguages() {
entry.Abstract = append(entry.Abstract, &model.MultiLangString{
Lang: lang.String(),
Value: src.Abstract.Get(lang),
Translated: true,
})
}
for _, note := range src.Notes {
entry.Notes = append(entry.Notes, &model.Note{
Title: ¬e.Title,
Text: string(note.Note),
})
}
if src.Extra != nil {
for key, val := range *src.Extra {
entry.Extra = append(entry.Extra, &model.KeyValue{
Key: key,
Value: val,
})
}
}
if src.Media != nil {
for key, ml := range src.Media {
mediaList := &model.MediaList{
Name: key,
Items: make([]*model.Media, 0),
}
for _, media := range ml {
mediaList.Items = append(mediaList.Items, sourceMediaToMedia(&media))
}
entry.Media = append(entry.Media, mediaList)
}
}
return entry
}