log_by_lua_block {
    local skynet_account = require("skynet.account")

    -- tracking runs only when request comes from authenticated user
    if skynet_account.is_authenticated() then
        local function track(premature, skylink, auth_headers)
            if premature then return end

            local httpc = require("resty.http").new()

            -- 10.10.10.70 points to accounts service (alias not available when using resty-http)
            local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, {
                method = "POST",
                headers = auth_headers,
            })

            if err or (res and res.status ~= ngx.HTTP_NO_CONTENT) then
                local error_response = err or ("[HTTP " .. res.status .. "] " .. res.body)
                ngx.log(ngx.ERR, "Failed accounts service request /track/upload/" .. skylink .. ": ", error_response)
            end
        end

        -- report all skylinks (header empty if request failed) but only if jwt is preset (user is authenticated)
        if ngx.header["Skynet-Skylink"] then
            local auth_headers = skynet_account.get_auth_headers()
            local ok, err = ngx.timer.at(0, track, ngx.header["Skynet-Skylink"], auth_headers)
            if err then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
        end
    end

    -- this block runs only when scanner module is enabled
    if os.getenv("PORTAL_MODULES"):match("s") then
        local function scan(premature, skylink)
            if premature then return end

            local httpc = require("resty.http").new()

            -- 10.10.10.101 points to malware-scanner service (alias not available when using resty-http)
            local res, err = httpc:request_uri("http://10.10.10.101:4000/scan/" .. skylink, {
                method = "POST",
            })

            if err or (res and res.status ~= ngx.HTTP_OK) then
                local error_response = err or ("[HTTP " .. res.status .. "] " .. res.body)
                ngx.log(ngx.ERR, "Failed malware-scanner request /scan/" .. skylink .. ": ", error_response)
            end
        end

        -- scan all skylinks but make sure to only run if skylink is present (empty if request failed)
        if ngx.header["Skynet-Skylink"] then
            local ok, err = ngx.timer.at(0, scan, ngx.header["Skynet-Skylink"])
            if err then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
        end
    end
}