-
Notifications
You must be signed in to change notification settings - Fork 9
/
background-task.ios.js
executable file
·356 lines (263 loc) · 7.16 KB
/
background-task.ios.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
var fs = require("@nativescript/core/file-system")
exports.getFile = function(args){
var CompleteCallback = createCallback(args)
var toFile = args.toFile
var url = args.url
var identifier = args.identifier + ""
var partBytesSize = args.partBytesSize
var checkPartialDownload = args.checkPartialDownload
var debug = args.debug
var task = NSBackgroundTaskHttpRequestToFile.alloc().initWithUrlToFileIdentifier(url, toFile, identifier)
task.setCheckPartialDownload(checkPartialDownload == undefined ? false : checkPartialDownload)
task.setPartBytesSize(partBytesSize || 0)
task.setDebug(debug == undefined ? false : debug)
if(args.headers){
for(var i in args.headers){
var header = args.headers[i]
for(var key in header){
task.addHeaderWithNameAndValue(key, header[key])
}
}
}
task.delegate = CompleteCallback.new()
task.runTask();
}
exports.unzip = function(args){
var CompleteCallback = createCallback(args)
var task = NSBackgroundTaskUnzipTask.alloc().initWithFromFileToFile(args.fromFile, args.toFile)
task.delegate = CompleteCallback.new()
task.runTask();
}
exports.copyFiles = function(args){
var CompleteCallback = createCallback(args)
var task = NSBackgroundTaskCopyFiles.alloc().initWithFromFileToFile(args.fromFile, args.toFile)
task.delegate = CompleteCallback.new()
task.runTask();
}
/*
args = {
files: [
{
image:
fileDst:
fileSrc:
quality:
}
]
}
*/
exports.saveLargeFiles = function(args){
var CompleteCallback = createCallback(args)
try{
if(!args.files || args.files.length == 0){
if(args.doneCallback){
args.doneCallback()
return
}
}
var task = NSLargeFilePersisterTask.new()
task.delegate = CompleteCallback.new()
for(var i in args.files) {
var item = args.files[i]
var largeFile = NSLargeFile.new()
if (item.image instanceof UIImage)
largeFile.image = item.image
else if(item.image && item.image.ios)
largeFile.image = item.image.ios
largeFile.fileDst = item.fileDst
largeFile.fileSrc = item.fileSrc
largeFile.quality = item.quality || 0
task.addLargeFile(largeFile)
}
task.runTask()
}catch(error){
console.log("BackgroundTask.saveLargeFiles error=" + error)
if(args.errorCallback)
args.errorCallback(error)
}
}
/*
args = {
files: [
{
fileSrc:
filePartPath:
fileParthName:
fileParteSufix: default is "part"
filePartMaxSize: default is 5 (5MB)
}
]
}
*/
exports.splitFiles = function(args){
var CompleteCallback = createCallback(args)
try{
if(!args.files || args.files.length == 0){
if(args.doneCallback)
args.doneCallback()
}
var task = NSSplitFileTask.new()
task.delegate = CompleteCallback.new()
for(var i in args.files) {
var item = args.files[i]
var splitFile = NSSplitFile.new()
splitFile.fileSrc = item.fileSrc
splitFile.filePartPath = item.filePartPath
splitFile.filePartMaxSize = item.filePartMaxSize || splitFile.filePartMaxSize
splitFile.filePartName = item.filePartName
splitFile.filePartSufix = item.filePartSufix || splitFile.filePartSufix
task.addSplitFile(splitFile)
}
task.runTask()
}catch(error){
console.log("BackgroundTask.splitFiles error=" + error)
if(args.errorCallback)
args.errorCallback(error)
}
}
/*
args = {
url:
items: [
{
jsonKey:
fileSrc:
data: - json data
}
]
headers: [
{}
]
}
*/
exports.postFiles = function(args){
var CompleteCallback = createCallback(args)
try{
var task = NSHttpPostFileTask.alloc().initWithUrl(args.url)
task.delegate = CompleteCallback.new()
if(args.formData){
task.setUseFormData(true)
}
if(args.gzip){
task.setUseGzip(true)
}
if(args.debug)
task.setDebug(true)
for(var i in args.items){
var jsonItem = args.items[i]
var fileSrc = jsonItem.fileSrc
var jsonKey = jsonItem.jsonKey
var jsonData = jsonItem.data
var httpPostFile = NSHttpPostFile.alloc().initWithFileSrcJsonKey(fileSrc, jsonKey)
httpPostFile.identifier = jsonItem.identifier
for(var key in jsonData){
httpPostFile.addJsonKeyValue(key, jsonData[key])
}
task.addPostFile(httpPostFile)
}
if(args.gzip == false)
task.setUseGzip(false)
if(args.headers){
for(var i in args.headers){
var header = args.headers[i]
for(var key in header){
task.addHeaderWithNameAndValue(key, header[key])
}
}
}
task.runTask()
}catch(error){
console.log("BackgroundTask.postFiles error=" + error)
if(args.errorCallback)
args.errorCallback(error)
}
}
/*
insert/update/delete
args = {
dbName
items: [
{
query:
args
}
]
}
insert or update
args = {
dbName
items: [
{
insertQuery:
updateQuery:
tableName:
updateKey: // where column name
updateKeyValue: // where column value
params:
}
]
}
*/
exports.dbBatch = function(args){
var CompleteCallback = createCallback(args)
var dbPath = fs.path.join(fs.knownFolders.documents().path, args.dbName)
try{
var task = NSDbBatchTask.alloc().initWithDbPath(dbPath)
task.delegate = CompleteCallback.new()
if(args.debug)
task.setDebug(true)
for(var i in args.items){
var item = args.items[i]
var query = NSQuery.new()
if(item.query){
query.query = item.query
query.params = item.args
} else {
query.insertQuery = item.insertQuery
query.updateQuery = item.updateQuery
query.tableName = item.tableName
query.updateKey = item.updateKey + ""
query.updateKeyValue = item.updateKeyValue + ""
query.updateKeyDataType = item.updateKeyDataType || "text"
query.params = item.args
}
task.addQuery(query)
}
task.runTask()
}catch(error){
console.log("BackgroundTask.dbBatchInsert error=" + error)
if(args.errorCallback)
args.errorCallback(error)
}
}
function createCallback(args){
var CompleteCallback = (function(_super){
__extends(CompleteCallback, _super);
function CompleteCallback(){
_super.apply(this, arguments);
}
CompleteCallback.prototype.onComplete = function(result){
if(args.doneCallback)
invokeOnRunLoop(()=>{
args.doneCallback(result)
})
}
CompleteCallback.prototype.onError = function(message){
if(args.errorCallback)
invokeOnRunLoop(()=>{
args.errorCallback(message)
})
}
CompleteCallback.ObjCProtocols = [NSBackgroundTaskCompleteCallback]
return CompleteCallback
}(NSObject))
return CompleteCallback
}
let invokeOnRunLoop = (function() {
var runloop = CFRunLoopGetMain();
return function(func) {
CFRunLoopPerformBlock(runloop, kCFRunLoopDefaultMode, func);
CFRunLoopWakeUp(runloop);
}
}());
exports.invokeOnRunLoop = invokeOnRunLoop