46 lines
1.4 KiB
Lua
46 lines
1.4 KiB
Lua
local sql = require("lsqlite3")
|
|
local zlib = require("zlib")
|
|
|
|
local db = require("db")
|
|
local queries = require("queries")
|
|
local util = require("util")
|
|
local pages = require("pages")
|
|
|
|
local stmnt_download
|
|
local oldconfigure = configure
|
|
function configure(...)
|
|
stmnt_download = assert(db.conn:prepare(queries.select_download))
|
|
return oldconfigure(...)
|
|
end
|
|
|
|
local function download_get(req)
|
|
local host = http_request_get_host(req)
|
|
local path = http_request_get_path(req)
|
|
print("host:",host,"path:",path)
|
|
http_request_populate_qs(req)
|
|
local story = assert(http_argument_get_string(req,"story"))
|
|
local story_id = util.decode_id(story)
|
|
print("Downloading", story_id)
|
|
stmnt_download:bind_names{
|
|
postid = story_id
|
|
}
|
|
local err = util.do_sql(stmnt_download)
|
|
if err == sql.DONE then
|
|
--No rows, story not found
|
|
http_responose(req,404,pages.nostory{path=story})
|
|
stmnt_download:reset()
|
|
return
|
|
end
|
|
assert(err == sql.ROW, "after doing download sql, result was not a row, was:" .. tostring(err))
|
|
local txt_compressed, title = unpack(stmnt_download:get_values())
|
|
local text = zlib.decompress(txt_compressed)
|
|
stmnt_download:reset()
|
|
http_response_header(req,"Content-Type","application/octet-stream")
|
|
local nicetitle = title:gsub("%W","_")
|
|
http_response_header(req,"Content-Disposition","attachment; filename=\"" .. nicetitle .. ".txt\"")
|
|
http_response(req,200,text)
|
|
|
|
end
|
|
|
|
return download_get
|