-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexport-db.js
More file actions
137 lines (123 loc) · 3.67 KB
/
export-db.js
File metadata and controls
137 lines (123 loc) · 3.67 KB
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
'use strict'
const path = require('path')
const { dialog, BrowserWindow } = require('electron')
const i18next = require('i18next')
const { InvalidFilePathError } = require('./errors')
const { zip } = require('./archiver')
const showErrorModalDialog = require('./show-error-modal-dialog')
const showMessageModalDialog = require('./show-message-modal-dialog')
const {
showLoadingWindow,
hideLoadingWindow,
setLoadingDescription
} = require('./window-creators/change-loading-win-visibility-state')
const wins = require('./window-creators/windows')
const WINDOW_NAMES = require('./window-creators/window.names')
const isMainWinAvailable = require('./helpers/is-main-win-available')
const {
DEFAULT_ARCHIVE_DB_FILE_NAME,
DB_FILE_NAME,
DB_SHM_FILE_NAME,
DB_WAL_FILE_NAME,
SECRET_KEY_FILE_NAME
} = require('./const')
module.exports = ({
pathToUserData,
pathToUserDocuments
}) => {
const _timestamp = (new Date()).toISOString().split('.')[0]
const timestamp = _timestamp.replace(/[:]/g, '-')
const defaultPath = path.join(
pathToUserDocuments,
`${DEFAULT_ARCHIVE_DB_FILE_NAME}-${timestamp}.zip`
)
const dbPath = path.join(pathToUserData, DB_FILE_NAME)
const dbShmPath = path.join(pathToUserData, DB_SHM_FILE_NAME)
const dbWalPath = path.join(pathToUserData, DB_WAL_FILE_NAME)
const secretKeyPath = path.join(pathToUserData, SECRET_KEY_FILE_NAME)
return async () => {
const win = isMainWinAvailable(wins[WINDOW_NAMES.MAIN_WINDOW])
? wins[WINDOW_NAMES.MAIN_WINDOW]
: BrowserWindow.getFocusedWindow()
try {
const {
canceled,
filePath
} = await dialog.showSaveDialog(
win,
{
title: i18next.t('exportDB.saveDialog.title'),
defaultPath,
buttonLabel: i18next
.t('exportDB.saveDialog.buttonLabel'),
filters: [{ name: 'ZIP', extensions: ['zip'] }]
}
)
if (
canceled ||
!filePath
) {
return
}
if (typeof filePath !== 'string') {
throw new InvalidFilePathError()
}
await showLoadingWindow({
windowName: WINDOW_NAMES.LOADING_WINDOW,
description: i18next
.t('exportDB.loadingWindow.description')
})
const progressHandler = async (args) => {
const {
progress,
prettyArchiveSize
} = args ?? {}
const _description = i18next.t('exportDB.loadingWindow.description')
const _archived = i18next.t(
'exportDB.loadingWindow.archiveSize',
{ prettyArchiveSize }
)
const archived = prettyArchiveSize
? `<br><small>${_archived}</small>`
: ''
const description = `${_description}${archived}`
await setLoadingDescription({
windowName: WINDOW_NAMES.LOADING_WINDOW,
progress,
description
})
}
await zip(filePath, [
dbPath,
dbShmPath,
dbWalPath,
secretKeyPath
], { progressHandler })
await hideLoadingWindow({
windowName: WINDOW_NAMES.LOADING_WINDOW
})
await showMessageModalDialog(win, {
buttons: [
i18next.t('common.confirmButtonText')
],
defaultId: 0,
title: i18next.t('exportDB.modalDialog.title'),
message: i18next.t('exportDB.modalDialog.message')
})
} catch (err) {
try {
await hideLoadingWindow({
windowName: WINDOW_NAMES.LOADING_WINDOW
})
await showErrorModalDialog(
win,
i18next.t('exportDB.modalDialog.title'),
err
)
} catch (err) {
console.error(err)
}
console.error(err)
}
}
}