-
Notifications
You must be signed in to change notification settings - Fork 2
/
media.go
446 lines (409 loc) · 15 KB
/
media.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
package routes
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io"
"net/http"
"strconv"
"strings"
// We import this so that we can decode gifs.
_ "image/gif"
// We import this so that we can decode pngs.
_ "image/png"
"cloud.google.com/go/storage"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/h2non/bimg"
"google.golang.org/api/option"
)
// GetGCSClient ...
func (fes *APIServer) GetGCSClient(ctx context.Context) (*storage.Client, error) {
// If we have credentials, use them. Otherwise, return a client without authentication.
if fes.Config.GCPCredentialsPath != "" {
return storage.NewClient(ctx, option.WithCredentialsFile(fes.Config.GCPCredentialsPath))
} else {
return storage.NewClient(ctx, option.WithoutAuthentication())
}
}
func (fes *APIServer) uploadSingleImage(image string, extension string) (_imageURL string, _err error) {
// Set up gcp storage client
ctx := context.Background()
client, err := fes.GetGCSClient(ctx)
if err != nil {
return "", err
}
defer client.Close()
bucketName := fes.Config.GCPBucketName
var dec io.Reader
var imageFileName string
if extension != ".gif" {
var imageBytes []byte
imageBytes, err = resizeAndConvertFromEncodedImageContent(image, 1000)
if err != nil {
return "", err
}
dec = bytes.NewBuffer(imageBytes)
imageFileName = getImageHex(string(imageBytes)) + ".webp"
} else {
dec = base64.NewDecoder(base64.StdEncoding,
strings.NewReader(image))
imageFileName = getImageHex(image) + ".gif"
}
// Create writer and then copy the content of the decoder into the writer.
wc := client.Bucket(bucketName).Object(imageFileName).NewWriter(ctx)
if _, err = io.Copy(wc, dec); err != nil {
return "", err
}
if err = wc.Close(); err != nil {
return "", err
}
return fmt.Sprintf("https://%v/%v", fes.Config.GCPBucketName, imageFileName), nil
}
func getEncodedImageContent(encodedImageString string) string {
return encodedImageString[strings.Index(encodedImageString, ",")+1:]
}
func resizeAndConvertToWebp(encodedImageString string, maxDim uint) (_image []byte, _err error) {
// Extract the relevant portion of the base64 encoded string and process the image.
encodedImageContent := getEncodedImageContent(encodedImageString)
return resizeAndConvertFromEncodedImageContent(encodedImageContent, maxDim)
}
func resizeAndConvertFromEncodedImageContent(encodedImageContent string, maxDim uint) (_image []byte, _err error) {
// always strip metadata
processOptions := bimg.Options{StripMetadata: true}
decodedBytes, err := base64.StdEncoding.DecodeString(encodedImageContent)
imgBytes, err := bimg.NewImage(decodedBytes).Process(processOptions)
if err != nil {
return nil, err
}
img := bimg.NewImage(imgBytes)
// resize the image
resizedImage, err := _resizeImage(img, maxDim)
if err != nil {
return nil, err
}
return resizedImage.Convert(bimg.WEBP)
}
type UploadImageResponse struct {
// Location of the image after upload
ImageURL string
}
// Upload image before submitting post ...
func (fes *APIServer) UploadImage(ww http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 << 20)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: Problem parsing multipart form data: %v", err))
return
}
JWT := req.Form["JWT"]
userPublicKey := req.Form["UserPublicKeyBase58Check"]
if len(JWT) == 0 {
_AddBadRequestError(ww, fmt.Sprintf("No JWT provided"))
return
}
if len(userPublicKey) == 0 {
_AddBadRequestError(ww, fmt.Sprintf("No public key provided"))
return
}
isValid, err := fes.ValidateJWT(userPublicKey[0], JWT[0])
if !isValid {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: Invalid token: %v", err))
return
}
file, fileHeader, err := req.FormFile("file")
if file != nil {
defer file.Close()
}
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: Problem getting file from form data: %v", err))
return
}
if fileHeader.Size > MaxRequestBodySizeBytes {
_AddBadRequestError(ww, fmt.Sprintf("File too large."))
return
}
fileExtension, err := mapMimeTypeToExtension(fileHeader.Header.Get("Content-Type"))
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: problem extracting file extension: %v", err))
return
}
buf := bytes.NewBuffer(nil)
if _, err = io.Copy(buf, file); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: problem copying file to buffer: %v", err))
return
}
encodedFileString := base64.StdEncoding.EncodeToString(buf.Bytes())
imageURL, err := fes.uploadSingleImage(encodedFileString, fileExtension)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: problem uploading image: %v", err))
return
}
// Return all the data associated with the transaction in the response
res := UploadImageResponse{
ImageURL: imageURL,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadImage: Problem encoding response as JSON: %v", err))
return
}
}
func mapMimeTypeToExtension(mimeType string) (string, error) {
switch mimeType {
case "image/gif":
return ".gif", nil
case "image/jpeg":
return ".jpeg", nil
case "image/png":
return ".png", nil
case "image/webp":
return ".webp", nil
}
return "", fmt.Errorf("Mime type not supported: %v", mimeType)
}
// getImageHex ...
func getImageHex(base64EncodedImage string) string {
return hex.EncodeToString(chainhash.HashB([]byte(base64EncodedImage)))
}
// For backwards compatibility, we continue to cast the values of the extra data map to bytes for Post transactions.
func preprocessPostExtraData(extraData map[string]string) map[string][]byte {
extraDataProcessed := make(map[string][]byte)
for k, v := range extraData {
if len(v) > 0 {
extraDataProcessed[k] = []byte(v)
}
}
return extraDataProcessed
}
// All txn types other than Post's should use preprocessExtraData to encode the values of the extra data map.
func preprocessExtraData(extraData map[string]string) map[string][]byte {
if len(extraData) == 0 {
return nil
}
extraDataProcessed := make(map[string][]byte)
for k, v := range extraData {
extraDataProcessed[k] = []byte(v)
}
return extraDataProcessed
}
func extraDataToResponse(extraData map[string][]byte) map[string]string {
if extraData == nil || len(extraData) == 0 {
return nil
}
extraDataResponse := make(map[string]string)
for k, v := range extraData {
extraDataResponse[k] = string(v)
}
return extraDataResponse
}
func _resizeImage(imageObj *bimg.Image, maxDim uint) (_imgObj *bimg.Image, _err error) {
// Get the width and height.
imgSize, err := imageObj.Size()
if err != nil {
return nil, err
}
imgWidth := imgSize.Width
imgHeight := imgSize.Height
// Resize the image based on which side is longer. Doing it this way preserves the
// image's aspect ratio while making sure it isn't too large.
var resizedImageBytes []byte
newWidth := imgWidth
newHeight := imgHeight
if imgWidth > imgHeight {
if newWidth >= int(maxDim) {
newWidth = int(maxDim)
newHeight = int(float64(imgHeight) * float64(newWidth) / float64(imgWidth))
}
} else {
if newHeight >= int(maxDim) {
newHeight = int(maxDim)
newWidth = int(float64(imgWidth) * float64(newHeight) / float64(imgHeight))
}
}
resizedImageBytes, err = imageObj.Resize(newWidth, newHeight)
if err != nil {
return nil, err
}
resizedImage := bimg.NewImage(resizedImageBytes)
return resizedImage, nil
}
type GetFullTikTokURLRequest struct {
TikTokShortVideoID string
}
type GetFullTikTokURLResponse struct {
FullTikTokURL string
}
func (fes *APIServer) GetFullTikTokURL(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := GetFullTikTokURLRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"GetFullTikTokURL: Problem parsing request body: %v", err))
return
}
tiktokURL := fmt.Sprintf("https://vm.tiktok.com/%v", requestData.TikTokShortVideoID)
// Make sure the url matches the TikTok short URL regex.
if !lib.TikTokShortURLRegex.Match([]byte(tiktokURL)) {
_AddBadRequestError(ww, fmt.Sprintf(
"GetFullTikTokURL: TikTokShortVideoURL does not conform to regex: %v", tiktokURL))
return
}
// Create a new HTTP Client, create the request, and perform the GET request.
client := &http.Client{}
req, err := http.NewRequest("GET", tiktokURL, nil)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"GetFullTikTokURL: creating GET request: %v", err))
return
}
resp, err := client.Do(req)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"GetFullTikTokURL: error performing GET request: %v", err))
return
}
// If the response is not a 200 or 302, raise an error.
if resp.StatusCode != 200 && resp.StatusCode != 302 {
_AddBadRequestError(ww, fmt.Sprintf("GetFullTikTokURL: GET request did not return 200 or 302 status code but instead a status code of %v", resp.StatusCode))
return
}
finalURL := resp.Request.URL
// If we didn't get the final destination URL, that is an error.
if finalURL == nil {
_AddBadRequestError(ww, fmt.Sprintf(
"GetFullTikTokURL: response did not include redirected url"))
return
}
// Convert the final URL to a string and verify that it meets the full TikTok URL regex.
fullURL := finalURL.String()
if !lib.TikTokFullURLRegex.Match([]byte(fullURL)) {
_AddBadRequestError(ww, fmt.Sprintf("GetFullTikTokURL: destination url did not conform to tiktok full URL format: %v", fullURL))
return
}
res := GetFullTikTokURLResponse{
FullTikTokURL: fullURL,
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TikTokMobileCurl: Problem encoding response as JSON: %v", err))
return
}
}
// UploadVideo creates a one-time tokenized URL that can be used to upload larger video files using the tus protocol.
// The client uses the Location header in the response from this function to upload the file.
// The client uses the Stream-Media-Id header in the response from cloudflare to understand how to access the file for streaming.
// See Cloudflare documentation here: https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads#using-tus-recommended-for-videos-over-200mb
func (fes *APIServer) UploadVideo(ww http.ResponseWriter, req *http.Request) {
if fes.Config.CloudflareStreamToken == "" || fes.Config.CloudflareAccountId == "" {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: This node is not configured to support video uploads"))
return
}
uploadLengthStr := req.Header.Get("Upload-Length")
if uploadLengthStr == "" {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: Must provide Upload-Length header"))
return
}
uploadLength, err := strconv.Atoi(uploadLengthStr)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: Unable to convert Upload-Length header to int for validation: %v", err))
return
}
if uploadLength > 4*1024*1024*1024 {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: Files must be less than 4GB"))
return
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/accounts/%v/stream?direct_user=true", fes.Config.CloudflareAccountId)
client := &http.Client{}
// Create the request and set relevant headers
request, err := http.NewRequest("POST", url, nil)
// Set Cloudflare token
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", fes.Config.CloudflareStreamToken))
request.Header.Add("Tus-Resumable", "1.0.0")
// Tells Cloudflare expected file size in bytes
request.Header.Add("Upload-Length", req.Header.Get("Upload-Length"))
// Upload-Metadata options are described here: https://developers.cloudflare.com/stream/uploading-videos/upload-video-file#supported-options-in-upload-metadata
request.Header.Add("Upload-Metadata", req.Header.Get("Upload-Metadata"))
// Perform the request
resp, err := client.Do(request)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"UploadVideo: error performing POST request: %v", err))
return
}
if resp.StatusCode != 201 {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: POST request did not return 201 status code but instead a status code of %v", resp.StatusCode))
return
}
// Allow Location and Stream-Media-Id headers so these headers can be used on the client size
ww.Header().Add("Access-Control-Expose-Headers", "Location, Stream-Media-Id")
// The Location header specifies the one-time tokenized URL
ww.Header().Add("Location", resp.Header.Get("Location"))
if ww.Header().Get("Access-Control-Allow-Origin") != "" {
ww.Header().Set("Access-Control-Allow-Origin", "*")
}
if ww.Header().Get("Access-Control-Allow-Headers") != "*" {
ww.Header().Set("Access-Control-Allow-Headers", "*")
}
ww.WriteHeader(200)
}
type CFVideoDetailsResponse struct {
Result map[string]interface{} `json:"result"`
Success bool `json:"success"`
Errors []interface{} `json:"errors"`
Messages []interface{} `json:"messages"`
}
type GetVideoStatusResponse struct {
ReadyToStream bool
Duration float64
Dimensions map[string]interface{}
}
func (fes *APIServer) GetVideoStatus(ww http.ResponseWriter, req *http.Request) {
if fes.Config.CloudflareStreamToken == "" || fes.Config.CloudflareAccountId == "" {
_AddBadRequestError(ww, fmt.Sprintf("UploadVideo: This node is not configured to support video uploads"))
return
}
vars := mux.Vars(req)
videoId, videoIdExists := vars["videoId"]
if !videoIdExists {
_AddBadRequestError(ww, fmt.Sprintf("GetVideoStatus: Missing videoId"))
return
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/accounts/%v/stream/%v", fes.Config.CloudflareAccountId, videoId)
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", fes.Config.CloudflareStreamToken))
request.Header.Add("Content-Type", "application/json")
resp, err := client.Do(request)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"GetVideoStatus: error performing GET request: %v", err))
return
}
if resp.StatusCode != 200 {
_AddBadRequestError(ww, fmt.Sprintf("GetVideoStatus: GET request did not return 200 status code but instead a status code of %v", resp.StatusCode))
return
}
cfVideoDetailsResponse := &CFVideoDetailsResponse{}
if err = json.NewDecoder(resp.Body).Decode(&cfVideoDetailsResponse); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetVideoStatus: failed decoding body: %v", err))
return
}
if err = resp.Body.Close(); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetVideoStatus: failed closing body: %v", err))
return
}
isReady, _ := cfVideoDetailsResponse.Result["readyToStream"]
duration, _ := cfVideoDetailsResponse.Result["duration"]
dimensions, _ := cfVideoDetailsResponse.Result["input"]
res := &GetVideoStatusResponse{
ReadyToStream: isReady.(bool),
Duration: duration.(float64),
Dimensions: dimensions.(map[string]interface{}),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, fmt.Sprintf("GetVideoStatus: Problem serializing object to JSON: %v", err))
return
}
}