-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
437 lines (419 loc) · 18.5 KB
/
index.js
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
'use strict';
const http = require('http');
const https = require('https');
const querystring = require('querystring');
const AWS = require('aws-sdk');
const S3 = new AWS.S3({
signatureVersion: 'v4',
});
const Sharp = require('sharp');
// set the S3 and API GW endpoints
// const BUCKET = "ivs-record"
// const BUCKET = 'ganbei-gtv-test'; // dev
// const BUCKET = 'gtv-mc-videos'; // fat
// const BUCKET = 'gtv-datalake-crawler'; // datalake
// const BUCKET = "gtv-mc-ivs-sit"
// const BUCKET = "gtv-mc-videos-sit"
// const BUCKET = 'aiai-mc-videos-stag'; // staging
// const BUCKET = 'aiai-datalake';
// const BUCKET = 'stag-ivs';
const BUCKET = 'aiai-mc-videos-prod';
const PROGRESS_BAR_PREFIX = 'progressBarThumbnails';
const PREVIEW_PREFIX = 'previewThumbnails';
const generateIvsProgressBarThumbnail = (callback, response, key, progressBarThumbnailMatch, secondsPerImg, imgInterval, imgPerThumbnail, imgPerWidth, imgHeight, liveIsEnded) => {
const ORIGINTHUMBNAILFOLDER = 'thumbnails';
const SECONDSPERIMG = secondsPerImg; // seconds per image
const IMGINTERVAL = imgInterval; // 每多少秒取一张图片
const IMGPERTHUMBNAIL = imgPerThumbnail;
const IMGPERWIDTH = imgPerWidth;
// const IMGPERHEIGHT = 5;
const IMGHEIGHT = imgHeight;
const THUMBNAILTYPE = 'webp';
S3.listObjectsV2(
{
Bucket: BUCKET, /* required */
Prefix: progressBarThumbnailMatch[1] + "/" + ORIGINTHUMBNAILFOLDER + "/",
MaxKeys: 1
},
function(err, data) {
if (err) {
// console.log(err, err.stack); // an error occurred
callback(null, response);
} else {
// console.log(data); // successful response
if (data.Contents.length == 0) {
console.log("No thumbnail found for video: " + progressBarThumbnailMatch[1]);
callback(null, response);
} else {
let thumbnailMatch = data.Contents[0].Key.match(/(.*)\/thumb(.*)\.(.*)/);
let startNumber = parseInt(parseInt(progressBarThumbnailMatch[3]) * IMGPERTHUMBNAIL * IMGINTERVAL / SECONDSPERIMG);
let thumbnailNumsString = [];
for(let i = 0; i < IMGPERTHUMBNAIL; i++) {
let thumbnailNum = parseInt(startNumber + i * IMGINTERVAL / SECONDSPERIMG);
let thumbnailNumString = thumbnailNum.toString();
thumbnailNumsString.push(thumbnailNumString);
}
// console.log('需要的照片:', thumbnailNumsString, thumbnailMatch, thumbnailMatch);
let promises = [];
for(let i = 0; i < thumbnailNumsString.length; i++){
promises.push(
S3.getObject({ Bucket: BUCKET, Key: thumbnailMatch[1] + "/thumb" + thumbnailNumsString[i] + "." + thumbnailMatch[3] }).promise().catch(err => {return 'error';})
)
}
Promise.all(promises).then(originValues => {
let values = []
let if_generate = true
for(let o in originValues){
if(originValues[o] != 'error') {
values.push(originValues[o])
}else{
if (!liveIsEnded){
if_generate = false
}
break;
}
}
// console.log(values.length);
if(values.length > 0 && if_generate) {
let imgOriginBuffers = [];
for(let v in values){
imgOriginBuffers.push(values[v].Body);
}
// 取第一张图片获取长和宽
Sharp(imgOriginBuffers[0]).metadata().then(function(metadata) {
let imgHeight = IMGHEIGHT;
let imgWidth = parseInt(metadata.width * imgHeight / metadata.height);
let resizePromises = []
for(let i = 0; i < imgOriginBuffers.length; i++) {
resizePromises.push(
Sharp(imgOriginBuffers[i]).resize(imgWidth, imgHeight).toBuffer()
)
}
Promise.all(resizePromises).then(resizeBuffers => {
let finalThumbnailWidth = IMGPERWIDTH * imgWidth;
let finalThumbnailHeight = Math.ceil(resizeBuffers.length / IMGPERWIDTH) * imgHeight;
// let needPushNum = IMGPERWIDTH - resizeBuffers.length % IMGPERWIDTH
// for(let i = 0; i < needPushNum; i++) {
// resizeBuffers.push(resizeBuffers[resizeBuffers.length - 1]);
// }
let compositeImages = []
for(let i = 0; i < resizeBuffers.length; i++) {
compositeImages.push({
input: resizeBuffers[i],
left: i % IMGPERWIDTH * imgWidth,
top: parseInt(i / IMGPERWIDTH) * imgHeight
})
}
// console.log('照片位置:', compositeImages);
Sharp({
create: {
width: finalThumbnailWidth,
height: finalThumbnailHeight,
channels: 4,
background: { r: 255, g: 255, b: 255, alpha: 0.5 }
}
})
.composite(compositeImages)
.webp()
.toBuffer()
.then(buffer => {
// save the resized object to S3 bucket with appropriate object key.
S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/' + THUMBNAILTYPE,
CacheControl: 'max-age=31536000',
Key: key,
StorageClass: 'STANDARD'
}).promise()
// even if there is exception in saving the object we send back the generated
// image back to viewer below
.catch(() => { console.log("Exception while writing resized image to bucket") });
// generate a binary response with resized image
response.status = 200;
response.body = buffer.toString('base64');
response.bodyEncoding = 'base64';
response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/' + THUMBNAILTYPE }];
callback(null, response);
})
.catch(err => {
console.log("Exception while reading source image :%s", err);
response.headers['content-type'] = [{ key: 'Content-Type', value: ("Exception while reading source image :%s", err) }];
callback(null, response);
});
}).catch(reason => {
console.log(reason)
callback(null, response);
});
})
} else {
callback(null, response);
}
}).catch(reason => {
console.log(reason)
callback(null, response);
});
}
}
}
)
}
const generateUserUploadProgressBarThumbnail = (callback, response, key, progressBarThumbnailMatch, secondsPerImg, imgInterval, imgPerThumbnail, imgPerWidth, imgHeight) => {
const ORIGINTHUMBNAILFOLDER = 'thumbs'
const SECONDSPERIMG = secondsPerImg; // seconds per image
const IMGINTERVAL = imgInterval; // 每多少秒取一张图片
const IMGPERTHUMBNAIL = imgPerThumbnail;
const IMGPERWIDTH = imgPerWidth;
// const IMGPERHEIGHT = 5;
const IMGHEIGHT = imgHeight;
const THUMBNAILTYPE = 'webp';
S3.listObjectsV2(
{
Bucket: BUCKET, /* required */
Prefix: progressBarThumbnailMatch[1] + "/" + ORIGINTHUMBNAILFOLDER + "/",
MaxKeys: 1
},
function(err, data) {
if (err) {
// console.log(err, err.stack); // an error occurred
callback(null, response);
} else {
// console.log(data); // successful response
if (data.Contents.length == 0) {
console.log("No thumbnail found for video: " + progressBarThumbnailMatch[1]);
callback(null, response);
} else {
let thumbnailMatch = data.Contents[0].Key.match(/(.*)\.(.*)\.(.*)/);
let startNumber = parseInt(parseInt(progressBarThumbnailMatch[3]) * IMGPERTHUMBNAIL * IMGINTERVAL / SECONDSPERIMG);
let thumbnailNumsString = [];
for(let i = 0; i < IMGPERTHUMBNAIL; i++) {
let thumbnailNum = parseInt(startNumber + i * IMGINTERVAL / SECONDSPERIMG);
let thumbnailNumString = thumbnailNum.toString();
let thumbnailNumStringLength = thumbnailNumString.length;
for(let l = 0; l < 7 - thumbnailNumStringLength; l++) {
thumbnailNumString = "0" + thumbnailNumString;
}
thumbnailNumsString.push(thumbnailNumString);
}
// console.log('需要的照片:', thumbnailNumsString);
let promises = [];
for(let i = 0; i < thumbnailNumsString.length; i++){
promises.push(
S3.getObject({ Bucket: BUCKET, Key: thumbnailMatch[1] + "." + thumbnailNumsString[i] + "." + thumbnailMatch[3] }).promise().catch(err => {return 'error';})
)
}
Promise.all(promises).then(originValues => {
let values = []
for(let o in originValues){
if(originValues[o] != 'error') {
values.push(originValues[o])
}else{
break;
}
}
if(values.length > 0) {
let imgOriginBuffers = [];
for(let v in values){
imgOriginBuffers.push(values[v].Body);
}
// 取第一张图片获取长和宽
Sharp(imgOriginBuffers[0]).metadata().then(function(metadata) {
let imgHeight = IMGHEIGHT;
let imgWidth = parseInt(metadata.width * imgHeight / metadata.height);
let resizePromises = []
for(let i = 0; i < imgOriginBuffers.length; i++) {
resizePromises.push(
Sharp(imgOriginBuffers[i]).resize(imgWidth, imgHeight).toBuffer()
)
}
Promise.all(resizePromises).then(resizeBuffers => {
let finalThumbnailWidth = IMGPERWIDTH * imgWidth;
let finalThumbnailHeight = Math.ceil(resizeBuffers.length / IMGPERWIDTH) * imgHeight;
// let needPushNum = IMGPERWIDTH - resizeBuffers.length % IMGPERWIDTH
// for(let i = 0; i < needPushNum; i++) {
// resizeBuffers.push(resizeBuffers[resizeBuffers.length - 1]);
// }
let compositeImages = []
for(let i = 0; i < resizeBuffers.length; i++) {
compositeImages.push({
input: resizeBuffers[i],
left: i % IMGPERWIDTH * imgWidth,
top: parseInt(i / IMGPERWIDTH) * imgHeight
})
}
// console.log('照片位置:', compositeImages);
Sharp({
create: {
width: finalThumbnailWidth,
height: finalThumbnailHeight,
channels: 4,
background: { r: 255, g: 255, b: 255, alpha: 0.5 }
}
})
.composite(compositeImages)
.webp()
.toBuffer()
.then(buffer => {
// save the resized object to S3 bucket with appropriate object key.
S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/' + THUMBNAILTYPE,
CacheControl: 'max-age=31536000',
Key: key,
StorageClass: 'STANDARD'
}).promise()
// even if there is exception in saving the object we send back the generated
// image back to viewer below
.catch(() => { console.log("Exception while writing resized image to bucket") });
// generate a binary response with resized image
response.status = 200;
response.body = buffer.toString('base64');
response.bodyEncoding = 'base64';
response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/' + THUMBNAILTYPE }];
callback(null, response);
})
.catch(err => {
console.log("Exception while reading source image :%s", err);
response.headers['content-type'] = [{ key: 'Content-Type', value: ("Exception while reading source image :%s", err) }];
callback(null, response);
});
}).catch(reason => {
console.log(reason)
callback(null, response);
});
})
} else {
callback(null, response);
}
}).catch(reason => {
console.log(reason)
callback(null, response);
});
}
}
}
);
}
const generateSpecifiedDimensionImg = (callback, response, key) => {
// Ex: key=image/20211230/3/21/36/22766b4eaf3b4b06a53e5d9df80e3607_png_200_200.webp
let originalKey, match, width, height, quality, lossless, requiredFormat, imageName, originFormat;
match = key.match(/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)\.(.*)/);
imageName = match[1];
// correction for jpg required for 'Sharp'
originFormat = match[2];
width = parseInt(match[3], 10);
height = parseInt(match[4], 10);
quality = parseInt(match[5], 10);
lossless = parseInt(match[6], 10);
requiredFormat = match[7] == "jpg" ? "jpeg" : match[7];
originalKey = imageName + "." + originFormat;
console.log(originalKey)
let dimension;
if (width == 0) {
dimension = { height: height, withoutEnlargement: true };
} else {
if (height == 0) {
dimension = { width: width, withoutEnlargement: true };
} else {
dimension = { width: width, height: height, withoutEnlargement: true };
}
}
// console.log(key, originalKey)
let sharp_options;
if (requiredFormat == "webp") {
sharp_options = {
quality: quality,
lossless: lossless == 1 ? true : false,
nearlossless: lossless == 0 ? true : false,
smartSubsample: false
}
// console.log(sharp_options)
} else {
sharp_options = {
quality: quality
}
}
// get the source image file
S3.getObject({ Bucket: BUCKET, Key: originalKey }).promise()
// perform the resize operation
.then(data => Sharp(data.Body)
.resize(dimension)
.toFormat(requiredFormat, sharp_options)
.toBuffer()
)
.then(buffer => {
// save the resized object to S3 bucket with appropriate object key.
S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/' + requiredFormat,
CacheControl: 'max-age=31536000',
Key: key,
StorageClass: 'STANDARD'
}).promise()
// even if there is exception in saving the object we send back the generated
// image back to viewer below
.catch(() => { console.log("Exception while writing resized image to bucket") });
// generate a binary response with resized image
response.status = 200;
response.body = buffer.toString('base64');
response.bodyEncoding = 'base64';
response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/' + requiredFormat }];
callback(null, response);
})
.catch(err => {
console.log("Exception while reading source image :%s", err);
response.headers['content-type'] = [{ key: 'Content-Type', value: ("Exception while reading source image :%s", err) }];
callback(null, response);
});
}
exports.handler = (event, context, callback) => {
let response = event.Records[0].cf.response;
// console.log("Response status code :%s", response.status);
//check if image is not present
if (response.status == 403) {
let request = event.Records[0].cf.request;
let params = querystring.parse(request.querystring);
// read the required path
let path = request.uri;
let key = path.substring(1);
let progressBarThumbnailMatch = key.match(/(.*)\/(.*)\/(.*)\.(.*)/)
if (progressBarThumbnailMatch && progressBarThumbnailMatch[2] && progressBarThumbnailMatch[2] == PROGRESS_BAR_PREFIX) {
if (key.substring(0, 4) == "ivs/") {
// 判断直播是否结束
const live_end_json = key.match(/(.*)\/(.*)\/(.*)\/(.*)\.(.*)/)[1] + "/events/recording-ended.json"
S3.getObject({Bucket: BUCKET, Key: live_end_json}).promise().then(() => {
generateIvsProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 90, true); //ivs thumbnails
}).catch(() => {
generateIvsProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 90, false);
})
} else {
generateUserUploadProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 90); //UserUpload thumbnails
}
} else {
if (progressBarThumbnailMatch && progressBarThumbnailMatch[2] && progressBarThumbnailMatch[2] == PREVIEW_PREFIX) {
if (key.substring(0, 4) == "ivs/") {
const live_end_json = key.match(/(.*)\/(.*)\/(.*)\/(.*)\.(.*)/)[1] + "/events/recording-ended.json"
S3.getObject({Bucket: BUCKET, Key: live_end_json}).promise().then(() => {
generateIvsProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 253, true); //ivs thumbnails
}).catch(() => {
generateIvsProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 253, false);
})
} else {
generateUserUploadProgressBarThumbnail(callback, response, key, progressBarThumbnailMatch, 10, 10, 25, 5, 253); //UserUpload thumbnails
}
} else {
if (params.d || params.w || params.h){
generateSpecifiedDimensionImg(callback, response, key)
} else {
callback(null, response);
}
}
}
} // end of if block checking response statusCode
else {
// allow the response to pass through
callback(null, response);
}
};