@@ -290,67 +290,98 @@ function handleText(message, replyToken, source) {
290
290
}
291
291
292
292
function handleImage ( message , replyToken ) {
293
- const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .jpg` ) ;
294
- const previewPath = path . join ( __dirname , 'downloaded' , `${ message . id } -preview.jpg` ) ;
295
-
296
- return downloadContent ( message . id , downloadPath )
297
- . then ( ( downloadPath ) => {
298
- // ImageMagick is needed here to run 'convert'
299
- // Please consider about security and performance by yourself
300
- cp . execSync ( `convert -resize 240x jpeg:${ downloadPath } jpeg:${ previewPath } ` ) ;
293
+ let getContent ;
294
+ if ( message . contentProvider . type === "line" ) {
295
+ const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .jpg` ) ;
296
+ const previewPath = path . join ( __dirname , 'downloaded' , `${ message . id } -preview.jpg` ) ;
297
+
298
+ getContent = downloadContent ( message . id , downloadPath )
299
+ . then ( ( downloadPath ) => {
300
+ // ImageMagick is needed here to run 'convert'
301
+ // Please consider about security and performance by yourself
302
+ cp . execSync ( `convert -resize 240x jpeg:${ downloadPath } jpeg:${ previewPath } ` ) ;
303
+
304
+ return {
305
+ originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
306
+ previewImageUrl : baseURL + '/downloaded/' + path . basename ( previewPath ) ,
307
+ } ;
308
+ } ) ;
309
+ } else if ( message . contentProvider . type === "external" ) {
310
+ getContent = Promise . resolve ( message . contentProvider ) ;
311
+ }
301
312
313
+ return getContent
314
+ . then ( ( { originalContentUrl, previewImageUrl } ) => {
302
315
return client . replyMessage (
303
316
replyToken ,
304
317
{
305
318
type : 'image' ,
306
- originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
307
- previewImageUrl : baseURL + '/downloaded/' + path . basename ( previewPath ) ,
319
+ originalContentUrl,
320
+ previewImageUrl,
308
321
}
309
322
) ;
310
323
} ) ;
311
324
}
312
325
313
326
function handleVideo ( message , replyToken ) {
314
- const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .mp4` ) ;
315
- const previewPath = path . join ( __dirname , 'downloaded' , `${ message . id } -preview.jpg` ) ;
316
-
317
- return downloadContent ( message . id , downloadPath )
318
- . then ( ( downloadPath ) => {
319
- // FFmpeg and ImageMagick is needed here to run 'convert'
320
- // Please consider about security and performance by yourself
321
- cp . execSync ( `convert mp4:${ downloadPath } [0] jpeg:${ previewPath } ` ) ;
327
+ let getContent ;
328
+ if ( message . contentProvider . type === "line" ) {
329
+ const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .mp4` ) ;
330
+ const previewPath = path . join ( __dirname , 'downloaded' , `${ message . id } -preview.jpg` ) ;
331
+
332
+ getContent = downloadContent ( message . id , downloadPath )
333
+ . then ( ( downloadPath ) => {
334
+ // FFmpeg and ImageMagick is needed here to run 'convert'
335
+ // Please consider about security and performance by yourself
336
+ cp . execSync ( `convert mp4:${ downloadPath } [0] jpeg:${ previewPath } ` ) ;
337
+
338
+ return {
339
+ originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
340
+ previewImageUrl : baseURL + '/downloaded/' + path . basename ( previewPath ) ,
341
+ }
342
+ } ) ;
343
+ } else if ( message . contentProvider . type === "external" ) {
344
+ getContent = Promise . resolve ( message . contentProvider ) ;
345
+ }
322
346
347
+ return getContent
348
+ . then ( ( { originalContentUrl, previewImageUrl } ) => {
323
349
return client . replyMessage (
324
350
replyToken ,
325
351
{
326
352
type : 'video' ,
327
- originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
328
- previewImageUrl : baseURL + '/downloaded/' + path . basename ( previewPath ) ,
353
+ originalContentUrl,
354
+ previewImageUrl,
329
355
}
330
356
) ;
331
357
} ) ;
332
358
}
333
359
334
360
function handleAudio ( message , replyToken ) {
335
- const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .m4a` ) ;
336
-
337
- return downloadContent ( message . id , downloadPath )
338
- . then ( ( downloadPath ) => {
339
- var getDuration = require ( 'get-audio-duration' ) ;
340
- var audioDuration ;
341
- getDuration ( downloadPath )
342
- . then ( ( duration ) => { audioDuration = duration ; } )
343
- . catch ( ( error ) => { audioDuration = 1 ; } )
344
- . finally ( ( ) => {
345
- return client . replyMessage (
346
- replyToken ,
347
- {
348
- type : 'audio' ,
349
- originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
350
- duration : audioDuration * 1000 ,
351
- }
352
- ) ;
353
- } ) ;
361
+ let getContent ;
362
+ if ( message . contentProvider . type === "line" ) {
363
+ const downloadPath = path . join ( __dirname , 'downloaded' , `${ message . id } .m4a` ) ;
364
+
365
+ getContent = downloadContent ( message . id , downloadPath )
366
+ . then ( ( downloadPath ) => {
367
+ return {
368
+ originalContentUrl : baseURL + '/downloaded/' + path . basename ( downloadPath ) ,
369
+ } ;
370
+ } ) ;
371
+ } else {
372
+ getContent = Promise . resolve ( message . contentProvider ) ;
373
+ }
374
+
375
+ return getContent
376
+ . then ( ( { originalContentUrl } ) => {
377
+ return client . replyMessage (
378
+ replyToken ,
379
+ {
380
+ type : 'audio' ,
381
+ originalContentUrl,
382
+ duration : message . duration ,
383
+ }
384
+ ) ;
354
385
} ) ;
355
386
}
356
387
0 commit comments