From e47102ec8c2eae60ea7e2f2777e3f891b4c974e1 Mon Sep 17 00:00:00 2001 From: jmaa Date: Sat, 16 Jun 2018 13:10:49 +0200 Subject: [PATCH] Improved error handling when attempting to download bad files. --- internet.lua | 24 +++++++++++++++----- memes.lua | 63 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/internet.lua b/internet.lua index 75d6c96..53b51d4 100644 --- a/internet.lua +++ b/internet.lua @@ -46,11 +46,15 @@ local function safe_access (base, path) end local function generic_request (...) + --print('Request', ...) local output, code, headers, status = https.request(...) - if status == nil and code ~= 'connection refused' then + --print('Https', output, code, headers, status) + if code ~= nil and status ~= 'connection refused' then return output, code, headers, status end - return http.request(...) + local output, code, headers, status = http.request(...) + --print('Http', output, code, headers, status) + return output, code, headers, status end -------------------------------------------------------------------------------- @@ -262,14 +266,25 @@ end -- Download file function internet.download_file (url, filename) + assert(type(url) == 'string') + assert(type(filename) == 'string') + -- retrieve the content of a URL - local body, code = https.request(url) - if not body then error(('Https connection to "%s" failed, with error "%s"'):format(url, code)) end + --local body, code, headers, status = generic_request(url) + local body, code, headers, status = https.request(url) + + if code ~= 200 then + return false, code + --error(('Connection to "%s" failed, with error "%s"'):format(url, status)) + end + assert(type(body) == 'string') -- save the content to a file local f = assert(io.open(filename, 'wb')) -- open in "binary" mode f:write(body) f:close() + + return true end function internet.download_video (url) @@ -287,7 +302,6 @@ function internet.download_headers (url) url = url, method = 'HEAD' } - print(_, code, headers, status) -- return headers end diff --git a/memes.lua b/memes.lua index 767909a..b7447b3 100644 --- a/memes.lua +++ b/memes.lua @@ -244,24 +244,16 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font assert(type(font_name) == 'string') -- Download and paste found image if topic.type == 'image' then - local file_extension = topic.url:match '%.(%a+)$' - local url, filename = topic.url, os.tmpname() --CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..file_extension - assert(type(url) == 'string' and #url > 0) - assert(type(filename) == 'string' and #filename > 0) - internet.download_file(url, filename) - -- Convert svg to png - if url:match '%.svg$' then - local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png' - os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null') - filename = filename_2 - end - -- - local found_img = assert(imlib.image.load(filename)) + assert(type(topic.filename) == 'string') + assert(not topic.url) + + local found_img = assert(imlib.image.load(topic.filename)) found_img:crop_and_scale(0, 0, found_img:get_width(), found_img:get_height(), w, h) flatten_onto (target, found_img, x, y) found_img:free() --os.remove(filename) elseif topic.type == 'text' then + assert(type(topic.text) == 'string') local text = topic.text draw_centered_text_in_box(font_name, target, text, x, y, w, h, bg_color, font_color) elseif topic.type == 'droste' then @@ -317,6 +309,24 @@ local DROSTE_EFFECT_TRIGGERS = { ['loop'] = true, } +local function download_and_standardize_image (image_url) + local file_extension = image_url:match '%.(%a+)$' + local url, filename = image_url, os.tmpname() + assert(type(url) == 'string' and #url > 0) + assert(type(filename) == 'string' and #filename > 0) + local success, errmsg = internet.download_file(url, filename) + + -- Convert svg to png + if success and url:match '%.svg$' then + local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png' + os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null') + filename = filename_2 + end + -- + + return success and filename, errmsg +end + local function fill_in_topics_information (topics) assert(type(topics) == 'table') -- @@ -328,9 +338,13 @@ local function fill_in_topics_information (topics) local url = topic_to_image_url[topic] - if DROSTE_EFFECT_TRIGGERS[topic] then new_topics[i] = { topic = topic, type = 'droste' } - elseif url then new_topics[i] = { topic = topic, type = 'image', url = url } - else new_topics[i] = { topic = topic, type = 'text', text = topic } + if DROSTE_EFFECT_TRIGGERS[topic] then + new_topics[i] = { topic = topic, type = 'droste' } + elseif url then + local filename = download_and_standardize_image(url) + new_topics[i] = { topic = topic, type = 'image', filename = filename } + else + new_topics[i] = { topic = topic, type = 'text', text = topic } end end return new_topics @@ -610,12 +624,17 @@ function memes.generate_for_message (user, message) -- Is this a rich picture? if is_image_link(message) then - local url = message - local img_link = generate_is_this_a_pidgeon { - { type = 'image', url = url }, - { type = 'text', text = 'Is this a rich picture?' } - } - return img_link, 'KYNG' + local filename, status = download_and_standardize_image(message) + if filename then + local img_link = generate_is_this_a_pidgeon { + { type = 'image', filename = filename }, + { type = 'text', text = 'Is this a rich picture?' } + } + return img_link, 'KYNG' + else + img_link = 'Kunne ikke skaffe det billede. Fik fejlkode '..tostring(status) + return img_link, '!KYNG' + end end -- Comparison memes