forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.lua
More file actions
127 lines (101 loc) · 2.96 KB
/
Copy pathupload.lua
File metadata and controls
127 lines (101 loc) · 2.96 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
return function (connection, req, args)
dofile("httpserver-header.lc")(connection, 200, 'json')
connection:send('{')
local mbOffset = nil
local mbLen = nil
local mbData = nil
local mbCmd = nil
local mbFilename = nil
local fieldsCount = 0
local fileSize = 0
local i = 0
local binaryData = ''
local currentByte = nil
for name, value in pairs(args) do
if (name == "offset") then
mbOffset = tonumber(value, 10)
fieldsCount = fieldsCount + 1
end
if (name == "len") then
mbLen = tonumber(value, 10)
fieldsCount = fieldsCount + 1
end
if (name == "data") then
mbData = value
fieldsCount = fieldsCount + 1
end
if (name == "filename") then
mbFilename = value
fieldsCount = fieldsCount + 1
end
if (name == "filesize") then
fileSize = tonumber(value, 10)
fieldsCount = fieldsCount + 1
end
if (name == "cmd") then
mbCmd = value
fieldsCount = fieldsCount + 1
end
end
if (mbCmd == 'upload') then
if (fieldsCount > 5) then
if (mbFilename ~= 'upload.lua') then
connection:send('"offset":"' .. mbOffset .. '",')
connection:send('"len":"' .. mbLen .. '",')
connection:send('"filename":"' .. mbFilename .. '"')
mbFilename = 'http/' .. mbFilename
for i=1,string.len(mbData),2 do
currentByte = tonumber(string.sub(mbData, i, i + 1), 16)
binaryData = binaryData .. string.char(currentByte)
end
if (mbOffset > 0) then
file.open(mbFilename .. '.dnl','a+')
else
file.remove(mbFilename .. '.dnl')
file.open(mbFilename .. '.dnl','w+')
end
file.seek("set", mbOffset)
file.write(binaryData)
file.close()
binaryData = nil
if (fileSize == mbLen + mbOffset) then
file.remove(mbFilename)
file.rename(mbFilename .. '.dnl', mbFilename)
file.remove(mbFilename .. '.dnl')
if (string.sub(mbFilename, -4) == '.lua') then
file.remove(string.sub(mbFilename, 0, -3) .. "lc")
node.compile(mbFilename)
file.remove(mbFilename)
end
end
end
end
elseif (mbCmd == 'list') then
local remaining, used, total=file.fsinfo()
local headerExist = 0
connection:send('"files":{')
for name, size in pairs(file.list()) do
local isHttpFile = string.match(name, "(http/)") ~= nil
if isHttpFile then
if (headerExist > 0) then
connection:send(',')
end
local url = string.match(name, ".*/(.*)")
connection:send('"' .. url .. '":"' .. size .. '"')
headerExist = 1
end
end
connection:send('},')
connection:send('"total":"' .. total .. '",')
connection:send('"used":"' .. used .. '",')
connection:send('"free":"' .. remaining .. '"')
elseif (mbCmd == 'remove') then
if (fieldsCount > 1) then
if (mbFilename ~= 'upload.lua') and (mbFilename ~= 'upload.lc') and (mbFilename ~= 'upload.html.gz') then
file.remove('http/' .. mbFilename)
end
end
end
connection:send('}')
collectgarbage()
end