|
| 1 | +const ipc = require('@criptext/electron-better-ipc'); |
| 2 | +const { shell } = require('electron'); |
| 3 | +const moment = require('moment'); |
| 4 | +const { |
| 5 | + createDefaultBackupFolder, |
| 6 | + getDefaultBackupFolder, |
| 7 | + prepareBackupFiles, |
| 8 | + exportBackupUnencrypted, |
| 9 | + exportBackupEncrypted, |
| 10 | + restoreUnencryptedBackup, |
| 11 | + restoreEncryptedBackup |
| 12 | +} = require('./../BackupManager'); |
| 13 | +const globalManager = require('./../globalManager'); |
| 14 | +const { showNotification } = require('./../notificationManager'); |
| 15 | +const { sendEventToAllWindows } = require('./../windows/windowUtils'); |
| 16 | +const { |
| 17 | + defineBackupFileName, |
| 18 | + defineUnitToAppend, |
| 19 | + backupDateFormat |
| 20 | +} = require('./../utils/TimeUtils'); |
| 21 | +const { getSettings, updateSettings } = require('./../DBManager'); |
| 22 | +global.autoBackupIntervalId = null; |
| 23 | + |
| 24 | +const simulatePause = ms => { |
| 25 | + return new Promise(resolve => { |
| 26 | + setTimeout(resolve, ms); |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +const commitBackupStatus = (eventName, status, params) => { |
| 31 | + sendEventToAllWindows(eventName, params); |
| 32 | + if (status) globalManager.backupStatus = status; |
| 33 | +}; |
| 34 | + |
| 35 | +ipc.answerRenderer('create-default-backup-folder', () => |
| 36 | + createDefaultBackupFolder() |
| 37 | +); |
| 38 | + |
| 39 | +const doExportBackupUnencrypted = async params => { |
| 40 | + const { backupPath, notificationParams } = params; |
| 41 | + try { |
| 42 | + globalManager.windowsEvents.disable(); |
| 43 | + commitBackupStatus('local-backup-disable-events', 1); |
| 44 | + await prepareBackupFiles({}); |
| 45 | + await simulatePause(2000); |
| 46 | + globalManager.windowsEvents.enable(); |
| 47 | + commitBackupStatus('local-backup-enable-events', 2); |
| 48 | + const backupSize = await exportBackupUnencrypted({ backupPath }); |
| 49 | + commitBackupStatus('local-backup-export-finished', 3); |
| 50 | + await simulatePause(2000); |
| 51 | + commitBackupStatus('local-backup-success', null, backupSize); |
| 52 | + await simulatePause(2000); |
| 53 | + if (notificationParams) { |
| 54 | + showNotification({ |
| 55 | + title: notificationParams.success.title, |
| 56 | + message: notificationParams.success.message, |
| 57 | + clickHandler: function() { |
| 58 | + shell.showItemInFolder(backupPath); |
| 59 | + }, |
| 60 | + forceToShow: true |
| 61 | + }); |
| 62 | + } |
| 63 | + return backupSize; |
| 64 | + } catch (error) { |
| 65 | + globalManager.windowsEvents.enable(); |
| 66 | + commitBackupStatus('local-backup-enable-events', null, { error }); |
| 67 | + if (notificationParams) { |
| 68 | + showNotification({ |
| 69 | + title: notificationParams.error.title, |
| 70 | + message: notificationParams.error.message, |
| 71 | + forceToShow: true |
| 72 | + }); |
| 73 | + } |
| 74 | + return 0; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +ipc.answerRenderer('export-backup-unencrypted', doExportBackupUnencrypted); |
| 79 | + |
| 80 | +ipc.answerRenderer('export-backup-encrypted', async params => { |
| 81 | + const { backupPath, password, notificationParams } = params; |
| 82 | + try { |
| 83 | + globalManager.windowsEvents.disable(); |
| 84 | + commitBackupStatus('local-backup-disable-events', 1); |
| 85 | + await prepareBackupFiles({}); |
| 86 | + await simulatePause(2000); |
| 87 | + globalManager.windowsEvents.enable(); |
| 88 | + commitBackupStatus('local-backup-enable-events', 2); |
| 89 | + const backupSize = await exportBackupEncrypted({ |
| 90 | + backupPath, |
| 91 | + password |
| 92 | + }); |
| 93 | + commitBackupStatus('local-backup-export-finished', 3); |
| 94 | + await simulatePause(2000); |
| 95 | + commitBackupStatus('local-backup-success', null, backupSize); |
| 96 | + await simulatePause(2000); |
| 97 | + if (notificationParams) { |
| 98 | + showNotification({ |
| 99 | + title: notificationParams.success.title, |
| 100 | + message: notificationParams.success.message, |
| 101 | + clickHandler: function() { |
| 102 | + shell.showItemInFolder(backupPath); |
| 103 | + }, |
| 104 | + forceToShow: true |
| 105 | + }); |
| 106 | + } |
| 107 | + return backupSize; |
| 108 | + } catch (error) { |
| 109 | + globalManager.windowsEvents.enable(); |
| 110 | + commitBackupStatus('local-backup-enable-events', null, { error }); |
| 111 | + if (notificationParams) { |
| 112 | + showNotification({ |
| 113 | + title: notificationParams.error.title, |
| 114 | + message: notificationParams.error.message, |
| 115 | + forceToShow: true |
| 116 | + }); |
| 117 | + } |
| 118 | + return 0; |
| 119 | + } |
| 120 | +}); |
| 121 | + |
| 122 | +ipc.answerRenderer('get-default-backup-folder', () => getDefaultBackupFolder()); |
| 123 | + |
| 124 | +ipc.answerRenderer('restore-backup-unencrypted', async ({ backupPath }) => { |
| 125 | + try { |
| 126 | + globalManager.windowsEvents.disable(); |
| 127 | + commitBackupStatus('restore-backup-disable-events'); |
| 128 | + await prepareBackupFiles({ backupPrevFiles: false }); |
| 129 | + await simulatePause(2000); |
| 130 | + globalManager.windowsEvents.enable(); |
| 131 | + commitBackupStatus('restore-backup-enable-events'); |
| 132 | + await restoreUnencryptedBackup({ filePath: backupPath }); |
| 133 | + commitBackupStatus('restore-backup-finished'); |
| 134 | + await simulatePause(2000); |
| 135 | + commitBackupStatus('restore-backup-success', null); |
| 136 | + } catch (error) { |
| 137 | + globalManager.windowsEvents.enable(); |
| 138 | + commitBackupStatus('restore-backup-enable-events', null, { |
| 139 | + error: error.message |
| 140 | + }); |
| 141 | + } |
| 142 | +}); |
| 143 | + |
| 144 | +ipc.answerRenderer('restore-backup-encrypted', async params => { |
| 145 | + const { backupPath, password } = params; |
| 146 | + try { |
| 147 | + globalManager.windowsEvents.disable(); |
| 148 | + commitBackupStatus('restore-backup-disable-events'); |
| 149 | + await prepareBackupFiles({ backupPrevFiles: false }); |
| 150 | + await simulatePause(2000); |
| 151 | + globalManager.windowsEvents.enable(); |
| 152 | + commitBackupStatus('restore-backup-enable-events'); |
| 153 | + await restoreEncryptedBackup({ filePath: backupPath, password }); |
| 154 | + commitBackupStatus('restore-backup-finished'); |
| 155 | + await simulatePause(2000); |
| 156 | + commitBackupStatus('restore-backup-success', null); |
| 157 | + } catch (error) { |
| 158 | + globalManager.windowsEvents.enable(); |
| 159 | + commitBackupStatus('restore-backup-enable-events', null, { |
| 160 | + error: error.message |
| 161 | + }); |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +const initAutoBackupMonitor = async () => { |
| 166 | + clearTimeout(global.autoBackupIntervalId); |
| 167 | + const { |
| 168 | + autoBackupPath, |
| 169 | + autoBackupFrequency, |
| 170 | + autoBackupNextDate |
| 171 | + } = await getSettings(); |
| 172 | + if (!autoBackupNextDate) { |
| 173 | + log('Failed to get next date'); |
| 174 | + return; |
| 175 | + } |
| 176 | + const now = moment(); |
| 177 | + const pendingDate = moment(autoBackupNextDate); |
| 178 | + const timeDiff = pendingDate.diff(now); |
| 179 | + |
| 180 | + global.autoBackupIntervalId = setTimeout(async () => { |
| 181 | + try { |
| 182 | + const backupFileName = defineBackupFileName('db'); |
| 183 | + const backupSize = await doExportBackupUnencrypted({ |
| 184 | + backupPath: `${autoBackupPath}/${backupFileName}` |
| 185 | + }); |
| 186 | + const timeUnit = defineUnitToAppend(autoBackupFrequency); |
| 187 | + const today = moment(Date.now()); |
| 188 | + const nextDate = moment(autoBackupNextDate); |
| 189 | + do { |
| 190 | + nextDate.add(1, timeUnit); |
| 191 | + } while (nextDate.isBefore(today)); |
| 192 | + await updateSettings({ |
| 193 | + autoBackupLastDate: pendingDate.format(backupDateFormat), |
| 194 | + autoBackupLastSize: backupSize, |
| 195 | + autoBackupNextDate: nextDate.format(backupDateFormat) |
| 196 | + }); |
| 197 | + initAutoBackupMonitor(); |
| 198 | + } catch (backupErr) { |
| 199 | + log( |
| 200 | + `Failed to do scheduled backup at ${pendingDate.format( |
| 201 | + backupDateFormat |
| 202 | + )}` |
| 203 | + ); |
| 204 | + } |
| 205 | + }, timeDiff); |
| 206 | +}; |
| 207 | + |
| 208 | +ipc.answerRenderer('init-autobackup-monitor', initAutoBackupMonitor); |
| 209 | + |
| 210 | +const log = message => { |
| 211 | + if (process.env.NODE_ENV === 'development') { |
| 212 | + console.log(`[AutoBackup]: ${message}`); |
| 213 | + } |
| 214 | +}; |
| 215 | + |
| 216 | +process.on('exit', () => { |
| 217 | + global.autoBackupIntervalId = null; |
| 218 | +}); |
| 219 | + |
| 220 | +module.exports = { |
| 221 | + doExportBackupUnencrypted |
| 222 | +}; |
0 commit comments