diff options
author | Jason Stephenson <j.stephenson@live.co.uk> | 2022-07-25 09:26:48 +0100 |
---|---|---|
committer | Jason Stephenson <j.stephenson@live.co.uk> | 2022-07-25 09:26:48 +0100 |
commit | 8fe5e7d31887f743718e5307692dadd56e7d5431 (patch) | |
tree | b7129e8d465079abc97aff208fb21f872a21644d /lualib | |
parent | 2b6fdae55cd09bc2183f3e30e6b816792c53cfce (diff) | |
download | rspamd-8fe5e7d31887f743718e5307692dadd56e7d5431.tar.gz rspamd-8fe5e7d31887f743718e5307692dadd56e7d5431.zip |
get_dump_content to return content directly without converting userdata to string. write_dump_content to convert string to userdata (rspamd_text) and use save_in_file.
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/rspamadm/mime.lua | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/lualib/rspamadm/mime.lua b/lualib/rspamadm/mime.lua index 13b022def..b09ab57bf 100644 --- a/lualib/rspamadm/mime.lua +++ b/lualib/rspamadm/mime.lua @@ -18,6 +18,7 @@ local argparse = require "argparse" local ansicolors = require "ansicolors" local rspamd_util = require "rspamd_util" local rspamd_task = require "rspamd_task" +local rspamd_text = require "rspamd_text" local rspamd_logger = require "rspamd_logger" local lua_meta = require "lua_meta" local rspamd_url = require "rspamd_url" @@ -900,6 +901,10 @@ assert(filename_only("/home/dir.with.dots/very_simple.eml") == "very_simple") --Write the dump content to file or standard out local function write_dump_content(dump_content, fname, extension, outdir) + if type(dump_content) == "string" then + dump_content = rspamd_text.fromstring(dump_content) + end + local wrote_filepath = nil if outdir then if outdir:sub(-1) ~= "/" then @@ -907,18 +912,18 @@ local function write_dump_content(dump_content, fname, extension, outdir) end local outpath = string.format("%s%s.%s", outdir, filename_only(fname), extension) - local outfile = io.open(outpath, "w") - - if outfile then - outfile:write(dump_content) - outfile:close() - io.write(outpath.."\n") + if rspamd_util.file_exists(outpath) then + os.remove(outpath) + end + + if dump_content:save_in_file(outpath) then wrote_filepath = outpath + io.write(wrote_filepath.."\n") else - io.stderr:write(string.format("Unable to open: %s\n", outpath)) + io.stderr:write(string.format("Unable to save dump content to file: %s\n", outpath)) end else - io.write(dump_content) + dump_content:save_in_file(1) end return wrote_filepath end @@ -932,12 +937,7 @@ local function get_dump_content(task, opts, fname) for i, part in ipairs(ucl_object.parts) do if part.content then local part_filename = string.format("%s-part%d", filename_only(fname), i) - local part_path = nil - if type(part.content) == "string" then - part_path = write_dump_content(part.content, part_filename, "raw", opts.outdir) - else - part_path = write_dump_content(tostring(part.content), part_filename, "raw", opts.outdir) - end + local part_path = write_dump_content(part.content, part_filename, "raw", opts.outdir) if part_path then part.content = ucl.null part.content_path = part_path @@ -945,16 +945,10 @@ local function get_dump_content(task, opts, fname) end end end - local extension = output_fmt(opts) return ucl.to_format(ucl_object, extension), extension end - local content = task:get_content() - if type(content) == "string" then - return content, "mime" - else - return tostring(content), "mime" - end + return task:get_content(), "mime" end local function dump_handler(opts) |