1
1
const extract = require ( 'extract-zip' ) ;
2
+ const _ = require ( 'lodash' ) ;
2
3
const { promisify } = require ( 'util' ) ;
3
4
const fs = require ( 'fs' ) ;
4
5
const ObjectId = require ( 'bson-objectid' ) ;
5
6
const { VAR_FOLDER } = require ( '../config/config' ) ;
6
- const { LOADED_SPACE_CHANNEL } = require ( '../config/channels' ) ;
7
7
const {
8
- ERROR_SPACE_ALREADY_AVAILABLE ,
9
- ERROR_GENERAL ,
10
- ERROR_ZIP_CORRUPTED ,
11
- } = require ( '../config/errors' ) ;
8
+ LOADED_SPACE_CHANNEL ,
9
+ CANCEL_LOAD_SPACE_CHANNEL ,
10
+ EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL ,
11
+ } = require ( '../config/channels' ) ;
12
+ const { ERROR_GENERAL , ERROR_ZIP_CORRUPTED } = require ( '../config/errors' ) ;
12
13
const logger = require ( '../logger' ) ;
13
14
const {
14
15
performFileSystemOperation,
@@ -24,7 +25,10 @@ const {
24
25
// use promisified fs
25
26
const fsPromises = fs . promises ;
26
27
27
- const loadSpace = ( mainWindow , db ) => async ( event , { fileLocation } ) => {
28
+ const extractFileToLoadSpace = ( mainWindow , db ) => async (
29
+ event ,
30
+ { fileLocation }
31
+ ) => {
28
32
const tmpId = ObjectId ( ) . str ;
29
33
30
34
// make temporary folder hidden
@@ -37,7 +41,10 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
37
41
// abort if there is no manifest
38
42
const hasManifest = await isFileAvailable ( manifestPath ) ;
39
43
if ( ! hasManifest ) {
40
- mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_ZIP_CORRUPTED ) ;
44
+ mainWindow . webContents . send (
45
+ EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL ,
46
+ ERROR_ZIP_CORRUPTED
47
+ ) ;
41
48
return clean ( extractPath ) ;
42
49
}
43
50
const manifestString = await fsPromises . readFile ( manifestPath ) ;
@@ -47,62 +54,109 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
47
54
48
55
// get handle to spaces collection
49
56
const spaces = db . get ( SPACES_COLLECTION ) ;
50
- const existingSpace = spaces . find ( { id } ) . value ( ) ;
51
-
52
- // abort if there is already a space with that id
53
- if ( existingSpace ) {
54
- mainWindow . webContents . send (
55
- LOADED_SPACE_CHANNEL ,
56
- ERROR_SPACE_ALREADY_AVAILABLE
57
- ) ;
58
- return clean ( extractPath ) ;
59
- }
60
-
61
- // abort if there is no space
62
- const hasSpace = await isFileAvailable ( spacePath ) ;
63
- if ( ! hasSpace ) {
64
- mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_ZIP_CORRUPTED ) ;
65
- return clean ( extractPath ) ;
66
- }
57
+ const savedSpace = spaces . find ( { id } ) . value ( ) ;
67
58
68
59
const spaceString = await fsPromises . readFile ( spacePath ) ;
69
60
const {
70
- space,
61
+ space = { } ,
71
62
appInstanceResources : resources = [ ] ,
72
63
actions = [ ] ,
73
64
} = JSON . parse ( spaceString ) ;
74
- const finalPath = ` ${ VAR_FOLDER } / ${ id } ` ;
65
+ const elements = { space , resources , actions } ;
75
66
76
- // we need to wrap this operation to avoid errors in windows
77
- performFileSystemOperation ( fs . renameSync ) ( extractPath , finalPath ) ;
67
+ return mainWindow . webContents . send ( EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL , {
68
+ extractPath,
69
+ savedSpace,
70
+ elements,
71
+ } ) ;
72
+ } catch ( err ) {
73
+ logger . error ( err ) ;
74
+ mainWindow . webContents . send (
75
+ EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL ,
76
+ ERROR_GENERAL
77
+ ) ;
78
+ return clean ( extractPath ) ;
79
+ }
80
+ } ;
78
81
79
- const wasRenamed = await isFileAvailable ( finalPath ) ;
82
+ const cancelLoadSpace = mainWindow => async ( event , { extractPath } ) => {
83
+ const isCleanSuccessful = clean ( extractPath ) ;
84
+ mainWindow . webContents . send ( CANCEL_LOAD_SPACE_CHANNEL , isCleanSuccessful ) ;
85
+ return isCleanSuccessful ;
86
+ } ;
80
87
81
- if ( ! wasRenamed ) {
82
- logger . error ( 'unable to rename extract path' ) ;
83
- mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
84
- return clean ( extractPath ) ;
88
+ const loadSpace = ( mainWindow , db ) => async (
89
+ event ,
90
+ {
91
+ extractPath,
92
+ elements : { space, actions, resources } ,
93
+ selection : {
94
+ space : isSpaceSelected ,
95
+ resources : isResourcesSelected ,
96
+ actions : isActionsSelected ,
97
+ } ,
98
+ }
99
+ ) => {
100
+ try {
101
+ // space must be always defined
102
+ if ( _ . isEmpty ( space ) ) {
103
+ logger . debug ( 'try to load undefined space' ) ;
104
+ return mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
85
105
}
86
106
87
- // write space to database
88
- spaces . push ( space ) . write ( ) ;
107
+ // write space to database if selected
108
+ if ( isSpaceSelected ) {
109
+ const { id } = space ;
110
+ const finalPath = `${ VAR_FOLDER } /${ id } ` ;
111
+
112
+ // we need to wrap this operation to avoid errors in windows
113
+ performFileSystemOperation ( fs . renameSync ) ( extractPath , finalPath ) ;
89
114
90
- // write resources to database
91
- db . get ( APP_INSTANCE_RESOURCES_COLLECTION )
92
- . push ( ...resources )
93
- . write ( ) ;
115
+ const wasRenamed = await isFileAvailable ( finalPath ) ;
94
116
95
- // write actions to database
96
- db . get ( ACTIONS_COLLECTION )
97
- . push ( ...actions )
98
- . write ( ) ;
117
+ if ( ! wasRenamed ) {
118
+ logger . error ( 'unable to rename extract path' ) ;
119
+ mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
120
+ return clean ( extractPath ) ;
121
+ }
122
+
123
+ db . get ( SPACES_COLLECTION )
124
+ . push ( space )
125
+ . write ( ) ;
126
+ } else {
127
+ clean ( extractPath ) ;
128
+ }
129
+
130
+ // write resources to database if selected
131
+ if ( isResourcesSelected ) {
132
+ if ( _ . isEmpty ( resources ) ) {
133
+ logger . debug ( 'try to load empty resources' ) ;
134
+ return mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
135
+ }
136
+ db . get ( APP_INSTANCE_RESOURCES_COLLECTION )
137
+ . push ( ...resources )
138
+ . write ( ) ;
139
+ }
140
+
141
+ // write actions to database if selected
142
+ if ( isActionsSelected ) {
143
+ if ( _ . isEmpty ( actions ) ) {
144
+ logger . debug ( 'try to load empty actions' ) ;
145
+ return mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
146
+ }
147
+ db . get ( ACTIONS_COLLECTION )
148
+ . push ( ...actions )
149
+ . write ( ) ;
150
+ }
99
151
100
- return mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , { spaceId : id } ) ;
152
+ return mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , {
153
+ spaceId : space . id ,
154
+ } ) ;
101
155
} catch ( err ) {
102
156
logger . error ( err ) ;
103
157
mainWindow . webContents . send ( LOADED_SPACE_CHANNEL , ERROR_GENERAL ) ;
104
158
return clean ( extractPath ) ;
105
159
}
106
160
} ;
107
161
108
- module . exports = loadSpace ;
162
+ module . exports = { cancelLoadSpace , extractFileToLoadSpace , loadSpace } ;
0 commit comments