get_cached_file() umgeschrieben, sendet jetzt einen "If-Modified-Since"-Header, was viel schneller geht, als die zwei Last-Modified-Header zu vergleichen

This commit is contained in:
Andreas Bielawski 2016-08-03 18:35:58 +02:00
parent e0f53dd205
commit 74ab78322d
2 changed files with 50 additions and 20 deletions

View File

@ -40,19 +40,14 @@ function media:action(msg)
if not file then return end if not file then return end
if ext == 'gif' then if ext == 'gif' then
print('send gif')
result = utilities.send_document(self, receiver, file, nil, msg.message_id) result = utilities.send_document(self, receiver, file, nil, msg.message_id)
elseif ext == 'ogg' then elseif ext == 'ogg' then
print('send ogg')
result = utilities.send_voice(self, receiver, file, nil, msg.message_id) result = utilities.send_voice(self, receiver, file, nil, msg.message_id)
elseif mime_type == 'audio' then elseif mime_type == 'audio' then
print('send_audio')
result = utilities.send_audio(self, receiver, file, nil, msg.message_id) result = utilities.send_audio(self, receiver, file, nil, msg.message_id)
elseif mime_type == 'video' then elseif mime_type == 'video' then
print('send_video')
result = utilities.send_video(self, receiver, file, nil, msg.message_id) result = utilities.send_video(self, receiver, file, nil, msg.message_id)
else else
print('send_file')
result = utilities.send_document(self, receiver, file, nil, msg.message_id) result = utilities.send_document(self, receiver, file, nil, msg.message_id)
end end

View File

@ -851,20 +851,62 @@ function get_http_header(url)
return header, code return header, code
end end
-- checks with If-Modified-Since header, if url has been changed
-- URL and Last-Modified heder are required
function was_modified_since(url, last_modified)
local doer = http
local do_redir = true
if url:match('^https') then
doer = https
do_redir = false
end
local _, code, header = doer.request {
url = url,
method = "HEAD",
redirect = do_redir,
headers = {
["If-Modified-Since"] = last_modified
}
}
if code == 304 then
return false, nil, code
else
if header["last-modified"] then
new_last_modified = header["last-modified"]
elseif header["Last-Modified"] then
new_last_modified = header["Last-Modified"]
end
return true, new_last_modified, code
end
end
-- only url is needed! -- only url is needed!
function get_cached_file(url, file_name, receiver, chat_action, self) function get_cached_file(url, file_name, receiver, chat_action, self)
local hash = 'telegram:cache:sent_file' local hash = 'telegram:cache:sent_file'
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')
if cached_last_modified then
was_modified, new_last_modified, code = was_modified_since(url, cached_last_modified)
if not was_modified then
print('File wasn\'t modified, skipping download...')
return cached_file_id, nil, true
else
if code ~= 200 then
redis:del(hash..':'..url)
return
end
print('File was modified, redownloading...')
if receiver and chat_action and self then
utilities.send_typing(self, receiver, chat_action)
end
file = download_to_file(url, file_name)
return file, new_last_modified, false
end
end
-- get last-modified and Content-Length header -- get last-modified and Content-Length header
local header, code = get_http_header(url) 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 -- file size limit is 50 MB
if header["Content-Length"] then if header["Content-Length"] then
@ -896,14 +938,7 @@ function get_cached_file(url, file_name, receiver, chat_action, self)
end end
if not nocache then if not nocache then
if last_modified == cached_last_modified then file = download_to_file(url, file_name)
print('File not modified and already cached')
nocache = true
file = cached_file_id
else
print('File cached, but modified or not already cached. (Re)downloading...')
file = download_to_file(url, file_name)
end
else else
print('No Last-Modified header!') print('No Last-Modified header!')
file = download_to_file(url, file_name) file = download_to_file(url, file_name)