-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasters.go
188 lines (161 loc) · 4.99 KB
/
masters.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
package main
import (
"net/http"
"path/filepath"
"strings"
"github.com/bcc-code/bcc-media-flows/paths"
"github.com/bcc-code/bcc-media-flows/services/ingest"
ingestworkflows "github.com/bcc-code/bcc-media-flows/workflows/ingest"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/google/uuid"
"go.temporal.io/sdk/client"
)
func (s *TriggerServer) getPersons() ([]string, error) {
return singleValueArrayFromRows(s.database.Query("SELECT name FROM persons"))
}
func (s *TriggerServer) addPerson(id string) error {
_, err := s.database.Exec("INSERT INTO persons (name) VALUES (?) ON CONFLICT DO NOTHING", id)
return err
}
func (s *TriggerServer) getTags() ([]string, error) {
return singleValueArrayFromRows(s.database.Query("SELECT name FROM tags"))
}
func (s *TriggerServer) addTag(id string) error {
_, err := s.database.Exec("INSERT INTO tags (name) VALUES (?) ON CONFLICT DO NOTHING", id)
return err
}
func (s *TriggerServer) getProgramIDs() ([]string, error) {
return singleValueArrayFromRows(s.database.Query("SELECT name FROM program_ids"))
}
func (s *TriggerServer) addProgramID(id string) error {
_, err := s.database.Exec("INSERT INTO program_ids (name) VALUES (?) ON CONFLICT DO NOTHING", id)
return err
}
func (s *TriggerServer) removeProgramID(id string) error {
_, err := s.database.Exec("DELETE FROM program_ids WHERE name = ?", id)
return err
}
func (s *TriggerServer) uploadMasterAdminGET(ctx *gin.Context) {
programIDs, err := s.getProgramIDs()
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
ctx.HTML(http.StatusOK, "upload-master-admin.gohtml", gin.H{
"programIds": programIDs,
})
}
func (s *TriggerServer) uploadMasterAdminPOST(ctx *gin.Context) {
code := ctx.PostForm("code")
name := ctx.PostForm("name")
if code != "" && name != "" {
err := s.addProgramID(strings.ToUpper(code) + " - " + name)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
}
for _, id := range ctx.PostFormArray("deleteIds[]") {
err := s.removeProgramID(id)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
}
s.uploadMasterAdminGET(ctx)
}
func (s *TriggerServer) uploadMasterGET(ctx *gin.Context) {
filenames, err := getFilenames(masterTriggerDir)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
tags, err := s.getTags()
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
persons, err := s.getPersons()
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
programIDs, err := s.getProgramIDs()
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
ctx.HTML(http.StatusOK, "upload-master.gohtml", gin.H{
"files": filenames,
"tags": tags,
"persons": persons,
"programIds": programIDs,
})
}
type MasterPostParams struct {
ProgramID string `form:"programId"`
Tags []string `form:"tags[]"`
Persons []string `form:"persons[]"`
Path string `form:"path"`
SenderEmail string `form:"senderEmail"`
Language string `form:"language"`
Filename string `form:"filename"`
Episode string `form:"episode"`
EpisodeTitle string `form:"episodeTitle"`
EpisodeDescription string `form:"episodeDescription"`
DirectToPlayback bool `form:"directToPlayback"`
}
func (s *TriggerServer) uploadMasterPOST(ctx *gin.Context) {
var params MasterPostParams
err := ctx.BindWith(¶ms, binding.Form)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
queue := getQueue()
workflowOptions := client.StartWorkflowOptions{
ID: uuid.NewString(),
TaskQueue: queue,
}
for _, tag := range params.Tags {
err := s.addTag(tag)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
}
for _, person := range params.Persons {
err := s.addPerson(person)
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
}
rawPath := filepath.Join(masterTriggerDir, params.Path)
path, err := paths.Parse(rawPath)
if err != nil {
renderErrorPage(ctx, http.StatusBadRequest, err)
return
}
_, err = s.wfClient.ExecuteWorkflow(ctx, workflowOptions, ingestworkflows.Masters, ingestworkflows.MasterParams{
Metadata: &ingest.Metadata{
JobProperty: ingest.JobProperty{
ProgramID: params.ProgramID,
Tags: strings.Join(params.Tags, ", "),
PersonsAppearing: strings.Join(params.Persons, ", "),
SenderEmail: params.SenderEmail,
Language: params.Language,
ReceivedFilename: params.Filename,
EpisodeDescription: params.EpisodeDescription,
EpisodeTitle: params.EpisodeTitle,
Episode: params.Episode,
},
},
SourceFile: &path,
})
if err != nil {
renderErrorPage(ctx, http.StatusInternalServerError, err)
return
}
}