Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
121 lines (97 loc) · 3.27 KB
/
Copy pathserver.lua
File metadata and controls
121 lines (97 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
local resourceName = GetCurrentResourceName()
local timeoutMs = 45000
local maxFileSize = 10 * 1024 * 1024
local extensions = { ['image/jpeg'] = 'jpg', ['image/png'] = 'png', ['image/webp'] = 'webp', ['video/webm'] = 'webm' }
local pending = {}
local nextId = 0
local function parseDataUri(data)
local mime, pos = data:match('^data:(%a+/%a+);base64,()')
if extensions[mime] then
return mime, pos
end
end
local function saveFile(fileName, bytes)
if type(fileName) ~= 'string' then
return nil, 'Invalid target path'
end
local rel = fileName:gsub('\\', '/'):gsub('^/+', '')
if rel == '' or rel:find(':', 1, true) or rel:sub(1, 1) == '@' then
return nil, 'Invalid target path'
end
for segment in rel:gmatch('[^/]+') do
if segment == '..' then
return nil, 'Invalid target path'
end
end
local dir = ''
for segment in rel:gmatch('([^/]+)/') do
dir = dir .. segment .. '/'
os.createdir(('@%s/%s'):format(resourceName, dir))
end
if not SaveResourceFile(resourceName, rel, bytes, #bytes) then
return nil, 'Failed to write file'
end
return GetResourcePath(resourceName) .. '/' .. rel
end
local function finish(request, data)
if type(data) ~= 'string' or data == '' then
return request.cb('Screenshot failed', '')
end
if #data * 3 / 4 > maxFileSize then
return request.cb('File too large', '')
end
local _, pos = parseDataUri(data)
if not pos then
return request.cb('Invalid file type', '')
end
if not request.fileName then
return request.cb(false, data)
end
local bytes = Base64Decode(data:sub(pos))
if not bytes then
return request.cb('Invalid image data', '')
end
local path, err = saveFile(request.fileName, bytes)
if not path then
return request.cb(err, '')
end
request.cb(false, path)
end
local function requestClient(player, fileName, cb, payload)
nextId = nextId + 1
local id = nextId
pending[id] = { player = tonumber(player), fileName = fileName, cb = cb }
SetTimeout(timeoutMs + (payload.duration or 0) * 1000, function()
local request = pending[id]
if request then
pending[id] = nil
request.cb('Upload timeout', '')
end
end)
TriggerClientEvent('screenshot_basic:request', player, id, payload)
end
exports('requestClientScreenshot', function(player, options, cb)
options = type(options) == 'table' and options or {}
requestClient(player, options.fileName, cb, {
encoding = options.encoding,
quality = options.quality
})
end)
exports('requestClientVideo', function(player, options, cb)
options = type(options) == 'table' and options or {}
requestClient(player, options.fileName, cb, {
video = true,
duration = math.min(math.max(tonumber(options.duration) or 5, 1), 15),
fps = options.fps,
bitrate = options.bitrate
})
end)
RegisterNetEvent('screenshot_basic:result', function(requestId, data)
local src = tonumber(source)
local request = type(requestId) == 'number' and pending[requestId]
if not request or request.player ~= src then
return
end
pending[requestId] = nil
finish(request, data)
end)