-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
396 lines (312 loc) · 12.4 KB
/
preload.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const { contextBridge, ipcRenderer,shell } = require('electron');
const fs = require('fs');
const path = require('path');
const FormData = require('form-data');
const axios = require('axios');
const archiver = require('archiver');
const rimraf = require('rimraf');
const fetch = require('node-fetch');
const notifier = require('node-notifier');
const sendFileToAPI = async (filePath, apiUrl, accessToken, username) => {
const folderName_withzip = path.basename(filePath);
const folderName = path.parse(filePath).name;
const fileStream = fs.createReadStream(filePath);
const linkedDevice = localStorage.getItem('linkedDevice');
const appLiscence = localStorage.getItem('appLiscence');
const formData = new FormData();
formData.append('directory_path', folderName);
formData.append('username', username);
formData.append('files', fileStream, {
filename: folderName_withzip,
});
formData.append('deviceUID', linkedDevice);
formData.append('appLiscence', appLiscence);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
...formData.getHeaders()
},
body: formData,
});
if (response.ok) {
const responseData = await response.text();
return response;
} else {
const errorResponse = await response.text();
console.error('API Error Response:', errorResponse);
throw new Error(`API Error: ${response.statusText}`);
}
};
const createZipFromDirectory = async (directoryPath) => {
return new Promise(async (resolve, reject) => {
const zipFilePath = `${directoryPath}.zip`;
const output = fs.createWriteStream(zipFilePath);
const archive = archiver('zip', {
zlib: { level: 9 },
});
output.on('close', async () => {
resolve(zipFilePath);
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
archive.directory(directoryPath, false);
archive.finalize();
});
};
const setDirectoryPermissions = (directoryPath, mode) => {
fs.readdirSync(directoryPath).forEach((file) => {
const filePath = path.join(directoryPath, file);
fs.chmodSync(filePath, mode);
if (fs.statSync(filePath).isDirectory()) {
setDirectoryPermissions(filePath, mode);
}
});
};
contextBridge.exposeInMainWorld('versions', {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
ping: () => ipcRenderer.invoke('ping'),
deleteDirectory: () => ipcRenderer.invoke('deleteDirectory'),
createDirectory: (username) => {
try {
const projectPath = './';
const directoryPath = path.join(projectPath, 'Dentread', username);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
fs.chmodSync(directoryPath, 0o777);
setDirectoryPermissions(directoryPath, 0o777);
localStorage.setItem('dentread_dir', directoryPath);
return { success: true, message: `Directory created at ${directoryPath}`, directoryPath };
} else {
localStorage.setItem('dentread_dir', directoryPath);
return { success: false, message: 'Directory already exists' };
}
} catch (error) {
return { success: false, message: error.message };
}
},
deleteDirectory: () => {
try {
const projectPath = './';
const directoryPath = path.join(projectPath, 'Dentread');
const localStorageValue = localStorage.getItem('prefSyncOption');
if (fs.existsSync(directoryPath)) {
if (localStorageValue==='manualSync'){
rimraf.sync(directoryPath);
}
else
{
console.log("no manual sync found")
}
return { success: true, message: `Directory deleted: ${directoryPath}` };
} else {
return { success: false, message: 'Directory does not exist' };
}
// }
} catch (error) {
return { success: false, message: error.message };
}
},
emptyDirectory: (directoryName) => {
try {
let currentTimelocal = new Date().toLocaleString();
const projectPath = './';
const username = localStorage.getItem('savedUsername');
const dentreadDirectoryPath = path.join(projectPath, 'Dentread');
const usernameDirectoryPath = path.join(dentreadDirectoryPath, username);
const sanitizedDirectoryName = directoryName.trim(); // Remove leading and trailing spaces
const targetPath = path.join(usernameDirectoryPath, sanitizedDirectoryName);
console.log(targetPath,"targetPath")
if (fs.existsSync(targetPath)) {
if (fs.lstatSync(targetPath).isDirectory()) {
const directoryContents = fs.readdirSync(targetPath);
for (const item of directoryContents) {
const itemPath = path.join(targetPath, item);
if (fs.lstatSync(itemPath).isDirectory()) {
rimraf.sync(itemPath);
} else {
fs.unlinkSync(itemPath);
console.log(`Error deleting ${itemPath}: ${unlinkError.message}`);
}
}
fs.rmdirSync(targetPath);
console.log(`At [${currentTimelocal}] : Sync completed for: ${targetPath}`);
return { success: true, message: `Directory emptied: ${targetPath}` };
} else if (fs.lstatSync(targetPath).isFile()) {
fs.unlinkSync(targetPath);
console.log(`At [${currentTimelocal}] : Sync completed for: ${targetPath}`);
return { success: true, message: `File removed: ${targetPath}` };
} else {
return { success: false, message: 'Invalid target: Neither a file nor a directory' };
}
} else {
return { success: false, message: 'Target does not exist' };
}
} catch (error) {
return { success: false, message: error.message };
}
},
copyFilesWithCondition: function namedFunction(sourceDirectory, destinationDirectory, fileExtensions) {
try {
if (!fs.existsSync(destinationDirectory)) {
fs.mkdirSync(destinationDirectory, { recursive: true });
}
let currentTime = new Date().toLocaleString();
let currentonlytime = new Date().getTime();
const timeslot = localStorage.getItem('timeslot');
const defaultTimeslot = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const timeslotValue = timeslot ? parseInt(timeslot, 10) : defaultTimeslot;
const twentyFourHoursAgo = currentonlytime - timeslotValue;
const items = fs.readdirSync(sourceDirectory);
let totalCopied = 0;
function processNextItem() {
if (totalCopied >= 5) return;
if (items.length === 0) return;
const item = items.shift();
const sourceItemPath = path.join(sourceDirectory, item);
const destinationItemPath = path.join(destinationDirectory, item);
const creationTime = fs.statSync(sourceItemPath).birthtime.getTime();
console.log(creationTime,"creationTime")
console.log(twentyFourHoursAgo,"twentyFourHoursAgo")
if (fs.statSync(sourceItemPath).isDirectory()) {
const folderNamesSet = new Set(JSON.parse(localStorage.getItem('folderNames')));
if (!folderNamesSet.has(item)) {
if (!item.endsWith('.zip')&& creationTime >= twentyFourHoursAgo) {
const zipFileName = item + '.zip';
const output = fs.createWriteStream(path.join(destinationDirectory, zipFileName));
const archive = archiver('zip', {
zlib: { level: 9 }
});
output.on('close', () => {
totalCopied++;
console.log(`At [${currentTime}] Copied file: ${sourceItemPath} to ${destinationItemPath}`);
processNextItem();
});
archive.pipe(output);
archive.directory(sourceItemPath, false);
archive.finalize();
} else {
processNextItem();
}
} else {
processNextItem();
}
} else {
const fileExtension = path.extname(item).toLowerCase();
if (fileExtensions.includes(fileExtension)) {
const filenameSet = new Set(JSON.parse(localStorage.getItem('filenames')));
if (!filenameSet.has(item)&& creationTime >= twentyFourHoursAgo) {
fs.copyFileSync(sourceItemPath, destinationItemPath);
totalCopied++;
console.log(`At [${currentTime}] Copied folder: ${sourceItemPath} to ${destinationItemPath}`);
} else {
}
} else {
}
processNextItem();
}
}
processNextItem();
} catch (error) {
console.error("Error:", error);
}
},
listFilesAndFolders: async (directoryPath)=> {
try {
const items = await fs.promises.readdir(directoryPath);
const statsPromises = items.map(item => fs.promises.stat(path.join(directoryPath, item)));
const stats = await Promise.all(statsPromises);
return items.map((item, index) => ({
name: item,
isDirectory: stats[index].isDirectory(),
createdTimestamp: stats[index].birthtimeMs
}));
} catch (error) {
console.error('Error listing files and folders:', error);
return [];
}
},
hitApiWithFolderPathAndSubdirectories: async (reqdId) => {
try {
let currentTime = new Date().toLocaleString();
const savedUsername = localStorage.getItem('savedUsername');
const currentWorkingDirectory = process.cwd();
const newDirectoryPath = currentWorkingDirectory + '\\' + 'Dentread' + '\\' + savedUsername + '\\' + reqdId;
const apiUrl = 'https://api.dentread.com/datasync/';
const token = JSON.parse(localStorage.getItem('token'));
const accessToken = token.access;
const username = localStorage.getItem('savedUsername');
const isDirectory = fs.statSync(newDirectoryPath).isDirectory();
let zipFilePath = '';
if (isDirectory) {
zipFilePath = await createZipFromDirectory(newDirectoryPath);
const response = await sendFileToAPI(zipFilePath, apiUrl, accessToken, username);
if (zipFilePath) {
try {
await fs.promises.unlink(zipFilePath);
} catch (err) {
console.error('Error deleting zip file:', err);
}
}
if (response) {
return response;
} else {
return { message: 'API request failed', status: 500 };
}
} else {
const response = await sendFileToAPI(newDirectoryPath, apiUrl, accessToken, username);
if (response) {
return response;
} else {
return { message: 'API request failed', status: 500 };
}
}
} catch (error) {
console.error('API Error:', error);
return { message: 'API request failed', status: 500 };
}
},
settingsbuttonfunc: async () => {
ipcRenderer.invoke('open-settings')
},
logButtonfunc: async () => {
ipcRenderer.invoke('open-logs')
},
minimizeWindow: async () => {
const syncedFoldersJSON = localStorage.getItem('folderNames');
ipcRenderer.send('toggle-auto-sync', true, syncedFoldersJSON);
},
schedulerbuttonfunc: async () => {
const os = require('os');
const hostname = os.hostname();
localStorage.setItem('hostname', hostname);
ipcRenderer.invoke('open-scheduler');
},
manualbuttonfunc: async () => {
ipcRenderer.invoke('open-reload-manual');
},
minimizeWindow2: async () => {
function sendTestNotification() {
const localStorageValue = localStorage.getItem('prefSyncOption');
if (localStorageValue === 'manualSync') {
notifier.notify({
title: 'Dentread IM App Auto Sync Notification',
message: 'This is to notify that auto sync is off',
sound: true,
wait: true,
icon: path.join(__dirname, 'images/LogoDentread.png'),
});
}
}
// Initial call to check and send notification
setTimeout(sendTestNotification, 2 * 60 * 1000);
// Repeat notification every hour
const intervalId = setInterval(sendTestNotification, 60 * 60 * 1000);
// Send message to the main process to toggle auto-sync off
ipcRenderer.send('toggle-auto-sync', false);
},
});