-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaltura.go
394 lines (367 loc) · 10.9 KB
/
kaltura.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
/*
* BSD-3-Clause
* Copyright 2020 sot (PR_713, C_rho_272)
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
package TtKVC
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const (
jsonMime = "application/json"
kAPISessionStart = "api_v3/service/session/action/start?format=1"
kAPISessionEnd = "api_v3/service/session/action/end?format=1&ks=%s"
kAPISessionGet = "api_v3/service/session/action/get?format=1&ks=%s"
kAPIMediaGet = "api_v3/service/media/action/get?format=1&ks=%s"
kAPIMediaAdd = "api_v3/service/media/action/add?format=1&ks=%s"
kAPIMediaAddContent = "api_v3/service/media/action/addContent?format=1&ks=%s"
kAPIFlavorsList = "api_v3/service/flavorAsset/action/List?format=1&ks=%s"
kAPIThumbnailContextFormat = "%s/width/%d/height/%d"
kSessionTTL = 1800
kUserSessionType = 0
kVideoMediaType = 1
kFileSourceType = "1"
KEntryStatusReady = 2
kEntryIdField = "entryId"
)
type Kaltura struct {
URL string `json:"url"`
PartnerId uint `json:"partnerid"`
UserId string `json:"userid"`
Secret string `json:"secret"`
session string
}
type KSession struct {
Secret string `json:"secret"`
UserID string `json:"userId"`
Type uint `json:"type"`
PartnerID uint `json:"partnerId"`
Expiry int64 `json:"expiry"`
Privileges string `json:"privileges"`
}
type KSessionInfo struct {
KS string `json:"ks"`
SessionType string `json:"sessionType"`
PartnerID string `json:"partnerId"`
UserID string `json:"userId"`
Expiry string `json:"expiry"`
Privileges string `json:"privileges"`
ObjectType string `json:"objectType"`
}
type KObject struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ObjectType string `json:"objectType,omitempty"`
Description string `json:"description,omitempty"`
}
type KFlavorAssetSearchResult struct {
KObject
TotalCount uint64 `json:"totalCount,omitempty"`
Objects []KFlavorAsset `json:"objects,omitempty"`
}
type KFilter struct {
Filter interface{} `json:"filter"`
}
type KBaseEntry struct {
KObject
UserId string `json:"userId,omitempty"`
CreatorId string `json:"creatorId,omitempty"`
Tags string `json:"tags,omitempty"`
Status int `json:"status,omitempty"`
DownloadURL string `json:"downloadUrl,omitempty"`
ThumbnailUrl string `json:"thumbnailUrl,omitempty"`
}
type KMediaEntry struct {
KBaseEntry
MediaType uint `json:"mediaType,omitempty"`
SourceType string `json:"sourceType,omitempty"`
}
type KFlavorAsset struct {
KObject
FlavorParamsID int64 `json:"flavorParamsId"`
Width uint `json:"width"`
Height uint `json:"height"`
Bitrate uint `json:"bitrate"`
FrameRate float64 `json:"frameRate"`
IsOriginal bool `json:"isOriginal"`
IsWeb bool `json:"isWeb"`
ContainerFormat string `json:"containerFormat"`
VideoCodecID string `json:"videoCodecId"`
Status int `json:"status"`
Language string `json:"language"`
IsDefault bool `json:"isDefault"`
EntryID string `json:"entryId"`
PartnerID uint `json:"partnerId"`
Version string `json:"version"`
Size uint64 `json:"size"`
Tags string `json:"tags"`
FileExt string `json:"fileExt"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
type KError struct {
Code string `json:"code"`
Message string `json:"message"`
ObjectType string `json:"objectType"`
Args interface{} `json:"args"`
}
func (kl *Kaltura) prepareURL(context string) string {
delimiter := ""
if strings.LastIndexByte(kl.URL, '/') != len(kl.URL)-1 {
delimiter = "/"
}
return fmt.Sprintf("%s%s%s", kl.URL, delimiter, context)
}
func (kl *Kaltura) postJson(context string, obj interface{}) ([]byte, error) {
var err error
var data []byte
fullUrl := kl.prepareURL(context)
if data, err = json.Marshal(obj); err == nil {
if resp, httpErr := http.Post(fullUrl, jsonMime, bytes.NewReader(data)); checkResponse(resp, httpErr) {
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
} else {
err = responseError(resp, httpErr)
}
}
return data, err
}
func (kl *Kaltura) CreateSession() error {
if !isEmpty(kl.session) {
kl.EndSession()
}
var err error
obj := KSession{
Secret: kl.Secret,
UserID: kl.UserId,
Type: kUserSessionType,
PartnerID: kl.PartnerId,
Expiry: time.Now().Unix() + kSessionTTL,
Privileges: "*",
}
var data []byte
if data, err = kl.postJson(kAPISessionStart, obj); err == nil {
if err = jsonError(data); err == nil {
kl.session = strings.Replace(string(data), "\"", "", -1)
err = nil
}
}
return err
}
func (kl *Kaltura) EndSession() {
if !isEmpty(kl.session) {
fullUrl := kl.prepareURL(kAPISessionEnd)
fullUrl = fmt.Sprintf(fullUrl, kl.session)
if resp, err := http.Get(fullUrl); !checkResponse(resp, err) {
logger.Error(responseError(resp, err))
}
kl.session = ""
}
}
var dummy = struct{}{}
func (kl *Kaltura) GetSession() (KSessionInfo, error) {
var err error
res := KSessionInfo{}
if isEmpty(kl.session) {
err = errors.New("unauthorized")
} else {
if err = kl.kSend(kAPISessionGet, dummy, &res); err != nil {
kl.session = ""
}
return res, err
}
return res, err
}
type kFlavorByEntryFilter struct {
KObject
EntryId string `json:"entryIdEqual"`
}
func (kl *Kaltura) kSend(context string, send interface{}, result interface{}) error {
var err error
if isEmpty(kl.session) {
return errors.New("empty session")
}
fullContext := fmt.Sprintf(context, kl.session)
var data []byte
if data, err = kl.postJson(fullContext, send); err == nil {
if err = jsonError(data); err == nil {
err = json.Unmarshal(data, result)
}
}
return err
}
func (kl *Kaltura) GetMediaEntryFlavorAssets(id string) (KFlavorAssetSearchResult, error) {
var err error
var res KFlavorAssetSearchResult
obj := KFilter{Filter: kFlavorByEntryFilter{
KObject: KObject{
ObjectType: "KalturaAssetFilter",
},
EntryId: id,
}}
err = kl.kSend(kAPIFlavorsList, obj, &res)
return res, err
}
func (kl *Kaltura) GetMediaEntry(id string) (KMediaEntry, error) {
var err error
var entry KMediaEntry
obj := map[string]string{kEntryIdField: id}
err = kl.kSend(kAPIMediaGet, obj, &entry)
return entry, err
}
func (kl *Kaltura) CreateMediaEntry(path, name string, tags []string) (string, error) {
var err error
var entry KMediaEntry
var entryId string
var entryName = name
if isEmpty(entryName) {
entryName = filepath.Base(path)
}
cTags := strings.Builder{}
if len(tags) > 0 {
isFirst := true
for _, tag := range tags {
if isFirst {
isFirst = false
} else{
cTags.WriteRune(',')
}
cTags.WriteString(tag)
}
}
obj := map[string]interface{}{
"entry": KMediaEntry{
KBaseEntry: KBaseEntry{
KObject: KObject{
Name: entryName,
ObjectType: "KalturaMediaEntry",
},
UserId: kl.UserId,
CreatorId: kl.UserId,
Tags: cTags.String(),
},
MediaType: kVideoMediaType,
SourceType: kFileSourceType,
},
}
if err = kl.kSend(kAPIMediaAdd, obj, &entry); err == nil {
if isEmpty(entry.Id) {
err = errors.New("unable to get entry id")
} else {
entryId = entry.Id
}
}
return entryId, err
}
func jsonError(data []byte) error {
var err error
outErr := KError{}
if err = json.Unmarshal(data, &outErr); err == nil {
if strings.Contains(outErr.ObjectType, "Exception") || !isEmpty(outErr.Code) {
err = errors.New(outErr.ObjectType + ":" + outErr.Message)
}
} else {
err = nil
}
return err
}
func (kl *Kaltura) UploadMediaContent(name, entryId string) error {
if isEmpty(kl.session) {
return errors.New("empty session")
}
r, w := io.Pipe()
defer r.Close()
m := multipart.NewWriter(w)
var err error
go func() {
defer w.Close()
defer m.Close()
if err == nil {
if err := m.WriteField(kEntryIdField, entryId); err != nil {
logger.Error(err)
return
}
if err := m.WriteField("resource:objectType", "KalturaUploadedFileResource"); err != nil {
logger.Error(err)
return
}
name = filepath.Clean(name)
part, err := m.CreateFormFile("resource:fileData", filepath.Base(name))
if err != nil {
logger.Error(err)
return
}
file, err := os.Open(name)
if err != nil {
logger.Error(err)
return
}
if file != nil {
if _, err = io.Copy(part, file); err != nil {
logger.Error(err)
}
if err = file.Close(); err != nil{
logger.Error(err)
}
} else {
logger.Error("File object is nil")
}
}
}()
fullUrl := kl.prepareURL(kAPIMediaAddContent)
fullUrl = fmt.Sprintf(fullUrl, kl.session)
var resp *http.Response
if resp, err = http.Post(fullUrl, m.FormDataContentType(), r); checkResponse(resp, err) {
defer resp.Body.Close()
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err == nil {
if err = jsonError(data); err == nil {
entry := KMediaEntry{}
if err = json.Unmarshal(data, &entry); err == nil {
logger.Debug(entry)
}
}
}
} else {
err = responseError(resp, err)
}
return err
}
func FormatThumbnailURL(plainUrl string, width, height uint) string {
if width == 0 || height == 0 {
return plainUrl
} else {
return fmt.Sprintf(kAPIThumbnailContextFormat, plainUrl, width, height)
}
}