Update 19.11.2022
This commit is contained in:
80
HTML/gcphone/resources/screenshot-basic/src/client/client.ts
Normal file
80
HTML/gcphone/resources/screenshot-basic/src/client/client.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
const exp = (<any>global).exports;
|
||||
|
||||
RegisterNuiCallbackType('screenshot_created');
|
||||
|
||||
class ResultData {
|
||||
cb: (data: string) => void;
|
||||
}
|
||||
|
||||
const results: {[id: string]: ResultData} = {};
|
||||
let correlationId = 0;
|
||||
|
||||
function registerCorrelation(cb: (result: string) => void) {
|
||||
const id = correlationId.toString();
|
||||
|
||||
results[id] = { cb };
|
||||
|
||||
correlationId++;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
on('__cfx_nui:screenshot_created', (body: any, cb: (arg: any) => void) => {
|
||||
cb(true);
|
||||
|
||||
if (body.id !== undefined && results[body.id]) {
|
||||
results[body.id].cb(body.data);
|
||||
delete results[body.id];
|
||||
}
|
||||
});
|
||||
|
||||
exp('requestScreenshot', (options: any, cb: (result: string) => void) => {
|
||||
const realOptions = (cb !== undefined) ? options : {
|
||||
encoding: 'jpg'
|
||||
};
|
||||
|
||||
const realCb = (cb !== undefined) ? cb : options;
|
||||
|
||||
realOptions.resultURL = null;
|
||||
realOptions.targetField = null;
|
||||
realOptions.targetURL = `http://${GetCurrentResourceName()}/screenshot_created`;
|
||||
|
||||
realOptions.correlation = registerCorrelation(realCb);
|
||||
|
||||
SendNuiMessage(JSON.stringify({
|
||||
request: realOptions
|
||||
}));
|
||||
});
|
||||
|
||||
exp('requestScreenshotUpload', (url: string, field: string, options: any, cb: (result: string) => void) => {
|
||||
const realOptions = (cb !== undefined) ? options : {
|
||||
headers: {},
|
||||
encoding: 'jpg'
|
||||
};
|
||||
|
||||
const realCb = (cb !== undefined) ? cb : options;
|
||||
|
||||
realOptions.targetURL = url;
|
||||
realOptions.targetField = field;
|
||||
realOptions.resultURL = `http://${GetCurrentResourceName()}/screenshot_created`;
|
||||
|
||||
realOptions.correlation = registerCorrelation(realCb);
|
||||
|
||||
SendNuiMessage(JSON.stringify({
|
||||
request: realOptions
|
||||
}));
|
||||
});
|
||||
|
||||
onNet('screenshot_basic:requestScreenshot', (options: any, url: string) => {
|
||||
options.encoding = options.encoding || 'jpg';
|
||||
|
||||
options.targetURL = `http://${GetCurrentServerEndpoint()}${url}`;
|
||||
options.targetField = 'file';
|
||||
options.resultURL = null;
|
||||
|
||||
options.correlation = registerCorrelation(() => {});
|
||||
|
||||
SendNuiMessage(JSON.stringify({
|
||||
request: options
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./",
|
||||
"noImplicitAny": true,
|
||||
"module": "es6",
|
||||
"target": "es6",
|
||||
"allowJs": true,
|
||||
"lib": ["es2017"],
|
||||
"types": ["@citizenfx/server", "@citizenfx/client", "node"],
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": [
|
||||
"./**/*"
|
||||
],
|
||||
"exclude": [
|
||||
|
||||
]
|
||||
}
|
||||
96
HTML/gcphone/resources/screenshot-basic/src/server/server.ts
Normal file
96
HTML/gcphone/resources/screenshot-basic/src/server/server.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { setHttpCallback } from '@citizenfx/http-wrapper';
|
||||
|
||||
import { v4 } from 'uuid';
|
||||
import * as fs from 'fs';
|
||||
import * as Koa from 'koa';
|
||||
import * as Router from 'koa-router';
|
||||
import * as koaBody from 'koa-body';
|
||||
import * as mv from 'mv';
|
||||
import { File } from 'formidable';
|
||||
|
||||
const app = new Koa();
|
||||
const router = new Router();
|
||||
|
||||
class UploadData {
|
||||
fileName: string;
|
||||
|
||||
cb: (err: string | boolean, data: string) => void;
|
||||
}
|
||||
|
||||
const uploads: { [token: string]: UploadData } = {};
|
||||
|
||||
router.post('/upload/:token', async (ctx) => {
|
||||
const tkn: string = ctx.params['token'];
|
||||
|
||||
ctx.response.append('Access-Control-Allow-Origin', '*');
|
||||
ctx.response.append('Access-Control-Allow-Methods', 'GET, POST');
|
||||
|
||||
if (uploads[tkn] !== undefined) {
|
||||
const upload = uploads[tkn];
|
||||
delete uploads[tkn];
|
||||
|
||||
const finish = (err: string, data: string) => {
|
||||
setImmediate(() => {
|
||||
upload.cb(err || false, data);
|
||||
});
|
||||
}
|
||||
|
||||
const f = ctx.request.files['file'] as File;
|
||||
|
||||
if (f) {
|
||||
if (upload.fileName) {
|
||||
mv(f.path, upload.fileName, (err) => {
|
||||
if (err) {
|
||||
finish(err.message, null);
|
||||
return;
|
||||
}
|
||||
|
||||
finish(null, upload.fileName);
|
||||
});
|
||||
} else {
|
||||
fs.readFile(f.path, (err, data) => {
|
||||
if (err) {
|
||||
finish(err.message, null);
|
||||
return;
|
||||
}
|
||||
|
||||
fs.unlink(f.path, (err) => {
|
||||
finish(null, `data:${f.type};base64,${data.toString('base64')}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ctx.body = { success: true };
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.body = { success: false };
|
||||
});
|
||||
|
||||
app.use(koaBody({
|
||||
patchKoa: true,
|
||||
multipart: true,
|
||||
}))
|
||||
.use(router.routes())
|
||||
.use(router.allowedMethods());
|
||||
|
||||
setHttpCallback(app.callback());
|
||||
|
||||
// Cfx stuff
|
||||
const exp = (<any>global).exports;
|
||||
|
||||
exp('requestClientScreenshot', (player: string | number, options: any, cb: (err: string | boolean, data: string) => void) => {
|
||||
const tkn = v4();
|
||||
|
||||
const fileName = options.fileName;
|
||||
delete options['fileName']; // so the client won't get to know this
|
||||
|
||||
uploads[tkn] = {
|
||||
fileName,
|
||||
cb
|
||||
};
|
||||
|
||||
emitNet('screenshot_basic:requestScreenshot', player, options, `/${GetCurrentResourceName()}/upload/${tkn}`);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": { "*": ["types/*"] },
|
||||
"outDir": "./",
|
||||
"noImplicitAny": false,
|
||||
"module": "es6",
|
||||
"target": "es6",
|
||||
"allowJs": true,
|
||||
"lib": ["es2017"],
|
||||
"types": ["@citizenfx/server", "@citizenfx/client", "node"],
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": [
|
||||
"./**/*"
|
||||
],
|
||||
"exclude": [
|
||||
|
||||
]
|
||||
}
|
||||
1
HTML/gcphone/resources/screenshot-basic/src/server/types/@citizenfx/http-wrapper.d.ts
vendored
Normal file
1
HTML/gcphone/resources/screenshot-basic/src/server/types/@citizenfx/http-wrapper.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export function setHttpCallback(requestHandler: any): void;
|
||||
Reference in New Issue
Block a user