@@ -4,13 +4,9 @@ const zlib = require('zlib');
44const { app } = require ( 'electron' ) ;
55const myAccount = require ( './Account' ) ;
66const { databasePath } = require ( './database/DBEmodel' ) ;
7- const { backupFilenameRegex } = require ( './utils/RegexUtils' ) ;
8- const { createPathRecursive, store } = require ( './utils/FileUtils' ) ;
7+ const { createPathRecursive } = require ( './utils/FileUtils' ) ;
98const {
109 decryptStreamFileWithPassword,
11- encryptStreamFile,
12- exportEncryptDatabaseToFile,
13- generateKeyAndIvFromPassword,
1410 importDatabaseFromFile
1511} = require ( './database/DBEexporter' ) ;
1612
@@ -21,13 +17,6 @@ const STREAM_SIZE = 512 * 1024;
2117const TempBackupFolderName = 'BackupData' ;
2218const TempBackupDirectory = path . join ( databasePath , '..' , TempBackupFolderName ) ;
2319
24- const ExportUnencryptedFilename = path . join (
25- TempBackupDirectory ,
26- 'unencrypt.exp'
27- ) ;
28- const ExportZippedFilename = path . join ( TempBackupDirectory , 'zipped.exp' ) ;
29- const ExportEncryptedFilename = path . join ( TempBackupDirectory , 'encrypted.exp' ) ;
30-
3120const RestoreUnzippedFilename = path . join ( TempBackupDirectory , 'unzipped.res' ) ;
3221const RestoreUnencryptedFilename = path . join (
3322 TempBackupDirectory ,
@@ -61,18 +50,6 @@ const removeTempBackupDirectoryRecursive = pathToDelete => {
6150 }
6251} ;
6352
64- const cleanPreviousBackupFilesInFolder = pathToClean => {
65- try {
66- const files = fs . readdirSync ( pathToClean ) ;
67- const filtered = files . filter ( name => backupFilenameRegex . test ( name ) ) ;
68- filtered . forEach ( filename => {
69- fs . unlinkSync ( path . join ( pathToClean , filename ) ) ;
70- } ) ;
71- } catch ( cleanErr ) {
72- return cleanErr ;
73- }
74- } ;
75-
7653/* Default Directory
7754----------------------------- */
7855function getUsername ( ) {
@@ -96,19 +73,6 @@ const createDefaultBackupFolder = () => {
9673/* Methods
9774----------------------------- */
9875
99- const zipStreamFile = ( { inputFile, outputFile } ) => {
100- return new Promise ( ( resolve , reject ) => {
101- const highWaterMark = STREAM_SIZE ;
102- const reader = fs . createReadStream ( inputFile , { highWaterMark } ) ;
103- const writer = fs . createWriteStream ( outputFile ) ;
104- reader
105- . pipe ( zlib . createGzip ( ) )
106- . pipe ( writer )
107- . on ( 'error' , reject )
108- . on ( 'finish' , resolve ) ;
109- } ) ;
110- } ;
111-
11276const unzipStreamFile = ( { inputFile, outputFile } ) => {
11377 return new Promise ( ( resolve , reject ) => {
11478 const highWaterMark = STREAM_SIZE ;
@@ -131,95 +95,6 @@ const prepareBackupFiles = () => {
13195 }
13296} ;
13397
134- const getFileSizeInBytes = filename => {
135- try {
136- const stats = fs . statSync ( filename ) ;
137- return stats . size ;
138- } catch ( error ) {
139- return 0 ;
140- }
141- } ;
142-
143- /* Export Backup
144- ----------------------------- */
145- const exportBackupUnencrypted = async ( {
146- backupPath,
147- accountObj,
148- progressCallback
149- } ) => {
150- try {
151- try {
152- await exportEncryptDatabaseToFile ( {
153- outputPath : ExportUnencryptedFilename ,
154- accountObj,
155- progressCallback
156- } ) ;
157- } catch ( dbErr ) {
158- throw new Error ( 'Failed to export database' ) ;
159- }
160- // Compress backup file
161- try {
162- await zipStreamFile ( {
163- inputFile : ExportUnencryptedFilename ,
164- outputFile : ExportZippedFilename
165- } ) ;
166- } catch ( zipErr ) {
167- throw new Error ( 'Failed to compress backup file' ) ;
168- }
169- // Move to destination
170- try {
171- cleanPreviousBackupFilesInFolder ( path . join ( backupPath , '..' ) ) ;
172- await store ( backupPath , fs . readFileSync ( ExportZippedFilename ) ) ;
173- } catch ( fileErr ) {
174- throw new Error ( 'Failed to move backup file' ) ;
175- }
176- return getFileSizeInBytes ( backupPath ) ;
177- } catch ( exportBackupError ) {
178- throw exportBackupError ;
179- } finally {
180- removeTempBackupDirectoryRecursive ( TempBackupDirectory ) ;
181- }
182- } ;
183-
184- const exportBackupEncrypted = async ( { backupPath, password, accountObj } ) => {
185- try {
186- // Export database
187- try {
188- await exportEncryptDatabaseToFile ( {
189- outputPath : ExportUnencryptedFilename ,
190- accountObj
191- } ) ;
192- } catch ( dbErr ) {
193- throw new Error ( 'Failed to export database' ) ;
194- }
195- // GZip & Encrypt
196- try {
197- const { key, iv, salt } = generateKeyAndIvFromPassword ( password ) ;
198- await encryptStreamFile ( {
199- inputFile : ExportUnencryptedFilename ,
200- outputFile : ExportEncryptedFilename ,
201- key,
202- iv,
203- salt
204- } ) ;
205- } catch ( encryptErr ) {
206- throw new Error ( 'Failed to encrypt backup' ) ;
207- }
208- // Move to destination
209- try {
210- cleanPreviousBackupFilesInFolder ( path . join ( backupPath , '..' ) ) ;
211- await store ( backupPath , fs . readFileSync ( ExportEncryptedFilename ) ) ;
212- } catch ( fileErr ) {
213- throw new Error ( 'Failed to create backup file' ) ;
214- }
215- return getFileSizeInBytes ( backupPath ) ;
216- } catch ( exportBackupError ) {
217- throw exportBackupError ;
218- } finally {
219- removeTempBackupDirectoryRecursive ( TempBackupDirectory ) ;
220- }
221- } ;
222-
22398/* Restore Backup
22499----------------------------- */
225100const restoreUnencryptedBackup = async ( { filePath } ) => {
@@ -280,8 +155,6 @@ const restoreEncryptedBackup = async ({ filePath, password }) => {
280155module . exports = {
281156 createDefaultBackupFolder,
282157 getDefaultBackupFolder,
283- exportBackupUnencrypted,
284- exportBackupEncrypted,
285158 prepareBackupFiles,
286159 restoreUnencryptedBackup,
287160 restoreEncryptedBackup
0 commit comments