Skip to content

Commit

Permalink
Merge pull request #2 from daZepelin/main
Browse files Browse the repository at this point in the history
feat(img): exports to return request result data
  • Loading branch information
itschip authored Jul 25, 2023
2 parents 6d5931b + bd54f2a commit 93b9ac5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```


20 changes: 19 additions & 1 deletion client/cl_exports.lua
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 35 additions & 17 deletions server/sv_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
})
})

Expand All @@ -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)
})
})
}

0 comments on commit 93b9ac5

Please sign in to comment.