1+ import { request } from 'http' ;
12import { netEventController } from './event' ;
23import { CaptureRequest , RequestScreenshotUploadCB , ScreenshotCreatedBody } from './types' ;
3- import { uuidv4 } from './utils' ;
4-
5- RegisterNuiCallbackType ( 'screenshot_created' ) ;
4+ import { exportHandler , uuidv4 } from './utils' ;
65
76const clientCaptureMap = new Map < string , RequestScreenshotUploadCB > ( ) ;
87
8+ RegisterNuiCallbackType ( 'screenshot_created' ) ;
9+
910onNet ( 'screencapture:captureScreen' , ( token : string , options : object , dataType : string ) => {
1011 SendNUIMessage ( {
1112 ...options ,
@@ -38,6 +39,45 @@ on('__cfx_nui:screenshot_created', (body: ScreenshotCreatedBody, cb: (arg: any)
3839 }
3940} ) ;
4041
42+ async function requestScreenshotUpload (
43+ url : string ,
44+ formField : string ,
45+ optionsOrCB : CaptureRequest | RequestScreenshotUploadCB ,
46+ callback : RequestScreenshotUploadCB ,
47+ ) {
48+ // forgive me
49+ const isOptions = typeof optionsOrCB === 'object' && optionsOrCB !== null ;
50+ const realOptions = isOptions
51+ ? ( optionsOrCB as CaptureRequest )
52+ : ( { headers : { } , encoding : 'webp' } as CaptureRequest ) ;
53+ const realCallback = isOptions
54+ ? ( callback as RequestScreenshotUploadCB )
55+ : ( optionsOrCB as RequestScreenshotUploadCB ) ;
56+
57+ const correlationId = uuidv4 ( ) ;
58+ clientCaptureMap . set ( correlationId , realCallback ) ;
59+
60+ const token = await netEventController < string > ( 'screencapture:INTERNAL_requestUploadToken' , {
61+ ...realOptions ,
62+ formField,
63+ url,
64+ correlationId,
65+ } ) ;
66+
67+ if ( ! token ) {
68+ return console . error ( 'Failed to get upload token' ) ;
69+ }
70+
71+ return createImageCaptureMessage ( {
72+ ...realOptions ,
73+ formField,
74+ url,
75+ uploadToken : token ,
76+ dataType : 'blob' ,
77+ } ) ;
78+ }
79+
80+ exportHandler ( 'requestScreenshotUpload' , requestScreenshotUpload ) ;
4181global . exports (
4282 'requestScreenshotUpload' ,
4383 async (
@@ -46,41 +86,11 @@ global.exports(
4686 optionsOrCB : CaptureRequest | RequestScreenshotUploadCB ,
4787 callback : RequestScreenshotUploadCB ,
4888 ) => {
49- // forgive me
50- const isOptions = typeof optionsOrCB === 'object' && optionsOrCB !== null ;
51- const realOptions = isOptions
52- ? ( optionsOrCB as CaptureRequest )
53- : ( { headers : { } , encoding : 'webp' } as CaptureRequest ) ;
54- const realCallback = isOptions
55- ? ( callback as RequestScreenshotUploadCB )
56- : ( optionsOrCB as RequestScreenshotUploadCB ) ;
57-
58- const correlationId = uuidv4 ( ) ;
59- clientCaptureMap . set ( correlationId , realCallback ) ;
60-
61- const token = await netEventController < string > ( 'screencapture:INTERNAL_requestUploadToken' , {
62- ...realOptions ,
63- formField,
64- url,
65- correlationId,
66- } ) ;
67-
68- if ( ! token ) {
69- return console . error ( 'Failed to get upload token' ) ;
70- }
71-
72- return createImageCaptureMessage ( {
73- ...realOptions ,
74- formField,
75- url,
76- uploadToken : token ,
77- dataType : 'blob' ,
78- } ) ;
79- } ,
89+ return await requestScreenshotUpload ( url , formField , optionsOrCB , callback ) ;
90+ }
8091) ;
8192
82- // returns base64 data URI
83- global . exports ( 'requestScreenshot' , ( options : CaptureRequest , callback : RequestScreenshotUploadCB ) => {
93+ function requestScreenshot ( options : CaptureRequest , callback : RequestScreenshotUploadCB ) {
8494 const correlationId = uuidv4 ( ) ;
8595
8696 const realOptions = ( callback !== undefined ) ? options : {
@@ -100,6 +110,11 @@ global.exports('requestScreenshot', (options: CaptureRequest, callback: RequestS
100110 callbackUrl : `http://${ GetCurrentResourceName ( ) } /screenshot_created` ,
101111 correlationId,
102112 } ) ;
113+ }
114+
115+ exportHandler ( 'requestScreenshot' , requestScreenshot ) ;
116+ global . exports ( 'requestScreenshot' , ( options : CaptureRequest , callback : RequestScreenshotUploadCB ) => {
117+ return requestScreenshot ( options , callback ) ;
103118} ) ;
104119
105120function createImageCaptureMessage ( options : CaptureRequest ) {
0 commit comments