Archived
Private
Public Access
1
0

Update 19.11.2022

This commit is contained in:
2022-11-19 14:12:22 +01:00
parent 08ddca2211
commit 771f58073f
322 changed files with 50685 additions and 2 deletions

View 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
}));
});

View File

@@ -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": [
]
}

View 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}`);
});

View File

@@ -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": [
]
}

View File

@@ -0,0 +1 @@
export function setHttpCallback(requestHandler: any): void;