cordova-plugin-unvired-universal-sdk 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -0
- package/aar/README.md +3 -0
- package/aar/Unvired_Kernel_Android.aar +0 -0
- package/aar/Unvired_Kernel_HTML5_Android.aar +0 -0
- package/package.json +69 -0
- package/plugin.xml +23 -0
- package/src/android/build.gradle +35 -0
- package/src/android/xml/provider_paths.xml +21 -0
- package/src/browser/UnviredPluginProxy.js +2430 -0
- package/src/browser/bootstrap.min.js +17 -0
- package/src/browser/codemirror.js +9755 -0
- package/src/browser/jquery-3.2.1.js +10253 -0
- package/src/browser/sql-wasm.wasm +0 -0
- package/src/browser/sql.js +203 -0
- package/src/browser/src_index_worker_js.unvired-db-worker.js +231 -0
- package/src/browser/unvired-db-worker.js +166 -0
- package/src/browser/vendors-node_modules_comlink_dist_esm_comlink_mjs.unvired-db-worker.js +22 -0
- package/src/ios/AttachmentPlugin.h +21 -0
- package/src/ios/AttachmentPlugin.m +180 -0
- package/src/ios/DataStructureHelper.h +28 -0
- package/src/ios/DataStructureHelper.m +188 -0
- package/src/ios/IOSAuthPlugin.h +14 -0
- package/src/ios/IOSAuthPlugin.m +13 -0
- package/src/ios/IOSDatabasePlugin.h +28 -0
- package/src/ios/IOSDatabasePlugin.m +253 -0
- package/src/ios/IOSFWSettingsPlugin.h +65 -0
- package/src/ios/IOSFWSettingsPlugin.m +363 -0
- package/src/ios/IOSLoggerPlugin.h +34 -0
- package/src/ios/IOSLoggerPlugin.m +198 -0
- package/src/ios/IOSLoginPlugin.h +29 -0
- package/src/ios/IOSLoginPlugin.m +480 -0
- package/src/ios/IOSProxyPlugin.h +21 -0
- package/src/ios/IOSProxyPlugin.m +172 -0
- package/src/ios/IOSSyncEnginePlugin.h +54 -0
- package/src/ios/IOSSyncEnginePlugin.m +847 -0
- package/src/ios/PluginConstants.h +195 -0
- package/src/ios/PluginHelper.h +29 -0
- package/src/ios/PluginHelper.m +74 -0
- package/src/ios/SyncHTML5Response.h +50 -0
- package/src/ios/SyncHTML5Response.m +68 -0
- package/www/applicationMeta/applicationMetadataParser.ts +285 -0
- package/www/applicationMeta/fieldConstants.ts +92 -0
- package/www/attachment/attachmentHelper.ts +326 -0
- package/www/attachment/attachmentQHelper.ts +158 -0
- package/www/attachment/attachmentService.ts +259 -0
- package/www/authenticationService.ts +746 -0
- package/www/bootstrap.min.js +17 -0
- package/www/codemirror.js +9755 -0
- package/www/database/appDatabaseManager.ts +54 -0
- package/www/database/databaseManager.ts +616 -0
- package/www/helper/dbCreateTablesManager.ts +354 -0
- package/www/helper/frameworkHelper.ts +127 -0
- package/www/helper/frameworkSettingsManager.ts +287 -0
- package/www/helper/getMessageTimerManager.ts +81 -0
- package/www/helper/httpConnection.ts +1051 -0
- package/www/helper/logger.ts +312 -0
- package/www/helper/notificationListnerHelper.ts +56 -0
- package/www/helper/passcodeGenerator.ts +61 -0
- package/www/helper/reconciler.ts +1062 -0
- package/www/helper/serverResponseHandler.ts +677 -0
- package/www/helper/serviceConstants.ts +254 -0
- package/www/helper/settingsHelper.ts +386 -0
- package/www/helper/status.ts +83 -0
- package/www/helper/syncInputDataManager.ts +205 -0
- package/www/helper/unviredAccount.ts +104 -0
- package/www/helper/unviredAccountManager.ts +120 -0
- package/www/helper/urlService.ts +43 -0
- package/www/helper/userSettingsManager.ts +172 -0
- package/www/helper/utils.ts +110 -0
- package/www/inbox/downloadMessageService.ts +270 -0
- package/www/inbox/inboxHelper.ts +132 -0
- package/www/inbox/inboxService.ts +223 -0
- package/www/jquery-3.2.1.js +10253 -0
- package/www/kernel.js +1380 -0
- package/www/outbox/outboxAttachmentManager.ts +152 -0
- package/www/outbox/outboxHelper.ts +67 -0
- package/www/outbox/outboxService.ts +519 -0
- package/www/sql-wasm.wasm +0 -0
- package/www/sql.js +209 -0
- package/www/subtract.ts +5 -0
- package/www/sum.ts +4 -0
- package/www/syncEngine.ts +687 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { UnviredAccountManager } from "../helper/unviredAccountManager";
|
|
2
|
+
import { Logger } from "../helper/logger";
|
|
3
|
+
import FrameworkHelper from "../helper/frameworkHelper";
|
|
4
|
+
import DatabaseManager, { DatabaseType } from "../database/databaseManager";
|
|
5
|
+
|
|
6
|
+
const fileName = "AttachmentHelper";
|
|
7
|
+
|
|
8
|
+
export default class AttachmentHelper {
|
|
9
|
+
private static readonly ATTACHMENT_DIR = 'Attachment/';
|
|
10
|
+
|
|
11
|
+
// Helper method to create directories recursively
|
|
12
|
+
private static async createDirectoriesRecursively(dirPath: string): Promise<string> {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
// Split the path into parts
|
|
15
|
+
const pathParts = dirPath.split(/[\\\/]/).filter(part => part.length > 0);
|
|
16
|
+
let currentPath = '';
|
|
17
|
+
|
|
18
|
+
// Handle Windows drive letter
|
|
19
|
+
if (pathParts.length > 0 && pathParts[0].includes(':')) {
|
|
20
|
+
currentPath = pathParts[0] + '/';
|
|
21
|
+
pathParts.shift();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const createNextDirectory = (index: number) => {
|
|
25
|
+
if (index >= pathParts.length) {
|
|
26
|
+
// All directories created, resolve with the final path
|
|
27
|
+
resolve(dirPath);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
currentPath += pathParts[index] + '/';
|
|
32
|
+
|
|
33
|
+
window.resolveLocalFileSystemURL(currentPath,
|
|
34
|
+
(dirEntry) => {
|
|
35
|
+
// Directory exists, continue to next
|
|
36
|
+
Logger.logInfo(fileName, "createDirectoriesRecursively", "Directory exists: " + currentPath);
|
|
37
|
+
createNextDirectory(index + 1);
|
|
38
|
+
},
|
|
39
|
+
(error) => {
|
|
40
|
+
// Directory doesn't exist, create it
|
|
41
|
+
Logger.logInfo(fileName, "createDirectoriesRecursively", "Creating directory: " + currentPath);
|
|
42
|
+
|
|
43
|
+
// Get parent directory
|
|
44
|
+
const trimmedPath = currentPath.replace(/\/$/, ''); // Remove trailing slash if present
|
|
45
|
+
const parentPath = trimmedPath.substring(0, trimmedPath.lastIndexOf('/'));
|
|
46
|
+
window.resolveLocalFileSystemURL(parentPath,
|
|
47
|
+
(parentDir) => {
|
|
48
|
+
parentDir.getDirectory(pathParts[index], { create: true },
|
|
49
|
+
(newDir) => {
|
|
50
|
+
Logger.logInfo(fileName, "createDirectoriesRecursively", "Directory created: " + newDir.fullPath);
|
|
51
|
+
createNextDirectory(index + 1);
|
|
52
|
+
},
|
|
53
|
+
(createError) => {
|
|
54
|
+
Logger.logError(fileName, "createDirectoriesRecursively", "Failed to create directory: " + currentPath + " Error: " + createError);
|
|
55
|
+
reject(createError);
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
(parentError) => {
|
|
60
|
+
Logger.logError(fileName, "createDirectoriesRecursively", "Failed to get parent directory: " + parentPath + " Error: " + parentError);
|
|
61
|
+
reject(parentError);
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
createNextDirectory(0);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static getAttachmentDirectory(): Promise<string> {
|
|
73
|
+
return new Promise(async (resolve, reject) => {
|
|
74
|
+
if (FrameworkHelper.getPlatform() === "electron") {
|
|
75
|
+
const mobileUserSettings: any[] = await DatabaseManager.getInstance().select(DatabaseType.FrameworkDb, "MobileUserSettings", "keyName = 'ATTACHMENT_STORAGE_PATH'" );
|
|
76
|
+
if (mobileUserSettings.length > 0) {
|
|
77
|
+
const attachmentStoragePath = mobileUserSettings[0].description;
|
|
78
|
+
|
|
79
|
+
// Check if the path exists, if not create directories recursively
|
|
80
|
+
try {
|
|
81
|
+
const pathExists = await this.fileExists(attachmentStoragePath);
|
|
82
|
+
if (pathExists) {
|
|
83
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Path exists: " + attachmentStoragePath);
|
|
84
|
+
resolve(attachmentStoragePath);
|
|
85
|
+
} else {
|
|
86
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Path does not exist, creating directories: " + attachmentStoragePath);
|
|
87
|
+
const createdPath = await this.createDirectoriesRecursively(attachmentStoragePath);
|
|
88
|
+
resolve(createdPath);
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
Logger.logError(fileName, "getAttachmentDirectory", "Error checking/creating path: " + error);
|
|
92
|
+
resolve("");
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const account = UnviredAccountManager.getInstance().getLastLoggedInAccount()
|
|
99
|
+
const userDirectory = await FrameworkHelper.getFolderBasedOnUserId(account.getAccountId());
|
|
100
|
+
const inboxDir = `${userDirectory}${AttachmentHelper.ATTACHMENT_DIR}`;
|
|
101
|
+
window.resolveLocalFileSystemURL(inboxDir, (dir) => {
|
|
102
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Directory exists: " + dir.fullPath);
|
|
103
|
+
resolve(dir.nativeURL);
|
|
104
|
+
}, (error) => {
|
|
105
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Directory did not exist. Error: " + error);
|
|
106
|
+
// Directory did not exist, so creating it.
|
|
107
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Directory did not exist, so creating it: " + inboxDir);
|
|
108
|
+
window.resolveLocalFileSystemURL(userDirectory, (parentDir) => {
|
|
109
|
+
parentDir.getDirectory(AttachmentHelper.ATTACHMENT_DIR, {create: true}, (newDir) => {
|
|
110
|
+
Logger.logInfo(fileName, "getAttachmentDirectory", "Directory created: " + newDir.nativeURL);
|
|
111
|
+
resolve(newDir.nativeURL);
|
|
112
|
+
}, (error) => {
|
|
113
|
+
Logger.logError(fileName, "getAttachmentDirectory", "Unable to create Attachment directory. Error: " + error);
|
|
114
|
+
resolve("");
|
|
115
|
+
})
|
|
116
|
+
}, (error) => {
|
|
117
|
+
Logger.logError(fileName, "getAttachmentDirectory", "Unable to get Attachment directory. Error: " + error);
|
|
118
|
+
resolve("");
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static addAttachment(attachmentName: string, attachmentData: ArrayBuffer): Promise<string> {
|
|
125
|
+
return new Promise(async (resolve, reject) => {
|
|
126
|
+
const attachmentDir = await this.getAttachmentDirectory();
|
|
127
|
+
window.resolveLocalFileSystemURL(attachmentDir, (dirEntry) => {
|
|
128
|
+
dirEntry.getFile(`${attachmentName}`, { create: true }, (fileEntry) => {
|
|
129
|
+
fileEntry.createWriter((fileWriter) => {
|
|
130
|
+
fileWriter.onwriteend = () => {
|
|
131
|
+
Logger.logInfo("AttachmentHelper", "addAttachment", "Write completed");
|
|
132
|
+
resolve(fileEntry.nativeURL);
|
|
133
|
+
};
|
|
134
|
+
fileWriter.onerror = (e) => {
|
|
135
|
+
Logger.logError("AttachmentHelper", "addAttachment", "Write error: " + e);
|
|
136
|
+
reject(e);
|
|
137
|
+
};
|
|
138
|
+
const blob = new Blob([attachmentData]);
|
|
139
|
+
fileWriter.write(blob);
|
|
140
|
+
},
|
|
141
|
+
(error) => {
|
|
142
|
+
Logger.logError("AttachmentHelper", "addAttachment", "Create writer error: " + error);
|
|
143
|
+
reject(error);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
},
|
|
147
|
+
(error) => {
|
|
148
|
+
Logger.logError("AttachmentHelper", "addAttachment", "Get file error: " + error);
|
|
149
|
+
reject(error);
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
(error) => {
|
|
153
|
+
Logger.logError("AttachmentHelper", "addAttachment", "Resolve file system URL error: " + error);
|
|
154
|
+
reject(error);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static getAttachment(attachmentName: string): Promise<ArrayBuffer> {
|
|
160
|
+
return new Promise(async (resolve, reject) => {
|
|
161
|
+
const attachmentDir = await this.getAttachmentDirectory();
|
|
162
|
+
window.resolveLocalFileSystemURL(attachmentDir, (dirEntry) => {
|
|
163
|
+
dirEntry.getFile(`${attachmentName}`, { create: false }, (fileEntry) => {
|
|
164
|
+
fileEntry.file((file) => {
|
|
165
|
+
const reader = new FileReader();
|
|
166
|
+
reader.onloadend = () => {
|
|
167
|
+
Logger.logInfo("AttachmentHelper", "getAttachment", "Read completed");
|
|
168
|
+
resolve(reader.result as ArrayBuffer);
|
|
169
|
+
};
|
|
170
|
+
reader.onerror = (e) => {
|
|
171
|
+
Logger.logError("AttachmentHelper", "getAttachment", "Read error: " + e);
|
|
172
|
+
reject(e);
|
|
173
|
+
};
|
|
174
|
+
reader.readAsArrayBuffer(file);
|
|
175
|
+
},
|
|
176
|
+
(error) => {
|
|
177
|
+
Logger.logError("AttachmentHelper", "getAttachment", "Get file error: " + error);
|
|
178
|
+
reject(error);
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
(error) => {
|
|
182
|
+
Logger.logError("AttachmentHelper", "getAttachment", "Get file error: " + error);
|
|
183
|
+
reject(error);
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
error => {
|
|
187
|
+
Logger.logError("AttachmentHelper", "getAttachment", "Resolve file system URL error: " + error);
|
|
188
|
+
reject(error);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static deleteAttachment(attachmentName: string): Promise<void> {
|
|
194
|
+
return new Promise(async (resolve, reject) => {
|
|
195
|
+
const attachmentDir = await this.getAttachmentDirectory();
|
|
196
|
+
window.resolveLocalFileSystemURL(attachmentDir, (dirEntry) => {
|
|
197
|
+
dirEntry.getFile(`${attachmentName}`, { create: false }, (fileEntry) => {
|
|
198
|
+
fileEntry.remove(() => {
|
|
199
|
+
Logger.logInfo("AttachmentHelper", "deleteAttachment", "File removed");
|
|
200
|
+
resolve();
|
|
201
|
+
},
|
|
202
|
+
(error) => {
|
|
203
|
+
Logger.logError("AttachmentHelper", "deleteAttachment", "Remove file error: " + error);
|
|
204
|
+
reject(error);
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
},
|
|
208
|
+
(error) => {
|
|
209
|
+
Logger.logError("AttachmentHelper", "deleteAttachment", "Get file error: " + error);
|
|
210
|
+
reject(error);
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
(error) => {
|
|
214
|
+
Logger.logError("AttachmentHelper", "deleteAttachment", "Resolve file system URL error: " + error);
|
|
215
|
+
reject(error);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static readFileAsArrayBuffer(filePath: string): Promise<ArrayBuffer> {
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
window.resolveLocalFileSystemURL(filePath, (fileEntry) => {
|
|
223
|
+
fileEntry.file((file) => {
|
|
224
|
+
const reader = new FileReader();
|
|
225
|
+
reader.onloadend = function() {
|
|
226
|
+
Logger.logInfo("AttachmentHelper", "readFileAsArrayBuffer", "Read completed");
|
|
227
|
+
resolve(this.result as ArrayBuffer);
|
|
228
|
+
};
|
|
229
|
+
reader.onerror = function(err) {
|
|
230
|
+
Logger.logError("AttachmentHelper", "readFileAsArrayBuffer", "Read error: " + err);
|
|
231
|
+
reject(err);
|
|
232
|
+
};
|
|
233
|
+
reader.readAsArrayBuffer(file);
|
|
234
|
+
},
|
|
235
|
+
(error) => {
|
|
236
|
+
Logger.logError("AttachmentHelper", "readFileAsArrayBuffer", "Get file error: " + error);
|
|
237
|
+
reject(error);
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
(error) => {
|
|
241
|
+
Logger.logError("AttachmentHelper", "readFileAsArrayBuffer", "Resolve file system URL error: " + error);
|
|
242
|
+
reject(error);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static async fileExists(filePath: string): Promise<boolean> {
|
|
248
|
+
return new Promise((resolve) => {
|
|
249
|
+
window.resolveLocalFileSystemURL(filePath,
|
|
250
|
+
() => resolve(true), // File exists
|
|
251
|
+
() => resolve(false) // File doesn't exist
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Mainly used for electron/windows platform.
|
|
257
|
+
static async readExternalFile(filePath: string): Promise<ArrayBuffer> {
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
window.resolveLocalFileSystemURL(filePath, (fileEntry) => {
|
|
260
|
+
fileEntry.file((file) => {
|
|
261
|
+
const reader = new FileReader();
|
|
262
|
+
reader.onloadend = function() {
|
|
263
|
+
Logger.logInfo(fileName, "readExternalFile", "Read completed");
|
|
264
|
+
resolve(this.result as ArrayBuffer);
|
|
265
|
+
};
|
|
266
|
+
reader.onerror = function(err) {
|
|
267
|
+
Logger.logError(fileName, "readExternalFile", "Read error: " + err);
|
|
268
|
+
reject(err);
|
|
269
|
+
};
|
|
270
|
+
reader.readAsArrayBuffer(file);
|
|
271
|
+
},
|
|
272
|
+
(error) => {
|
|
273
|
+
Logger.logError(fileName, "readExternalFile", "Get file error: " + error);
|
|
274
|
+
reject(error);
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
(error) => {
|
|
278
|
+
Logger.logError(fileName, "readExternalFile", "Resolve file system URL error: " + error);
|
|
279
|
+
reject(error);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Mainly used for electron/windows platform.
|
|
285
|
+
static async writeExternalFile(filePath: string, fileName: string, data: ArrayBuffer): Promise<void> {
|
|
286
|
+
return new Promise((resolve, reject) => {
|
|
287
|
+
window.resolveLocalFileSystemURL(filePath, (dirEntry) => {
|
|
288
|
+
dirEntry.getFile(`${fileName}`, { create: true }, (fileEntry) => {
|
|
289
|
+
fileEntry.createWriter((fileWriter) => {
|
|
290
|
+
fileWriter.onwriteend = () => {
|
|
291
|
+
Logger.logInfo("AttachmentHelper", "writeExternalFile", "Write completed");
|
|
292
|
+
resolve();
|
|
293
|
+
};
|
|
294
|
+
fileWriter.onerror = (e) => {
|
|
295
|
+
Logger.logError("AttachmentHelper", "writeExternalFile", "Write error: " + e);
|
|
296
|
+
reject(e);
|
|
297
|
+
};
|
|
298
|
+
const blob = new Blob([data]);
|
|
299
|
+
fileWriter.write(blob);
|
|
300
|
+
},
|
|
301
|
+
(error) => {
|
|
302
|
+
Logger.logError("AttachmentHelper", "writeExternalFile", "Create writer error: " + error);
|
|
303
|
+
reject(error);
|
|
304
|
+
}
|
|
305
|
+
);
|
|
306
|
+
},
|
|
307
|
+
(error) => {
|
|
308
|
+
Logger.logError("AttachmentHelper", "writeExternalFile", "Get file error: " + error);
|
|
309
|
+
reject(error);
|
|
310
|
+
});
|
|
311
|
+
},
|
|
312
|
+
(error) => {
|
|
313
|
+
Logger.logError("AttachmentHelper", "writeExternalFile", "Resolve file system URL error: " + error);
|
|
314
|
+
reject(error);
|
|
315
|
+
});
|
|
316
|
+
})
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static async getAttachmentPath(attachmentName: string): Promise<string> {
|
|
320
|
+
return new Promise(async (resolve, reject) => {
|
|
321
|
+
const attachmentDir = await this.getAttachmentDirectory();
|
|
322
|
+
resolve(`${attachmentDir}${attachmentName}`);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import FrameworkHelper from "../helper/frameworkHelper";
|
|
2
|
+
import { Logger } from "../helper/logger";
|
|
3
|
+
import * as ServiceConstants from "../helper/serviceConstants";
|
|
4
|
+
import { DatabaseManager, DatabaseType } from "../database/databaseManager";
|
|
5
|
+
import { UnviredAccountManager } from "../helper/unviredAccountManager";
|
|
6
|
+
import { AuthenticationService } from "../authenticationService";
|
|
7
|
+
import { FrameworkSettingsFields, FrameworkSettingsManager } from "../helper/frameworkSettingsManager";
|
|
8
|
+
import { ObjectStatus, SyncStatus } from "../helper/utils";
|
|
9
|
+
import { HttpConnection } from "../helper/httpConnection";
|
|
10
|
+
import { Status } from "../helper/status";
|
|
11
|
+
import AttachmentHelper from "./attachmentHelper";
|
|
12
|
+
import { kAttachmentItem, kError, NotificationListenerHelper, NotificationListnerType } from "../helper/notificationListnerHelper";
|
|
13
|
+
|
|
14
|
+
const fileName = "AttachmentQHelper";
|
|
15
|
+
export default class AttachmentQHelper {
|
|
16
|
+
static async checkAttachmentAndQueueForAutoDownload(
|
|
17
|
+
attachmentItemName: string, attachmentItem: any): Promise<void> {
|
|
18
|
+
if (FrameworkHelper.getPlatform() === "browser") {
|
|
19
|
+
await Logger.logError(fileName, "checkAttachmentAndQueueForAutoDownload", "Attachment download is not supported in Web.");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
attachmentItem[ServiceConstants.AttachmentItemFieldAttachmentStatus] = ServiceConstants.AttachmentStatusDefault;
|
|
24
|
+
try {
|
|
25
|
+
await DatabaseManager.getInstance().update(DatabaseType.AppDb, attachmentItemName, attachmentItem, `${ServiceConstants.AttachmentItemFieldUid} = '${attachmentItem[ServiceConstants.AttachmentItemFieldUid]}'`);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
await Logger.logError(
|
|
28
|
+
"AttachmentHelper",
|
|
29
|
+
"checkAttachmentAndQueueForAutodownload",
|
|
30
|
+
`Error while updating the status of the attachment item. Error: ${e}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check if it is marked for auto download and queue request if required
|
|
34
|
+
const autoDownload = attachmentItem[ServiceConstants.AttachmentItemFieldAutoDownload];
|
|
35
|
+
if (autoDownload != null && autoDownload === "true") {
|
|
36
|
+
const isDatabaseOperationSuccessful = await AttachmentQHelper.queueForDownload(
|
|
37
|
+
attachmentItemName, attachmentItem, ServiceConstants.FwAttachmentAutoDownloadPriority);
|
|
38
|
+
if (!isDatabaseOperationSuccessful) {
|
|
39
|
+
await Logger.logError(
|
|
40
|
+
"AttachmentHelper",
|
|
41
|
+
"checkAttachmentAndQueueForAutodownload",
|
|
42
|
+
"Failed to Queue Attachment Item for Download");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static async queueForDownload(attachmentItemName: string, attachmentItem: any, priority: number): Promise<boolean> {
|
|
48
|
+
// In Demo Mode, Attachments cannot be queued for Download.
|
|
49
|
+
// Therefore we log an Error Message saying that this feature is not supported in demo Mode.
|
|
50
|
+
|
|
51
|
+
const demoMode = await FrameworkSettingsManager.getInstance().getFieldValue(FrameworkSettingsFields.isDemo);
|
|
52
|
+
if (demoMode) {
|
|
53
|
+
// Create an Error Object.
|
|
54
|
+
attachmentItem[ServiceConstants.AttachmentItemFieldMessage] =
|
|
55
|
+
"Downloading of attachments is not supported in demo mode.";
|
|
56
|
+
attachmentItem[ServiceConstants.AttachmentItemFieldAttachmentStatus] =
|
|
57
|
+
ServiceConstants.AttachmentStatusErrorInDownload;
|
|
58
|
+
|
|
59
|
+
// Update the Attachment in the Database as well.
|
|
60
|
+
try {
|
|
61
|
+
await DatabaseManager.getInstance().update(DatabaseType.AppDb, attachmentItemName, attachmentItem, `${ServiceConstants.AttachmentItemFieldUid} = '${attachmentItem[ServiceConstants.AttachmentItemFieldUid]}'`);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
await Logger.logError("AttachmentHelper", "queueForDownload",
|
|
64
|
+
`There was an error while downloading Attachments. Error: ${e}`);
|
|
65
|
+
}
|
|
66
|
+
const notifData = {
|
|
67
|
+
[kAttachmentItem]: attachmentItem,
|
|
68
|
+
[kError]: "Downloading of attachments is not supported in demo mode."
|
|
69
|
+
}
|
|
70
|
+
NotificationListenerHelper.postDataSenderNotification(notifData, NotificationListnerType.attachmentDownloadError);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (attachmentItem == null || Object.keys(attachmentItem).length === 0) {
|
|
74
|
+
throw new Error("Attachment Item is null.");
|
|
75
|
+
}
|
|
76
|
+
await AttachmentQHelper.addAttachmentQForDownload(attachmentItemName, attachmentItem, priority);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private static async addAttachmentQForDownload(attachmentItemName: string, attachmentItem: any, priority: number): Promise<void> {
|
|
81
|
+
const structMetas = await DatabaseManager.getInstance().select(DatabaseType.FrameworkDb, "StructureMeta");
|
|
82
|
+
//get AttachmentStructure
|
|
83
|
+
const attachmentStructMeta = structMetas.find(
|
|
84
|
+
(element) => element.structureName === attachmentItemName);
|
|
85
|
+
if (!attachmentStructMeta) {
|
|
86
|
+
await Logger.logError("AttachmentDownloader", "queueForDownload",
|
|
87
|
+
`Structure Meta for Attachment Item (${attachmentItemName}) is not found.`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
//get BEHeader
|
|
91
|
+
const headerStructMeta = structMetas.find(
|
|
92
|
+
(element) =>
|
|
93
|
+
element.beName === attachmentStructMeta.beName &&
|
|
94
|
+
element.isHeader === "1");
|
|
95
|
+
if (!headerStructMeta) {
|
|
96
|
+
await Logger.logError("AttachmentDownloader", "queueForDownload",
|
|
97
|
+
`Header Structure Meta for Attachment Item (${attachmentItemName}) is not found.`);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const timestamp = Date.now();
|
|
101
|
+
const uid = attachmentItem[ServiceConstants.AttachmentItemFieldUid] ?? "";
|
|
102
|
+
|
|
103
|
+
//create attachmentQObject and save it
|
|
104
|
+
const attachmentQObjectData = {
|
|
105
|
+
lid: FrameworkHelper.getUUID(),
|
|
106
|
+
timestamp: timestamp,
|
|
107
|
+
objectStatus: ObjectStatus.add,
|
|
108
|
+
syncStatus: SyncStatus.none,
|
|
109
|
+
uid: uid,
|
|
110
|
+
beName: attachmentStructMeta.beName,
|
|
111
|
+
beHeaderName: headerStructMeta.structureName,
|
|
112
|
+
beAttachmentStructName: attachmentStructMeta.structureName,
|
|
113
|
+
priority: priority,
|
|
114
|
+
timeStamp: timestamp
|
|
115
|
+
};
|
|
116
|
+
await Logger.logDebug("AttachmentDownloader", "queueForDownload",
|
|
117
|
+
`ATTACHMENT QUEUED LID ${attachmentQObjectData.lid} HEADER NAME : ${headerStructMeta.structureName} STRUCT NAME : ${attachmentStructMeta.structureName}`);
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
// Add the Attachment Q Object to the Attachment Q
|
|
121
|
+
await DatabaseManager.getInstance().insert(DatabaseType.FrameworkDb, "AttachmentQObject", attachmentQObjectData, true);
|
|
122
|
+
} catch (e) {
|
|
123
|
+
await Logger.logDebug("AttachmentDownloader", "queueForDownload",
|
|
124
|
+
`ATTACHMENT QUEUE FAILED...... LID ${attachmentQObjectData.lid} HEADER NAME : ${headerStructMeta.structureName} STRUCT NAME : ${attachmentStructMeta.structureName}`);
|
|
125
|
+
await Logger.logError("AttachmentDownloader", "queueForDownload",
|
|
126
|
+
`Error while adding attachment Q object to the Attachment Q. Error: ${e}`);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
//change status to queued for download
|
|
130
|
+
attachmentItem[ServiceConstants.AttachmentItemFieldAttachmentStatus] =
|
|
131
|
+
ServiceConstants.AttachmentStatusQueuedForDownload;
|
|
132
|
+
|
|
133
|
+
// Update the Attachment Status
|
|
134
|
+
try {
|
|
135
|
+
await DatabaseManager.getInstance().update(DatabaseType.AppDb, attachmentItemName, attachmentItem, `${ServiceConstants.AttachmentItemFieldUid} = '${attachmentItem[ServiceConstants.AttachmentItemFieldUid]}'`);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
await Logger.logError("AttachmentDownloader", "queueForDownload",
|
|
138
|
+
`DB Error while trying to mark Attachment as Queued for download. Error: ${e}`);
|
|
139
|
+
}
|
|
140
|
+
// call start AttachmentDownloadThread
|
|
141
|
+
//start();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static async downloadAttachmentAndGetPath(uid: string, fileName: string): Promise<any> {
|
|
145
|
+
const result = await new HttpConnection().downloadAttachment(uid);
|
|
146
|
+
if (result.status === Status.ok) {
|
|
147
|
+
const arrayBuffer = await result.arrayBuffer();
|
|
148
|
+
const attachmentPath = await AttachmentHelper.addAttachment(fileName, arrayBuffer);
|
|
149
|
+
return attachmentPath;
|
|
150
|
+
} else {
|
|
151
|
+
await Logger.logDebug("AttachmentHelper", "downloadAttachmentAndGetPath",
|
|
152
|
+
`Failed to download attachment. Result: ${result.toString()}`);
|
|
153
|
+
await Logger.logError("AttachmentHelper", "downloadAttachmentAndGetPath",
|
|
154
|
+
'Failed to download attachment.');
|
|
155
|
+
throw result;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|