Controll HTTP errors downloading images

This commit is contained in:
yago 2015-02-28 14:22:53 +01:00
parent 6a8be34956
commit dab0c97bb6

View File

@ -83,6 +83,8 @@ function download_to_file(url, file_name)
local headers = response[3] local headers = response[3]
local status = response[4] local status = response[4]
if code ~= 200 then return nil end
file_name = file_name or get_http_file_name(url, headers) file_name = file_name or get_http_file_name(url, headers)
local file_path = "/tmp/"..file_name local file_path = "/tmp/"..file_name
@ -242,10 +244,19 @@ end
-- Download the image and send to receiver, it will be deleted. -- Download the image and send to receiver, it will be deleted.
-- cb_function and cb_extra are optionals callback -- cb_function and cb_extra are optionals callback
function send_photo_from_url(receiver, url, cb_function, cb_extra) function send_photo_from_url(receiver, url, cb_function, cb_extra)
-- If callback not provided
cb_function = cb_function or ok_cb
cb_extra = cb_extra or false
local file_path = download_to_file(url, false) local file_path = download_to_file(url, false)
if not file_path then -- Error
local text = 'Error downloading the image'
send_msg(receiver, text, cb_function, cb_extra)
else
print("File path: "..file_path) print("File path: "..file_path)
_send_photo(receiver, file_path, cb_function, cb_extra) _send_photo(receiver, file_path, cb_function, cb_extra)
end end
end
-- Same as send_photo_from_url but as callback function -- Same as send_photo_from_url but as callback function
function send_photo_from_url_callback(cb_extra, success, result) function send_photo_from_url_callback(cb_extra, success, result)
@ -253,8 +264,13 @@ function send_photo_from_url_callback(cb_extra, success, result)
local url = cb_extra.url local url = cb_extra.url
local file_path = download_to_file(url, false) local file_path = download_to_file(url, false)
if not file_path then -- Error
local text = 'Error downloading the image'
send_msg(receiver, text, ok_cb, false)
else
print("File path: "..file_path) print("File path: "..file_path)
_send_photo(receiver, file_path, cb_function, cb_extra) _send_photo(receiver, file_path, ok_cb, false)
end
end end
-- Send multimple images asynchronous. -- Send multimple images asynchronous.