forked from MrSyabro/lua_http_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.lua
More file actions
112 lines (101 loc) · 3.3 KB
/
files.lua
File metadata and controls
112 lines (101 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local fs = require "lfs"
local h = require "html"
local page = dofile "page.lua"
local url = require "socket.url"
local query = {}
if request.url.query then
query = server.parsequery(request.url.query)
end
local file = query.file
if file then
local f, err = io.open(server.ROOT_DIR .. "/" .. file, "rb")
if f then
local data_lenghth = f:seek("end"); f:seek("set")
response.headers["Content-Type"] = "application/octet-stream"
response.headers["Content-Disposition"] = "attachment; filename=" .. file
response.headers["Content-Length"] = tostring(data_lenghth)
server:sendheaders()
for l in f:lines(server.datagramsize) do
client:send(l)
coroutine.yield()
end
f:close()
else
server:error(404)
end
return
end
local dir = query.dir or ""
local breadcrumpattr = { class = "breadcrumb-item" }
local dirnodes = url.parse_path(dir)
local dirnav = {}
if #dirnodes > 0 then
table.insert(dirnav, h.li(breadcrumpattr, h.a({ href = "/files" }, "Home")))
for i, node in ipairs(dirnodes) do
if i == #dirnodes then
table.insert(dirnav, h.li({ class = "breadcrumb-item active", ["aria-current"] = "page" }, node))
else
table.insert(dirnav,
h.li(breadcrumpattr,
h.a({ href = "/files?dir=" .. url.escape("/" .. table.concat(dirnodes, "/", 1, i) .. "/") }, node)))
end
end
end
local out = {}
local fileimgattr = { src = "assets/file.png", alt = "twbs", height = "32", class = "flex-shrink-0" }
local dirimgattr = { src = "assets/folder.png", class = "flex-shrink-0", height = "32", alt = "Directory" }
for d in fs.dir(server.ROOT_DIR .. "/" .. dir) do
if d ~= "." and d ~= ".." then
local attr, err = fs.attributes(server.ROOT_DIR .. "/" .. dir .. d)
if attr then
if attr.mode == "directory" then
local card = h.a({
class = "list-group-item list-group-item-action gap-3 py-3",
["aria-current"] = "true",
href = "files?dir=" .. url.escape(dir .. d .. "/")
},
h.div({ class = "d-flex gap-2 w-100 justify-content-between" },
h.img(dirimgattr),
h.h6({ class = "mb-0" }, d),
h.small(nil, os.date("%m.%d %H:%M", attr.modification))
)
)
table.insert(out, card)
elseif attr.mode == "file" then
local card = h.div({
class = "list-group-item gap-3 py-3",
["aria-current"] = "true"
},
h.div({ class = "d-flex gap-2 w-100 justify-content-between" },
h.img(fileimgattr),
h.h6({ class = "mb-0" }, d),
h.small(nil, os.date("%m.%d %H:%M", attr.modification))
),
h.p({ class = "opacity-50 text-nowrap" }, ("Size: %.2fkb"):format(attr.size / 1024)),
h.div({ class = "btn-group", role = "group", ["aria-label"] = "Basic example" },
h.a({ target = "_blank", class = "btn", href = "?file=" .. url.escape(dir .. d) }, "Download"),
h.a({ target = "_blank", class = "btn", href = dir .. d }, "Open")
--h.a({ class = "btn", href = "" }, "Delete")
)
)
table.insert(out, card)
end
else
error(err)
end
end
end
echo(page("Files", h.div({ class = "container my-5" },
(#dirnav > 0) and h.div({ class = "row" },
h.nav({ style = "--bs-breadcrumb-divider: '/';", ["aria-label"] = "breadcrumb" },
h.ol({ class = "col-lg-8 col-md-6 mx-auto breadcrumb" },
dirnav
)
)
) or "",
h.div({ class = "row" },
h.div({ class = "col-lg-8 col-md-6 mx-auto list-group" },
out
)
)
)))