-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabHttp.lua
More file actions
273 lines (233 loc) · 7.43 KB
/
tabHttp.lua
File metadata and controls
273 lines (233 loc) · 7.43 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
g_t.httpGetJson = _{
s1 = function(c, uri, timeout)
c.uri = uri
c.requestId = CS.Utils.HttpRequestGet(uri, timeout or 10, function(status, downloadText)
c:_onRespond(status, downloadText)
end)
c.json = require("cjson")
end,
_onRespond = function(c, status, downloadText)
c.requestEnd = true
if status == 1 then
local isOk, ret = pcall(function() return c.json.decode(downloadText) end)
if isOk then
c:output(true, ret)
else
c:output(false)
end
else
c:output(false)
end
c:stop()
end,
final = function(c)
if not c.requestEnd then
CS.Utils.StopRequest(c.requestId)
end
end,
s1_event = g_t.empty_event,
__addNickName = function(c)
c._nickName = "httpGetJson<" .. (c.uri) .. ">"
end,
}
g_t.httpGetJsonWithCompete = _{
s1 = function(c, urls, timeout)
local tabs = {}
for _, url in ipairs(urls) do
local tab = g_t.httpGetJson(url, timeout)
table.insert(tabs, tab)
end
c:call(g_t.compete(tabs), "s2", {"index", "json"})
end,
s3 = function(c)
c:output(c.index~=nil, c.json)
end,
__addNickName = function(c)
c._nickName = "httpGetJsonWithCompete"
end,
}
g_t.httpGetData = _{
s1 = function(c, uri, timeout)
c.requestId = CS.Utils.HttpRequestGet(uri, timeout or 10, function(status, downloadText, data)
c:_onRespond(status, downloadText, data)
end)
end,
_onRespond = function(c, status, downloadText, data)
c.requestEnd = true
if status == 1 then
c:output(true, data)
else
c:output(false)
end
c:stop()
end,
final = function(c)
if not c.requestEnd then
CS.Utils.StopRequest(c.requestId)
end
end,
s1_event = g_t.empty_event,
__addNickName = function(c)
c._nickName = "httpGetData"
end,
}
g_t.httpPost = _{
s1 = function(c, uri, postData, contentType, timeout)
c.uri = uri
c.requestId = CS.Utils.HttpRequestPost(uri, postData, contentType or "application/json", timeout or 10,function(status, downloadText)
c:_onRespond(status, downloadText)
end)
end,
_onRespond = function(c, status, downloadText)
c.requestEnd = true
if status == 1 then
c:output(true, downloadText)
else
c:output(false)
end
c:stop()
end,
final = function(c)
if not c.requestEnd then
CS.Utils.StopRequest(c.requestId)
end
end,
s1_event = g_t.empty_event,
}
g_t.httpPostFormData = _{
s1 = function(c, uri, fromData, timeout)
c.uri = uri
c.requestId = CS.Utils.HttpRequestPostFormData(uri, fromData, timeout or 10, function(status, downloadText)
c:_onRespond(status, downloadText)
end)
end,
_onRespond = function(c, status, downloadText)
c.requestEnd = true
if status == 1 then
c:output(true, downloadText)
else
c:output(false)
end
c:stop()
end,
final = function(c)
if not c.requestEnd then
CS.Utils.StopRequest(c.requestId)
end
end,
s1_event = g_t.empty_event,
}
g_t.httpError = {
webRequestError = 1,
fileSizeNotMatch = 2,
md5NotMatch = 3,
}
-- if fileSize is not nil, check the tmp file size first then continue from where last time aborted
-- if you do not need this behavior, delete the tmp file by yourself.
g_t.httpSafeDownload = _{
s1 = function(c, url, savePath, enableProgressEvent, fileSize, md5, tmpPath, progressInterval)
c.startTime = require("socket").gettime()
local downloadedSize = 0
c.savePath = savePath
tmpPath = tmpPath or (c.savePath .. ".tmp")
c.tmpPath = tmpPath
c.md5 = md5
c.fileSize = fileSize
c.url = url
if g_t.debug then
c:__addNickName()
end
c.report = {url = url, savePath = savePath, existSize = downloadedSize, totalSize = fileSize, md5 = md5}
if c:fileExists(tmpPath) then
if fileSize ~= nil then
local tmpFileSize = c:getFileSize(tmpPath)
if tmpFileSize == fileSize then
c.ret = {result = true}
c:start("s3")
return
elseif tmpFileSize > fileSize then
c:deleteFile(tmpPath)
else
downloadedSize = tmpFileSize
end
else
c:deleteFile(tmpPath)
end
end
local csTab = CS.TabHttp.TabDownloadFile(url, tmpPath, function(downloaded)
if enableProgressEvent then
c:upwardNotify("downloadProgress", downloaded)
end
end, downloadedSize, progressInterval or 500)
c.report.existSize = downloadedSize
c:call(g_t.tabCS(csTab) >> "ret", "s2")
end,
s3 = function(c)
c.report.result = c.ret and c.ret.result
c.report.time = require("socket").gettime() - c.startTime
if c.ret.result == true then
if c.fileSize ~= nil then
local downloadedSize = c:getFileSize(c.tmpPath)
if downloadedSize ~= c.fileSize then
c:deleteFile(c.tmpPath)
c.report.error = g_t.httpError.fileSizeNotMatch
c:output(false, g_t.httpError.fileSizeNotMatch, c.report)
return
end
end
if c.md5 ~= nil then
local md5 = c:getFileMd5(c.tmpPath)
if md5 ~= c.md5 then
c:deleteFile(c.tmpPath)
c.report.error = g_t.httpError.md5NotMatch
c:output(false, g_t.httpError.md5NotMatch, c.report)
return
end
end
if c:fileExists(c.savePath) then
c:deleteFile(c.savePath)
end
c:moveFile(c.tmpPath, c.savePath)
local downloadedSize = c:getFileSize(c.savePath)
c:upwardNotify("downloadProgress", downloadedSize)
c.report.fileSize = downloadedSize
c:output(true, nil, c.report)
else
c.report.error = g_t.httpError.webRequestError
c:output(false, g_t.httpError.webRequestError, c.report)
return
end
end,
__addNickName = function(c)
c._nickName = "safeDownload:" .. c.url
end,
--private:
fileExists = function(c, path)
return CS.Framework.FileUtils.IsFileExists(path)
end,
deleteFile = function(c, path)
CS.System.IO.File.Delete(path)
end,
getFileSize = function(c, path)
return CS.Utils.GetFileSize(path)
end,
getFileMd5 = function(c, path)
return CS.Framework.MD5Utils.GetFileMD5(path)
end,
moveFile = function(c, oldPath, newPath)
CS.System.IO.File.Move(oldPath, newPath)
end,
}
g_t.tabGetFileSize = _{
s1 = function(c, url)
local csTab = CS.TabHttp.TabGetFileSize(url)
c:call(g_t.tabCS(csTab) >> "result", "s2")
end,
s3 = function(c)
if c.result.isSuccess then
c:output(c.result.size)
else
c:output(-1, c.result)
end
end,
}