2016-06-14 15:25:53 +02:00
|
|
|
local media = {}
|
|
|
|
|
2016-07-03 13:02:54 +02:00
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local HTTPS = require('ssl.https')
|
|
|
|
local redis = (loadfile "./otouto/redis.lua")()
|
2016-06-14 15:25:53 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
|
|
|
local mimetype = (loadfile "./otouto/mimetype.lua")()
|
|
|
|
|
|
|
|
media.triggers = {
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(gif))$",
|
|
|
|
"^(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(mp4))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(pdf))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(ogg))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(zip))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(tar.gz))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(7z))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(mp3))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(rar))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(wmv))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(doc))$",
|
|
|
|
"^(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(avi))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(wav))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(apk))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(webm))$",
|
|
|
|
"^(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(ogv))$",
|
|
|
|
"(https?://[%w-_%.%?%.:,/%+=&%[%]]+%.(webp))$"
|
|
|
|
}
|
|
|
|
|
|
|
|
function media:action(msg)
|
|
|
|
local url = matches[1]
|
|
|
|
local ext = matches[2]
|
2016-07-03 13:02:54 +02:00
|
|
|
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')
|
2016-06-14 15:25:53 +02:00
|
|
|
local receiver = msg.chat.id
|
2016-07-03 13:02:54 +02:00
|
|
|
|
|
|
|
-- Last-Modified-Header auslesen
|
|
|
|
local doer = HTTP
|
|
|
|
local do_redir = true
|
|
|
|
if url:match('^https') then
|
|
|
|
doer = HTTPS
|
|
|
|
do_redir = false
|
|
|
|
end
|
|
|
|
local _, c, h = doer.request {
|
|
|
|
method = "HEAD",
|
|
|
|
url = url,
|
|
|
|
redirect = do_redir
|
|
|
|
}
|
|
|
|
|
|
|
|
if c ~= 200 then
|
|
|
|
if cached_file_id then
|
|
|
|
redis:del(hash..':'..url)
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2016-06-14 15:25:53 +02:00
|
|
|
|
2016-06-14 16:17:13 +02:00
|
|
|
utilities.send_typing(self, receiver, 'upload_document')
|
2016-07-03 13:02:54 +02:00
|
|
|
if not h["last-modified"] and not h["Last-Modified"] then
|
|
|
|
nocache = true
|
|
|
|
last_modified = nil
|
|
|
|
else
|
|
|
|
nocache = false
|
|
|
|
last_modified = h["last-modified"]
|
|
|
|
if not last_modified then
|
|
|
|
last_modified = h["Last-Modified"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-14 15:25:53 +02:00
|
|
|
local mime_type = mimetype.get_content_type_no_sub(ext)
|
2016-07-03 13:02:54 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print('No Last-Modified header!')
|
|
|
|
file = download_to_file(url)
|
|
|
|
end
|
2016-06-14 15:25:53 +02:00
|
|
|
|
|
|
|
if ext == 'gif' then
|
|
|
|
print('send gif')
|
2016-07-03 13:02:54 +02:00
|
|
|
result = utilities.send_document(self, receiver, file, nil, msg.message_id)
|
2016-06-14 15:25:53 +02:00
|
|
|
elseif mime_type == 'audio' then
|
|
|
|
print('send_audio')
|
2016-07-03 13:02:54 +02:00
|
|
|
result = utilities.send_audio(self, receiver, file, nil, msg.message_id)
|
2016-06-14 15:25:53 +02:00
|
|
|
elseif mime_type == 'video' then
|
|
|
|
print('send_video')
|
2016-07-03 13:02:54 +02:00
|
|
|
result = utilities.send_video(self, receiver, file, nil, msg.message_id)
|
2016-06-14 15:25:53 +02:00
|
|
|
else
|
|
|
|
print('send_file')
|
2016-07-03 13:02:54 +02:00
|
|
|
result = utilities.send_document(self, receiver, file, nil, msg.message_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if nocache then return end
|
|
|
|
if not result then return end
|
|
|
|
|
|
|
|
-- Cache File-ID und Last-Modified-Header in Redis
|
|
|
|
if result.result.video then
|
|
|
|
file_id = result.result.video.file_id
|
|
|
|
elseif result.result.audio then
|
|
|
|
file_id = result.result.audio.file_id
|
|
|
|
elseif result.result.voice then
|
|
|
|
file_id = result.result.voice.file_id
|
|
|
|
else
|
|
|
|
file_id = result.result.document.file_id
|
2016-06-14 15:25:53 +02:00
|
|
|
end
|
2016-07-03 13:02:54 +02:00
|
|
|
redis:hset(hash..':'..url, 'file_id', file_id)
|
|
|
|
redis:hset(hash..':'..url, 'last_modified', last_modified)
|
|
|
|
-- Why do we set a TTL? Because Telegram recycles outgoing file_id's
|
|
|
|
-- See: https://core.telegram.org/bots/faq#can-i-count-on-file-ids-to-be-persistent
|
|
|
|
redis:expire(hash..':'..url, 5259600) -- 2 months
|
2016-06-14 15:25:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return media
|