11import Koa from 'koa' ;
22import Router from '@koa/router' ;
3+ import { writeFileSync } from 'fs' ;
34
45// @ts -ignore - no types
56import { setHttpCallback } from '@citizenfx/http-wrapper' ;
@@ -30,7 +31,7 @@ export async function createServer(uploadStore: UploadStore) {
3031 ctx . response . append ( 'Access-Control-Allow-Origin' , '*' ) ;
3132 ctx . response . append ( 'Access-Control-Allow-Methods' , 'GET, POST' ) ;
3233
33- const { callback, dataType, isRemote, remoteConfig, url, playerSource, correlationId } =
34+ const { callback, dataType, isRemote, remoteConfig, url, playerSource, correlationId, screenshotBasicCompatibility } =
3435 uploadStore . getUpload ( token ) ;
3536
3637 if ( ! ctx . files ) {
@@ -41,30 +42,55 @@ export async function createServer(uploadStore: UploadStore) {
4142 const file = ctx . file ;
4243
4344 try {
44- // Get encoding from remoteConfig or default to 'webp'
4545 const encoding = remoteConfig ?. encoding || 'webp' ;
46+ // base64 or buffer
4647 const buf = await buffer ( dataType , file . buffer , encoding ) ;
4748
4849 if ( isRemote ) {
4950 const response = await uploadFile ( url , remoteConfig , buf , dataType ) ;
5051
51- // this is only when we return data back to the client
52- if ( playerSource && correlationId ) {
53- callback ( response , playerSource , correlationId ) ;
52+ if ( screenshotBasicCompatibility ) {
53+ ( callback as any ) ( false , response ) ;
5454 } else {
55- callback ( response ) ;
55+ if ( playerSource && correlationId ) {
56+ ( callback as any ) ( response , playerSource , correlationId ) ;
57+ } else {
58+ ( callback as any ) ( response ) ;
59+ }
5660 }
5761 } else {
58- callback ( buf ) ;
62+ if ( screenshotBasicCompatibility ) {
63+ // this will be a base64 string
64+ if ( remoteConfig ?. fileName ) {
65+ const filename = saveFileToDisk ( remoteConfig . fileName , buf ) ;
66+ ( callback as any ) ( false , filename ) ;
67+ } else {
68+ ( callback as any ) ( false , buf ) ;
69+ }
70+ } else {
71+ ( callback as any ) ( buf ) ;
72+ }
5973 }
6074
6175 ctx . status = 200 ;
6276 ctx . body = { status : 'success' } ;
6377 } catch ( err ) {
6478 if ( err instanceof Error ) {
79+ if ( screenshotBasicCompatibility ) {
80+ ( callback as any ) ( err . message , null ) ;
81+ } else {
82+ ( callback as any ) ( err ) ;
83+ }
84+
6585 ctx . status = 500 ;
6686 ctx . body = { status : 'error' , message : err . message } ;
6787 } else {
88+ if ( screenshotBasicCompatibility ) {
89+ ( callback as any ) ( 'An unknown error occurred' , null ) ;
90+ } else {
91+ ( callback as any ) ( new Error ( 'An unknown error occurred' ) ) ;
92+ }
93+
6894 ctx . status = 500 ;
6995 ctx . body = { status : 'error' , message : 'An unknown error occurred' } ;
7096 }
@@ -195,3 +221,13 @@ function getMimeType(encoding: string): string {
195221 return 'image/webp' ;
196222 }
197223}
224+
225+ function saveFileToDisk ( fileName : string , data : string | Buffer ) {
226+ try {
227+ writeFileSync ( fileName , data ) ;
228+ return fileName ;
229+ } catch ( err ) {
230+ console . error ( 'Error saving file to disk:' , err ) ;
231+ throw new Error ( 'Error saving file to disk' ) ;
232+ }
233+ }
0 commit comments