add send as attachment

This commit is contained in:
William Bell committed 2023-07-09 18:44:54 +01:00
1 parent 24e10027a3
commit ee781a224b
2 files changed
+25 -14

No files matched your search

+4 -6
View File
@@ -25,6 +25,10 @@ let api(req, res) = do
res.json({(data): "Hello, World!", description: "This is a test.", π: π, πRational: fraction(π)})
app.route("/api", null, api)
let google(req, res) = do
res.redirect("https://google.com/")
app.route("/google", null, google)
let api(req, res) = do
res.json(req.params)
app.route("/params/example/<name>", null, api)
@@ -40,12 +44,6 @@ let rand(req, res) = do
res.json(random())
app.route("/random", null, rand)
let close(req, res) = do
app.close()
res.json(true)
exit()
app.route("/close", null, close)
let about(req, res) = do
res.send("<h1>About</h1>")
app.route("/about", null, about)
+21 -8
View File
@@ -29,10 +29,21 @@ let handle(routes, conn, onErr) = do
for (i from 0 to request.on.headersSent.length) do
request.on.headersSent[i]()
response.sendFile(path) = do
response.sendFile(f) = do
let path = null
let attachment = null
if (typeof(f) == "string") do
path = f
else do
path = f.path
if ("attachment" in f) do
attachment = f.attachment
let FILE = file.read(path)
response.headers["Content-Type"] = FILE.contentType()
response.headers["Content-Disposition"] = "inline; filename="+json.stringify(path.split("/")[-1])
if (attachment) do
response.headers["Content-Disposition"] = "attachment; filename="+attachment
else do
response.headers["Content-Disposition"] = "inline; filename="+json.stringify(path.split("/")[-1])
let doRange = "Range" in request.headers
let FileSize = FILE.size()
let size = FileSize
@@ -66,7 +77,10 @@ let handle(routes, conn, onErr) = do
catch (_) do
data = buffer()
response.close()
response.redirect(path) = do
response.code = 302
response.headers["Location"] = path
response.send("")
response.close() = do
conn.close()
for (i from 0 to request.on.end.length) do
@@ -84,14 +98,13 @@ let handle(routes, conn, onErr) = do
response.headers["Content-Type"] = "application/json"
response.send(strdata)
response.data(data) = do
response.sendHeaders()
conn.write(buffer().from(data))
response.send(data) = do
response.headers["Connection"] = "close"
response.sendHeaders()
if (conn.isClosed()) return
conn.write(buffer().from(data))
response.send(str) = do
response.headers["Connection"] = "close"
response.data(buffer().from(str))
response.close()
let toTry = []