From a7e76299269bc5d25b8c0c597e9ab7c5edacef86 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 23 Jul 2016 13:58:15 +0200 Subject: [PATCH] =?UTF-8?q?-=20Verbesserungen=20f=C3=BCr=20Media:=20Abh?= =?UTF-8?q?=C3=A4ngig=20vom=20Dateityp=20wird=20jetzt=20die=20richtige=20C?= =?UTF-8?q?hatAction=20gesendet=20(upload=5Fvideo,=20upload=5Faudio,=20etc?= =?UTF-8?q?.)=20-=20Dateien=20mit=20einer=20Gr=C3=B6=C3=9Fe=20von=20>=2050?= =?UTF-8?q?=20MB=20werden=20ignoriert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/media.lua | 15 ++++++++++++--- otouto/utilities.lua | 32 +++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/otouto/plugins/media.lua b/otouto/plugins/media.lua index b082f40..e755f96 100644 --- a/otouto/plugins/media.lua +++ b/otouto/plugins/media.lua @@ -29,10 +29,19 @@ media.triggers = { function media:action(msg) local url = matches[1] local ext = matches[2] - local receiver = msg.chat.id - - local file, last_modified, nocache = get_cached_file(url, nil, msg.chat.id, 'upload_document', self) local mime_type = mimetype.get_content_type_no_sub(ext) + local receiver = msg.chat.id + + if mime_type == 'audio' then + 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 print('send gif') diff --git a/otouto/utilities.lua b/otouto/utilities.lua index b8be411..726fb32 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -776,7 +776,7 @@ function cache_file(result, url, last_modified) redis:expire(hash..':'..url, 5259600) -- 2 months end -function get_last_modified_header(url) +function get_http_header(url) local doer = HTTP local do_redir = true if url:match('^https') then @@ -789,12 +789,7 @@ function get_last_modified_header(url) redirect = do_redir } if not header then return end - if header["last-modified"] then - last_modified = header["last-modified"] - elseif header["Last-Modified"] then - last_modified = header["Last-Modified"] - end - return last_modified, code + return header, code end -- only url is needed! @@ -803,14 +798,33 @@ function get_cached_file(url, file_name, receiver, chat_action, self) local cached_file_id = redis:hget(hash..':'..url, 'file_id') local cached_last_modified = redis:hget(hash..':'..url, 'last_modified') - -- get last-modified header - local last_modified, code = get_last_modified_header(url) + -- get last-modified and Content-Length header + local header, code = get_http_header(url) if code ~= 200 then if cached_file_id then redis:del(hash..':'..url) end return 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 nocache = true