1+ import fetch from "node-fetch" ;
12import { Page } from "puppeteer-core" ;
23import BaseCommand from "../BaseCommand" ;
34import BotChecker from "../BotChecker" ;
5+ import TempFileService from "../TempFileService" ;
46
57export default class FetchPreview extends BaseCommand {
68
@@ -9,33 +11,47 @@ export default class FetchPreview extends BaseCommand {
911 const params = event ;
1012
1113 const {
12- botCheck,
1314 botUserAgent,
1415 url
1516 } = params ;
1617
17- if ( botCheck ) {
18- const { canCrawl , content } = await BotChecker . check ( url , botUserAgent ) ;
19- delete params . url ;
20- params . content = content ;
21- // disable all resources...
22- params . html = true ;
23- if ( ! canCrawl ) {
24- console . log ( "Bot denied succeeded" ) ;
25- } else {
26- console . log ( "Bot check succeeded" ) ;
27- }
18+ const { canCrawl , content } = await BotChecker . check ( url , botUserAgent ) ;
19+ delete params . url ;
20+ params . content = content ;
21+ // disable all resources...
22+ params . html = true ;
23+ if ( ! canCrawl ) {
24+ console . log ( "Bot denied succeeded" ) ;
25+ } else {
26+ console . log ( "Bot check succeeded" ) ;
2827 }
2928
3029 return await this . onSave ( params ) ;
3130 }
3231
33- protected onBeforeRender ( timeout : number , page : Page , stopTest : any ) {
32+ protected onBeforeRender ( ) {
3433 return Promise . resolve ( ) ;
3534 }
3635
37- protected async onRender ( { html, pdf, video, output } ) {
38- return { } ;
36+ protected async onRender ( { output } ) {
37+ let document : any ;
38+ let url = await this . page . evaluate ( ( ) => document . head . querySelector ( `meta[property="og:image"]` ) ?. content ) ;
39+ if ( url ) {
40+ url = await this . page . evaluate ( ( ) => document . head . querySelector ( `img` ) ?. src ) ;
41+ }
42+
43+ const file = await TempFileService . getTempFile ( ".jpg" ) ;
44+ await TempFileService . fetch ( url , file . path ) ;
45+
46+ await this . upload ( { url : output , filePath : file . path } ) ;
47+
48+ return {
49+ statusCode : 200 ,
50+ body : JSON . stringify ( url ) ,
51+ headers : {
52+ "content-type" : "application/json"
53+ }
54+ } ;
3955 }
4056
4157}
0 commit comments