@@ -10,6 +10,7 @@ export default class Storage {
10
10
11
11
dir = '' ;
12
12
storageDir = dirname ( atom . config . getUserConfigPath ( ) ) ;
13
+ backupdir = path . join ( this . storageDir , 'recovery' , 'snippet-injector' ) ;
13
14
14
15
constructor ( state ) {
15
16
if ( Util . isset ( state , 'object' ) ) {
@@ -69,17 +70,22 @@ export default class Storage {
69
70
console . time ( "storage:store duration" ) ;
70
71
}
71
72
72
- if ( Util . isset ( snippet , Snippet ) ) {
73
- fs . writeFileSync ( path . join ( this . storageDir , this . dir , snippet . getUID ( ) . split ( ' ' ) . join ( '-' ) + '.json' ) , JSON . stringify ( snippet , null , 2 ) ) ;
74
- var temp = fs . existsSync ( path . join ( this . storageDir , this . dir , snippet . getUID ( ) . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
75
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
76
- console . timeEnd ( "storage:store duration" ) ;
77
- }
78
- return temp ;
79
- } else {
80
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
81
- console . timeEnd ( "storage:store duration" ) ;
73
+ try {
74
+ if ( Util . isset ( snippet , Snippet ) ) {
75
+ fs . writeFileSync ( path . join ( this . storageDir , this . dir , snippet . getUID ( ) . split ( ' ' ) . join ( '-' ) + '.json' ) , JSON . stringify ( snippet , null , 2 ) ) ;
76
+ var temp = fs . existsSync ( path . join ( this . storageDir , this . dir , snippet . getUID ( ) . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
77
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
78
+ console . timeEnd ( "storage:store duration" ) ;
79
+ }
80
+ return temp ;
81
+ } else {
82
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
83
+ console . timeEnd ( "storage:store duration" ) ;
84
+ }
85
+ return false ;
82
86
}
87
+ } catch ( e ) {
88
+ atom . notifications . addError ( 'An Error occured while storing data!' , { detail : e . message } ) ;
83
89
return false ;
84
90
}
85
91
}
@@ -88,63 +94,75 @@ export default class Storage {
88
94
if ( this . dir === '' ) {
89
95
throw new TypeError ( 'Storage directory has not been initialized.' ) ;
90
96
} else {
91
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
92
- console . time ( "storage:retrieveFiles duration" ) ;
93
- }
94
- var temp = fs . readdirSync ( path . join ( this . storageDir , this . dir ) ) ;
95
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
96
- console . timeEnd ( "storage:retrieveFiles duration" ) ;
97
+ try {
98
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
99
+ console . time ( "storage:retrieveFiles duration" ) ;
100
+ }
101
+ var temp = fs . readdirSync ( path . join ( this . storageDir , this . dir ) ) ;
102
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
103
+ console . timeEnd ( "storage:retrieveFiles duration" ) ;
104
+ }
105
+ return temp ;
106
+ } catch ( e ) {
107
+ atom . notifications . addError ( 'An Error occured while reading storage!' , { detail : e . message } ) ;
108
+ return false ;
97
109
}
98
- return temp ;
99
110
}
100
111
}
101
112
102
113
retrieveFile ( uid ) {
103
114
if ( this . dir === '' ) {
104
115
throw new TypeError ( 'Storage directory has not been initialized.' ) ;
105
116
} else {
106
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
107
- console . time ( "storage:retrieveFile duration" ) ;
108
- }
109
- var temp = fs . readFileSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
110
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
111
- console . timeEnd ( "storage:retrieveFile duration" ) ;
117
+ try {
118
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
119
+ console . time ( "storage:retrieveFile duration" ) ;
120
+ }
121
+ var temp = fs . readFileSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
122
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
123
+ console . timeEnd ( "storage:retrieveFile duration" ) ;
124
+ }
125
+ return temp ;
126
+ } catch ( e ) {
127
+ atom . notifications . addError ( 'An Error occured while reading data!' , { detail : e . message } ) ;
128
+ return false ;
112
129
}
113
- return temp ;
114
130
}
115
131
}
116
132
117
133
static testFile ( uid ) {
118
- if ( this . dir === '' ) {
119
- throw new TypeError ( 'Storage directory has not been initialized.' ) ;
120
- } else {
121
- try {
122
- var file = this . retrieveFile ( uid ) ;
123
- } catch ( e ) {
124
- return false ;
125
- } finally {
126
- return true ;
127
- }
134
+ var file ;
135
+ try {
136
+ file = this . retrieveFile ( uid ) ;
137
+ } catch ( e ) {
138
+ return false ;
139
+ } finally {
140
+ return ( file !== false ) ;
128
141
}
129
142
}
130
143
131
144
deleteFile ( uid ) {
132
145
if ( this . dir === '' ) {
133
146
throw new TypeError ( 'Storage directory has not been initialized.' ) ;
134
147
} else {
135
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
136
- console . time ( "storage:deleteFile duration" ) ;
137
- }
138
- if ( fs . existsSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ) {
139
- fs . unlinkSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
148
+ try {
140
149
if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
141
- console . timeEnd ( "storage:deleteFile duration" ) ;
150
+ console . time ( "storage:deleteFile duration" ) ;
142
151
}
143
- return true ;
144
- } else {
145
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
146
- console . timeEnd ( "storage:deleteFile duration" ) ;
152
+ if ( fs . existsSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ) {
153
+ fs . unlinkSync ( path . join ( this . storageDir , this . dir , uid . split ( ' ' ) . join ( '-' ) + '.json' ) ) ;
154
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
155
+ console . timeEnd ( "storage:deleteFile duration" ) ;
156
+ }
157
+ return true ;
158
+ } else {
159
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
160
+ console . timeEnd ( "storage:deleteFile duration" ) ;
161
+ }
162
+ return false ;
147
163
}
164
+ } catch ( e ) {
165
+ atom . notifications . addError ( 'An Error occured while deleting data!' , { detail : e . message } ) ;
148
166
return false ;
149
167
}
150
168
}
@@ -154,63 +172,98 @@ export default class Storage {
154
172
if ( this . dir === '' ) {
155
173
throw new TypeError ( 'Storage directory has not been initialized.' ) ;
156
174
} else {
157
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
158
- console . time ( "storage:migrate duration" ) ;
159
- }
175
+ try {
176
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
177
+ console . time ( "storage:migrate duration" ) ;
178
+ }
160
179
161
- var files = this . retrieveFiles ( ) ;
162
- if ( files . length > 0 ) {
163
- var changed = 0 ;
164
- var migrated = 'The following snippets have been migrated:\n' ;
165
- var store = this . storageDir + this . dir ;
166
- var _this = this ;
167
- files . forEach ( function ( current ) {
168
- if ( current . endsWith ( '.snippet.json' ) ) {
169
- fs . renameSync ( path . join ( store , current ) , path . join ( store , current . replace ( '.snippet.json' , '.json' ) ) ) ;
170
- current = current . replace ( '.snippet.json' , '' ) ;
171
- } else {
172
- current = current . replace ( '.json' , '' ) ;
173
- }
174
- _this . backup ( current ) ;
175
-
176
- var snippet = new Snippet ( JSON . parse ( _this . retrieveFile ( current ) ) ) ;
177
- var res = Util . compareVersions ( Snippet . getLastUpdate ( ) , snippet . getVersion ( ) ) ;
178
- if ( res === Snippet . getLastUpdate ( ) || res === undefined ) {
179
- snippet . setVersion ( Util . getPackageVersion ( ) ) ;
180
- snippet . setUID ( Util . generateUID ( {
181
- unique : true ,
182
- tester : _this . testFile ,
183
- timeout : 100 ,
184
- length : 20 ,
185
- prefix : 'sn' ,
186
- insertstring : 'SNIPPET'
187
- } ) ) ;
188
- _this . store ( snippet ) ;
189
- _this . deleteFile ( current ) ;
190
- migrated += '- \'' + snippet . getTitle ( ) + '\'\n' ;
191
- changed ++ ;
180
+ var files = this . retrieveFiles ( ) ;
181
+ if ( files . length > 0 ) {
182
+ var changed = 0 ;
183
+ var migrated = 'The following snippets have been migrated:\n' ;
184
+ var store = this . storageDir + this . dir ;
185
+ var _this = this ;
186
+ files . forEach ( function ( current ) {
187
+ if ( current . endsWith ( '.snippet.json' ) ) {
188
+ fs . renameSync ( path . join ( store , current ) , path . join ( store , current . replace ( '.snippet.json' , '.json' ) ) ) ;
189
+ current = current . replace ( '.snippet.json' , '' ) ;
190
+ } else {
191
+ current = current . replace ( '.json' , '' ) ;
192
+ }
193
+ _this . backup ( current ) ;
194
+
195
+ var snippet = new Snippet ( JSON . parse ( _this . retrieveFile ( current ) ) ) ;
196
+ var res = Util . compareVersions ( Snippet . getLastUpdate ( ) , snippet . getVersion ( ) ) ;
197
+ if ( res === Snippet . getLastUpdate ( ) || res === undefined ) {
198
+ snippet . setVersion ( Util . getPackageVersion ( ) ) ;
199
+ // _this.deleteFile(current);
200
+ _this . store ( snippet ) ;
201
+
202
+ migrated += '- \'' + snippet . getTitle ( ) + '\'\n' ;
203
+ changed ++ ;
204
+ }
205
+ } ) ;
206
+ migrated += '\n\nHappy Coding :)' ;
207
+ if ( changed > 0 ) {
208
+ atom . notifications . addSuccess ( 'Successfully migrated snippets to newer version.' , { detail : migrated } ) ;
192
209
}
193
- } ) ;
194
- migrated += '\n\nHappy Coding :)' ;
195
- if ( changed > 0 ) {
196
- atom . notifications . addSuccess ( 'Successfully migrated snippets to newer version.' , { detail : migrated } ) ;
197
210
}
198
- }
199
211
200
- if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
201
- console . timeEnd ( "storage:migrate duration" ) ;
212
+ if ( window . localStorage . getItem ( 'snippet-injector-debug' ) === 'true' && window . localStorage . getItem ( 'snippet-injector-debug-time' ) === 'true' ) {
213
+ console . timeEnd ( "storage:migrate duration" ) ;
214
+ }
215
+ } catch ( e ) {
216
+ atom . notifications . addFatalError ( 'An Error occured while migrating the local storage! This may impact your stored data.' , { detail : e . message } ) ;
217
+ // atom.notifications.addInfo('Here comes the notification about backups.', null);
218
+ return false ;
202
219
}
203
220
}
204
221
}
205
222
206
223
backup ( uid ) {
207
- var backupdir = path . join ( this . storageDir , 'recovery' , 'snippet-injector' ) ;
208
- if ( ! fs . existsSync ( backupdir ) ) {
209
- fs . mkdirSync ( backupdir ) ;
224
+ try {
225
+ if ( ! fs . existsSync ( backupdir ) ) {
226
+ fs . mkdirSync ( backupdir ) ;
227
+ }
228
+ if ( fs . existsSync ( path . join ( this . storageDir , this . dir , uid + '.json' ) ) ) {
229
+ var snippet = JSON . parse ( this . retrieveFile ( uid ) ) ;
230
+ snippet . backup_timestamp = Date . now ( ) ;
231
+ fs . writeFileSync ( path . join ( this . backupdir , uid + '.json' ) , JSON . stringify ( snippet ) ) ;
232
+ }
233
+ } catch ( e ) {
234
+ atom . notifications . addError ( 'An Error occured during data backup!' , { detail : e . message } ) ;
235
+ return false ;
210
236
}
211
- if ( fs . existsSync ( path . join ( this . storageDir , this . dir , uid + '.json' ) ) ) {
212
- var snippet = JSON . parse ( this . retrieveFile ( uid ) ) ;
213
- fs . writeFileSync ( path . join ( backupdir , uid + '.json' ) , JSON . stringify ( snippet ) ) ;
237
+ }
238
+
239
+ restore ( uid ) {
240
+ try {
241
+ if ( ! fs . existsSync ( backupdir ) ) {
242
+ fs . mkdirSync ( backupdir ) ;
243
+ }
244
+
245
+ if ( fs . existsSync ( path . join ( this . backupdir , uid + '.json' ) ) ) {
246
+ var snippet = JSON . parse ( fs . readFileSync ( path . join ( this . backupdir , uid + '.json' ) ) ) ;
247
+ snippet . backup_timestamp = undefined ;
248
+ this . store ( snippet ) ;
249
+ }
250
+ } catch ( e ) {
251
+ atom . notifications . addError ( 'An Error occured during data restore!' , { detail : e . message } ) ;
252
+ return false ;
253
+ }
254
+ }
255
+
256
+ retrieveBackups ( ) {
257
+ try {
258
+ if ( fs . existsSync ( this . backupdir ) ) {
259
+ var temp = fs . readdirSync ( path . join ( this . storageDir , this . dir ) ) ;
260
+ return temp ;
261
+ } else {
262
+ return false ;
263
+ }
264
+ } catch ( e ) {
265
+ atom . notifications . addError ( 'An Error occured while reading storage!' , { detail : e . message } ) ;
266
+ return false ;
214
267
}
215
268
}
216
269
}
0 commit comments