@@ -10,6 +10,7 @@ export default class Storage {
1010
1111 dir = '' ;
1212 storageDir = dirname ( atom . config . getUserConfigPath ( ) ) ;
13+ backupdir = path . join ( this . storageDir , 'recovery' , 'snippet-injector' ) ;
1314
1415 constructor ( state ) {
1516 if ( Util . isset ( state , 'object' ) ) {
@@ -69,17 +70,22 @@ export default class Storage {
6970 console . time ( "storage:store duration" ) ;
7071 }
7172
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 ;
8286 }
87+ } catch ( e ) {
88+ atom . notifications . addError ( 'An Error occured while storing data!' , { detail : e . message } ) ;
8389 return false ;
8490 }
8591 }
@@ -88,63 +94,75 @@ export default class Storage {
8894 if ( this . dir === '' ) {
8995 throw new TypeError ( 'Storage directory has not been initialized.' ) ;
9096 } 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 ;
97109 }
98- return temp ;
99110 }
100111 }
101112
102113 retrieveFile ( uid ) {
103114 if ( this . dir === '' ) {
104115 throw new TypeError ( 'Storage directory has not been initialized.' ) ;
105116 } 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 ;
112129 }
113- return temp ;
114130 }
115131 }
116132
117133 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 ) ;
128141 }
129142 }
130143
131144 deleteFile ( uid ) {
132145 if ( this . dir === '' ) {
133146 throw new TypeError ( 'Storage directory has not been initialized.' ) ;
134147 } 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 {
140149 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" ) ;
142151 }
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 ;
147163 }
164+ } catch ( e ) {
165+ atom . notifications . addError ( 'An Error occured while deleting data!' , { detail : e . message } ) ;
148166 return false ;
149167 }
150168 }
@@ -154,63 +172,98 @@ export default class Storage {
154172 if ( this . dir === '' ) {
155173 throw new TypeError ( 'Storage directory has not been initialized.' ) ;
156174 } 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+ }
160179
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 } ) ;
192209 }
193- } ) ;
194- migrated += '\n\nHappy Coding :)' ;
195- if ( changed > 0 ) {
196- atom . notifications . addSuccess ( 'Successfully migrated snippets to newer version.' , { detail : migrated } ) ;
197210 }
198- }
199211
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 ;
202219 }
203220 }
204221 }
205222
206223 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 ;
210236 }
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 ;
214267 }
215268 }
216269}
0 commit comments