[root@CentOS-90-stream-amd64-base opt]# cat image2binary.lua
-- /opt/image2base64.lua(保留 dataURL 的 text/plain 版)
local ROOT_PREFIX_URI = "/anime"
local ROOT_DIR = "/anime"
local function read_file(p)
local f = io.open(p, "rb"); if not f then return nil end
local d = f:read("*a"); f:close(); return d
end
local uri = ngx.var.uri or ""
if not uri:find("^" .. ROOT_PREFIX_URI .. "/") then return ngx.exit(404) end
local rel = uri:sub(#ROOT_PREFIX_URI + 2)
if rel == "" or rel:find("%.%.", 1, true) or rel:find("\0") then return ngx.exit(400) end
local disk = ROOT_DIR .. "/" .. rel
local bin = read_file(disk); if not bin or #bin == 0 then return ngx.exit(404) end
local ext = (disk:match("%.([A-Za-z0-9]+)$") or ""):lower()
local mime = ({png="image/png", jpg="image/jpeg", jpeg="image/jpeg",
gif="image/gif", webp="image/webp", svg="image/svg+xml",
avif="image/avif", bmp="image/bmp", ico="image/x-icon"})[ext]
or "application/octet-stream"
local data_url = "data:" .. mime .. ";base64," .. ngx.encode_base64(bin)
local etag = '"' .. ngx.md5(bin) .. '"'
local inm = ngx.req.get_headers()["If-None-Match"]