Improved how bot saves file extension

This commit is contained in:
yago 2015-02-15 16:52:08 +01:00
parent 3a48303899
commit b391bb26d2

View File

@ -1,3 +1,4 @@
local mimetype = (loadfile "./libs/mimetype.lua")()
local ltn12 = require "ltn12" local ltn12 = require "ltn12"
function get_receiver(msg) function get_receiver(msg)
@ -37,8 +38,24 @@ function string.trim(s)
return s:gsub("^%s*(.-)%s*$", "%1") return s:gsub("^%s*(.-)%s*$", "%1")
end end
function get_http_file_name(url, headers)
-- Everything after the last /
local file_name = url:match("([^/]+)$")
-- Possible headers names
local content_type = headers["content-type"]
content_type = content_type or headers["Content-type"]
content_type = content_type or h["Content-Type"]
local extension = mimetype.get_mime_extension(content_type)
if extension then
file_name = file_name.."."..extension
end
return file_name
end
-- Saves file to /tmp/. If file_name isn't provided, -- Saves file to /tmp/. If file_name isn't provided,
-- will get the text after the last "/" for filename. -- will get the text after the last "/" for filename
-- and content-type for extension
function download_to_file(url, file_name) function download_to_file(url, file_name)
print("url to download: "..url) print("url to download: "..url)
@ -50,10 +67,10 @@ function download_to_file(url, file_name)
} }
local one, c, h = http.request(options) local one, c, h = http.request(options)
-- Everything after the last / local file_name = get_http_file_name(url, h)
local file_name = url:match("([^/]+)$")
local file_path = "/tmp/"..file_name local file_path = "/tmp/"..file_name
print("Saved to: "..file_path)
file = io.open(file_path, "w+") file = io.open(file_path, "w+")
file:write(table.concat(respbody)) file:write(table.concat(respbody))
@ -249,7 +266,7 @@ end
-- Callback to remove a file -- Callback to remove a file
function rmtmp_cb(cb_extra, success, result) function rmtmp_cb(cb_extra, success, result)
local file_path = cb_extra.file_path local file_path = cb_extra.file_path
local cb_function = cb_extra.cb_function local cb_function = cb_extra.cb_function or ok_cb
local cb_extra = cb_extra.cb_extra local cb_extra = cb_extra.cb_extra
if file_path ~= nil then if file_path ~= nil then