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
121 lines (99 loc) · 2.89 KB
/
Copy pathupload.lua
File metadata and controls
121 lines (99 loc) · 2.89 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
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 ~= 'http/upload.lua') then
connection:send('"offset":"' .. mbOffset .. '",')
connection:send('"len":"' .. mbLen .. '",')
connection:send('"filename":"' .. mbFilename .. '"')
for i=1,string.len(mbData),2 do
currentByte = tonumber(string.sub(mbData, i, i + 1), 16)
binaryData = binaryData .. string.char(currentByte)
end
local mbTmpFilename = string.sub(mbFilename, 0, 27) .. '.dnl'
if (mbOffset > 0) then
file.open(mbTmpFilename,'a+')
else
file.remove(mbTmpFilename)
file.open(mbTmpFilename,'w+')
end
file.seek("set", mbOffset)
file.write(binaryData)
file.close()
binaryData = nil
if (fileSize == mbLen + mbOffset) then
file.remove(mbFilename)
file.rename(mbTmpFilename, mbFilename)
file.remove(mbTmpFilename)
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
if (headerExist > 0) then
connection:send(',')
end
local url = string.match(name, ".*/(.*)")
url = name
connection:send('"' .. url .. '":"' .. size .. '"')
headerExist = 1
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 ~= 'http/upload.lua') and (mbFilename ~= 'http/upload.lc') and (mbFilename ~= 'http/upload.html.gz') then
file.remove(mbFilename)
end
end
end
connection:send('}')
collectgarbage()
end