openresty-config/lua/unused/youtube-image-proxy.lua
2025-02-11 20:13:49 -03:00

114 lines
3.1 KiB
Lua

local req = require("reqwest")
local http = require "resty.http"
-- local res, err = req.request("https://i.ytimg.com/" .. path, { headers = { ["User-Agent"] = "" }, version = 2 })
-- if err ~= nil then
-- ngx.status = 500
-- ngx.say(err)
-- end
--
-- ngx.print(res.body)
-- your_script.lua
-- local function blocking_io_operation()
-- local path = ngx.var.uri
-- local res, err = req.request("https://i.ytimg.com/" .. path, { headers = { ["User-Agent"] = "" }, version = 2 })
-- if err ~= nil then
-- ngx.status = 500
-- ngx.say(err)
-- end
-- return res
-- end
--
local function blocking_io_operation()
local httpc = http.new()
local path = ngx.var.uri
-- local res, err = req.request("https://i.ytimg.com/" .. path, { headers = { ["User-Agent"] = "" }, version = 2 })
-- if err ~= nil then
-- ngx.status = 500
-- ngx.say(err)
-- end
-- return res
local res, err = httpc:request_uri("https://i.ytimg.com/" .. path, {
method = "GET",
headers = {
["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0",
}
})
-- httpc:close()
if err ~= nil then
ngx.status = 500
ngx.say(err)
end
ngx.print(res.body)
-- return res.body
end
--
-- local function main()
-- -- Spawn a new thread to handle the blocking operation
-- local co = ngx.thread.spawn(blocking_io_operation)
--
-- -- Do other processing here if needed
--
-- -- Wait for the thread to finish and retrieve the result
-- local ok, result = ngx.thread.wait(co)
--
-- if not ok then
-- ngx.say("Error: ", result)
-- return
-- end
--
-- ngx.print(result.body)
-- end
-- main()
blocking_io_operation()
--
-- local req = require("reqwest")
--
-- local path = ngx.var.uri
--
-- -- Function to perform the request
-- local function perform_request()
-- local res, err = req.request("https://i.ytimg.com/" .. path, { headers = { ["User-Agent"] = "reqwest" }, version = 2 })
-- if err then
-- ngx.log(ngx.ERR, "12313" .. err)
-- return nil, err
-- end
-- return res.body
-- end
--
-- -- Thread function
-- local function thread_func(premature)
-- if premature then
-- return
-- end
--
-- -- Perform the request in the thread
-- local body, err = perform_request()
--
-- if err then
-- ngx.log(ngx.ERR, "Request failed: " .. err)
-- -- Here you can handle the error (e.g., log it)
-- else
-- -- Here you can store or process the response as needed
-- ngx.shared.my_shared_dict:set("response_body", body)
-- ngx.say(body)
-- -- ngx.log(ngx.ERR, "Requesasdasd")
-- end
-- end
--
-- -- Start the thread
-- local ok, err = ngx.thread.spawn(thread_func)
-- if not ok then
--
-- ngx.log(ngx.ERR, "xd")
-- ngx.status = 500
-- ngx.say("Failed to spawn thread: " .. err)
-- return
-- end
--
-- -- Respond immediately, while the request is processed in the background
-- -- ngx.say("Request is being processed. You will receive the response shortly.")