Browse Source

[Feature] Mime_types: Use detected content type as well

pull/2685/head
Vsevolod Stakhov 7 years ago
parent
commit
c27548d3eb
  1. 11
      lualib/lua_fuzzy.lua
  2. 23
      src/plugins/lua/mime_types.lua

11
lualib/lua_fuzzy.lua

@ -241,6 +241,8 @@ local function mime_types_check(task, part, rule)
if not t then return false, false end
local ct = string.format('%s/%s', t, st)
t,st = part:get_detected_type()
local detected_ct = string.format('%s/%s', t, st)
local id = part:get_id()
lua_util.debugm(N, task, 'check binary part %s: %s', id, ct)
@ -264,9 +266,14 @@ local function mime_types_check(task, part, rule)
if rule.mime_types then
if fun.any(function(gl_re)
if gl_re:match(ct) then return true else return false end
if gl_re:match(ct) or (detected_ct and gl_re:match(detected_ct)) then
return true
else
return false
end
end, rule.mime_types) then
lua_util.debugm(N, task, 'found mime type match for part %s: %s', id, ct)
lua_util.debugm(N, task, 'found mime type match for part %s: %s (%s detected)',
id, ct, detected_ct)
return check_length(task, part, rule),false
end

23
src/plugins/lua/mime_types.lua

@ -911,6 +911,7 @@ local function check_mime_type(task)
if parts then
for _,p in ipairs(parts) do
local mtype,subtype = p:get_type()
local dtype,dsubtype = p:get_detected_type()
if not mtype then
task:insert_result(settings['symbol_unknown'], 1.0, 'missing content type')
@ -920,6 +921,10 @@ local function check_mime_type(task)
-- Check for attachment
local filename = p:get_filename()
local ct = string.format('%s/%s', mtype, subtype):lower()
local detected_ct
if dtype and dsubtype then
detected_ct = string.format('%s/%s', dtype, dsubtype)
end
if filename then
filename = filename:gsub('[^%s%g]', '?')
@ -971,13 +976,27 @@ local function check_mime_type(task)
end
if map then
local v = map:get_key(ct)
local v
local detected_different = false
if detected_ct and detected_ct ~= ct then
v = map:get_key(detected_ct)
detected_different = true
else
v = map:get_key(ct)
end
if v then
local n = tonumber(v)
if n then
if n > 0 then
task:insert_result(settings['symbol_bad'], n, ct)
if detected_different then
-- Penalize case
n = n * 1.5
task:insert_result(settings['symbol_bad'], n,
string.format('%s:%s', ct, detected_ct))
else
task:insert_result(settings['symbol_bad'], n, ct)
end
task:insert_result('MIME_TRACE', 0.0,
string.format("%s:%s", p:get_id(), '-'))
elseif n < 0 then

Loading…
Cancel
Save