diff --git a/README.md b/README.md index 1975746..c3c3527 100644 --- a/README.md +++ b/README.md @@ -20,28 +20,32 @@ You can now call events or exports. ### Client Exports ```lua -exports.fivemanage_lib:takeImage() +local imageData = exports.fivemanage_lib:takeImage() -- With metadata -exports.fivemanage_lib:takeImage({ +local imageData = exports.fivemanage_lib:takeImage({ name = "My image", description = "This is my image", -- or any other field you want }) + +print(image.url) ``` ### Server Exports ```lua -exports.fivemanage_lib:takeServerImage(src) +local imageData = exports.fivemanage_lib:takeServerImage(src) -- With metadata -exports.fivemanage_lib:takeServerImage(src, { +local imageData = exports.fivemanage_lib:takeServerImage(src, { name = "My image", description = "This is my image", -- or any other field you want }) + +print(image.url) ``` diff --git a/client/cl_exports.lua b/client/cl_exports.lua index 75d25a4..b1fbe79 100644 --- a/client/cl_exports.lua +++ b/client/cl_exports.lua @@ -1,3 +1,21 @@ +local ImageRequestId = 0 + exports("takeImage", function(metadata) - TriggerServerEvent("fivemanage:server:takeImage", metadata) + local p = promise.new() + TriggerServerEvent("fivemanage:server:takeImage", metadata, ImageRequestId) + + local eventHandler = RegisterNetEvent('fivemanage:client:receiveImageCallback:'..ImageRequestId, function(res) + p:resolve(res) + end) + + SetTimeout(10000, function() + p:resolve(nil) + end) + + local imageData = Citizen.Await(p) + + RemoveEventHandler(eventHandler) + ImageRequestId = ImageRequestId + 1 + + return imageData end) diff --git a/server/sv_image.js b/server/sv_image.js index 2cfbf06..32e12d1 100644 --- a/server/sv_image.js +++ b/server/sv_image.js @@ -3,24 +3,35 @@ import fetch from 'node-fetch' const API_URL = "https://api.fivemanage.com/api/image" -exports('takeServerImage', (src, metadata) => { +exports('takeServerImage', async (src, metadata) => { if (!src) throw new Error("Player Id provided is not valid") - exports['screenshot-basic'].requestClientScreenshot(src, { - encoding: 'png', - quality: 0.85, - }, (_, data) => { - uploadImage(data, metadata) + + return await new Promise((resolve, reject) => { + exports['screenshot-basic'].requestClientScreenshot(src, { + encoding: 'png', + quality: 0.85, + }, (_, data) => { + uploadImage(data, metadata).then(res => { + resolve(res) + }) + }) }) }) -onNet('fivemanage:server:takeImage', (metadata) => { +onNet('fivemanage:server:takeImage', (metadata, requestId) => { const src = global.source; exports['screenshot-basic'].requestClientScreenshot(src, { encoding: 'png', quality: 0.85, }, (_, data) => { - uploadImage(data, metadata) + uploadImage(data, metadata).then(result => { + emitNet('fivemanage:client:receiveImageCallback:'+requestId, src, result) + setTimeout(() => { + emitNet('fivemanage:client:receiveImageCallback:'+requestId, src, result) + + }, 1000); + }) }) }) @@ -42,14 +53,21 @@ function uploadImage(data, metadata) { form.append('metadata', JSON.stringify(metadata)) } - fetch(API_URL, { - method: 'POST', - body: form, - headers: { - 'Authorization': apiToken - } - }) - .catch(err => { - console.log(err) + return new Promise((resolve, reject) => { + fetch(API_URL, { + method: 'POST', + body: form, + headers: { + 'Authorization': apiToken + } + }) + .then(res => res.json()) + .then(json => { + resolve(json) + }) + .catch(err => { + console.log(err) + reject(err) + }) }) } \ No newline at end of file