Übernehme Änderungen von Brawl345/Brawlbot-v2

Verbesserungen für Media: Abhängig vom Dateityp wird jetzt die richtige
ChatAction gesendet (upload_video, upload_audio, etc.)
Dateien mit einer Größe von > 50 MB werden ignoriert
This commit is contained in:
Akamaru 2016-07-23 20:04:22 +02:00
parent cb2c056100
commit 8db9a30d13
2 changed files with 36 additions and 13 deletions

View File

@ -30,10 +30,19 @@ media.triggers = {
function media:action(msg) function media:action(msg)
local url = matches[1] local url = matches[1]
local ext = matches[2] local ext = matches[2]
local mime_type = mimetype.get_content_type_no_sub(ext)
local receiver = msg.chat.id local receiver = msg.chat.id
local file, last_modified, nocache = get_cached_file(url, nil, msg.chat.id, 'upload_document', self) if mime_type == 'audio' then
local mime_type = mimetype.get_content_type_no_sub(ext) chat_action = 'upload_audio'
elseif mime_type == 'video' then
chat_action = 'upload_video'
else
chat_action = 'upload_document'
end
local file, last_modified, nocache = get_cached_file(url, nil, msg.chat.id, chat_action, self)
if not file then return end
if ext == 'gif' then if ext == 'gif' then
print('send gif') print('send gif')

View File

@ -776,7 +776,7 @@ function cache_file(result, url, last_modified)
redis:expire(hash..':'..url, 5259600) -- 2 months redis:expire(hash..':'..url, 5259600) -- 2 months
end end
function get_last_modified_header(url) function get_http_header(url)
local doer = HTTP local doer = HTTP
local do_redir = true local do_redir = true
if url:match('^https') then if url:match('^https') then
@ -789,12 +789,7 @@ function get_last_modified_header(url)
redirect = do_redir redirect = do_redir
} }
if not header then return end if not header then return end
if header["last-modified"] then return header, code
last_modified = header["last-modified"]
elseif header["Last-Modified"] then
last_modified = header["Last-Modified"]
end
return last_modified, code
end end
-- only url is needed! -- only url is needed!
@ -803,8 +798,8 @@ function get_cached_file(url, file_name, receiver, chat_action, self)
local cached_file_id = redis:hget(hash..':'..url, 'file_id') local cached_file_id = redis:hget(hash..':'..url, 'file_id')
local cached_last_modified = redis:hget(hash..':'..url, 'last_modified') local cached_last_modified = redis:hget(hash..':'..url, 'last_modified')
-- get last-modified header -- get last-modified and Content-Length header
local last_modified, code = get_last_modified_header(url) local header, code = get_http_header(url)
if code ~= 200 then if code ~= 200 then
if cached_file_id then if cached_file_id then
redis:del(hash..':'..url) redis:del(hash..':'..url)
@ -812,6 +807,25 @@ function get_cached_file(url, file_name, receiver, chat_action, self)
return return
end end
-- file size limit is 50 MB
if header["Content-Length"] then
if tonumber(header["Content-Length"]) > 52420000 then
print('file is too large, won\'t send!')
return nil
end
elseif header["content-length"] then
if tonumber(header["content-length"]) > 52420000 then
print('file is too large, won\'t send!')
return nil
end
end
if header["last-modified"] then
last_modified = header["last-modified"]
elseif header["Last-Modified"] then
last_modified = header["Last-Modified"]
end
if not last_modified then if not last_modified then
nocache = true nocache = true
else else