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:
parent
e0f53dd205
commit
74ab78322d
@ -40,19 +40,14 @@ function media:action(msg)
|
||||
if not file then return end
|
||||
|
||||
if ext == 'gif' then
|
||||
print('send gif')
|
||||
result = utilities.send_document(self, receiver, file, nil, msg.message_id)
|
||||
elseif ext == 'ogg' then
|
||||
print('send ogg')
|
||||
result = utilities.send_voice(self, receiver, file, nil, msg.message_id)
|
||||
elseif mime_type == 'audio' then
|
||||
print('send_audio')
|
||||
result = utilities.send_audio(self, receiver, file, nil, msg.message_id)
|
||||
elseif mime_type == 'video' then
|
||||
print('send_video')
|
||||
result = utilities.send_video(self, receiver, file, nil, msg.message_id)
|
||||
else
|
||||
print('send_file')
|
||||
result = utilities.send_document(self, receiver, file, nil, msg.message_id)
|
||||
end
|
||||
|
||||
|
@ -851,20 +851,62 @@ function get_http_header(url)
|
||||
return header, code
|
||||
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!
|
||||
function get_cached_file(url, file_name, receiver, chat_action, self)
|
||||
local hash = 'telegram:cache:sent_file'
|
||||
local cached_file_id = redis:hget(hash..':'..url, 'file_id')
|
||||
local cached_last_modified = redis:hget(hash..':'..url, 'last_modified')
|
||||
|
||||
-- get last-modified and Content-Length header
|
||||
local header, code = get_http_header(url)
|
||||
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
|
||||
if cached_file_id then
|
||||
redis:del(hash..':'..url)
|
||||
end
|
||||
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
|
||||
local header, code = get_http_header(url)
|
||||
|
||||
-- file size limit is 50 MB
|
||||
if header["Content-Length"] then
|
||||
@ -896,14 +938,7 @@ function get_cached_file(url, file_name, receiver, chat_action, self)
|
||||
end
|
||||
|
||||
if not nocache then
|
||||
if last_modified == cached_last_modified then
|
||||
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
|
||||
print('No Last-Modified header!')
|
||||
file = download_to_file(url, file_name)
|
||||
|
Reference in New Issue
Block a user