Last active
May 22, 2023 06:16
-
-
Save meteozond/b2c07c850fdcc7f41ec065756ccfb2e0 to your computer and use it in GitHub Desktop.
Getting snapshot from motion with nginx openresty content_by_lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
location /snapshot { | |
content_by_lua ' | |
local host = "127.0.0.1" | |
local port = 8001 | |
local sock = ngx.socket.tcp() | |
local ok, err = sock:connect(host, port) | |
bytes, err = sock:send("GET /\\r\\nHost: "..host..":"..port.."\\r\\n\\r\\n") | |
if not ok then | |
ngx.say("Request error ", err) | |
return | |
end | |
ngx.header.content_type = "text/html" | |
if not ok then | |
ngx.say("Connection error ", err) | |
return | |
end | |
local data = "" | |
while true do | |
local chunk, err = sock:receive(256) | |
if err then | |
ngx.say("Reading error ", string.len(data)) | |
return | |
end | |
data = data .. chunk | |
local match = string.match(data, "\\r\\n[-][-]BoundaryString\\r\\n.-\\r\\n\\r\\n(.-)\\r\\n[-][-]BoundaryString\\r\\n") | |
if match then | |
ngx.header.content_type = "image/jpeg" | |
ngx.say(match) | |
return | |
end | |
end | |
'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @imbalind, sorry for delay, I've completely missed your message.
string.len(data) == 0
means that script haven't received any data. Looks like it is problem with your Motion.