44// Stores files in local file system
55// Requires write access to the server's file system.
66
7- var fs = require ( 'fs' ) ;
8- var path = require ( 'path' ) ;
9- var pathSep = require ( 'path' ) . sep ;
7+ const fs = require ( 'fs' ) ;
8+ const path = require ( 'path' ) ;
9+ const pathSep = require ( 'path' ) . sep ;
1010const crypto = require ( "crypto" ) ;
1111const algorithm = 'aes-256-gcm' ;
1212
1313function FileSystemAdapter ( options ) {
1414 options = options || { } ;
1515 this . _encryptionKey = null ;
1616
17- if ( options . encryptionKey !== undefined ) {
18- this . _encryptionKey = crypto . createHash ( 'sha256' ) . update ( String ( options . encryptionKey ) ) . digest ( 'base64' ) . substr ( 0 , 32 ) ;
17+ if ( options . encryptionKey !== undefined ) {
18+ this . _encryptionKey = crypto . createHash ( 'sha256' ) . update ( String ( options . encryptionKey ) ) . digest ( 'base64' ) . substring ( 0 , 32 ) ;
1919 }
20- let filesSubDirectory = options . filesSubDirectory || '' ;
20+ const filesSubDirectory = options . filesSubDirectory || '' ;
2121 this . _filesDir = filesSubDirectory ;
2222 this . _mkdir ( this . _getApplicationDir ( ) ) ;
2323 if ( ! this . _applicationDirExist ( ) ) {
@@ -26,11 +26,11 @@ function FileSystemAdapter(options) {
2626}
2727
2828FileSystemAdapter . prototype . createFile = function ( filename , data ) {
29- let filepath = this . _getLocalFilePath ( filename ) ;
29+ const filepath = this . _getLocalFilePath ( filename ) ;
3030 const stream = fs . createWriteStream ( filepath ) ;
3131 return new Promise ( ( resolve , reject ) => {
32- try {
33- if ( this . _encryptionKey !== null ) {
32+ try {
33+ if ( this . _encryptionKey !== null ) {
3434 const iv = crypto . randomBytes ( 16 ) ;
3535 const cipher = crypto . createCipheriv (
3636 algorithm ,
@@ -48,21 +48,21 @@ FileSystemAdapter.prototype.createFile = function(filename, data) {
4848 stream . on ( 'finish' , function ( ) {
4949 resolve ( data ) ;
5050 } ) ;
51- } else {
51+ } else {
5252 stream . write ( data ) ;
5353 stream . end ( ) ;
5454 stream . on ( 'finish' , function ( ) {
5555 resolve ( data ) ;
5656 } ) ;
57- }
58- } catch ( err ) {
57+ }
58+ } catch ( err ) {
5959 return reject ( err ) ;
6060 }
6161 } ) ;
6262}
6363
6464FileSystemAdapter . prototype . deleteFile = function ( filename ) {
65- let filepath = this . _getLocalFilePath ( filename ) ;
65+ const filepath = this . _getLocalFilePath ( filename ) ;
6666 const chunks = [ ] ;
6767 const stream = fs . createReadStream ( filepath ) ;
6868 return new Promise ( ( resolve , reject ) => {
@@ -86,7 +86,7 @@ FileSystemAdapter.prototype.deleteFile = function(filename) {
8686}
8787
8888FileSystemAdapter . prototype . getFileData = function ( filename ) {
89- let filepath = this . _getLocalFilePath ( filename ) ;
89+ const filepath = this . _getLocalFilePath ( filename ) ;
9090 const stream = fs . createReadStream ( filepath ) ;
9191 stream . read ( ) ;
9292 return new Promise ( ( resolve , reject ) => {
@@ -96,18 +96,18 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
9696 } ) ;
9797 stream . on ( 'end' , ( ) => {
9898 const data = Buffer . concat ( chunks ) ;
99- if ( this . _encryptionKey !== null ) {
99+ if ( this . _encryptionKey !== null ) {
100100 const authTagLocation = data . length - 16 ;
101101 const ivLocation = data . length - 32 ;
102102 const authTag = data . slice ( authTagLocation ) ;
103103 const iv = data . slice ( ivLocation , authTagLocation ) ;
104104 const encrypted = data . slice ( 0 , ivLocation ) ;
105- try {
105+ try {
106106 const decipher = crypto . createDecipheriv ( algorithm , this . _encryptionKey , iv ) ;
107107 decipher . setAuthTag ( authTag ) ;
108- const decrypted = Buffer . concat ( [ decipher . update ( encrypted ) , decipher . final ( ) ] ) ;
108+ const decrypted = Buffer . concat ( [ decipher . update ( encrypted ) , decipher . final ( ) ] ) ;
109109 return resolve ( decrypted ) ;
110- } catch ( err ) {
110+ } catch ( err ) {
111111 return reject ( err ) ;
112112 }
113113 }
@@ -119,55 +119,36 @@ FileSystemAdapter.prototype.getFileData = function(filename) {
119119 } ) ;
120120}
121121
122- FileSystemAdapter . prototype . rotateEncryptionKey = function ( options = { } ) {
122+ FileSystemAdapter . prototype . rotateEncryptionKey = async function ( options = { } ) {
123123 const applicationDir = this . _getApplicationDir ( ) ;
124- var fileNames = [ ] ;
125- var oldKeyFileAdapter = { } ;
124+ let fileNames = [ ] ;
125+ let oldKeyFileAdapter = { } ;
126126 if ( options . oldKey !== undefined ) {
127- oldKeyFileAdapter = new FileSystemAdapter ( { filesSubDirectory : this . _filesDir , encryptionKey : options . oldKey } ) ;
128- } else {
129- oldKeyFileAdapter = new FileSystemAdapter ( { filesSubDirectory : this . _filesDir } ) ;
127+ oldKeyFileAdapter = new FileSystemAdapter ( { filesSubDirectory : this . _filesDir , encryptionKey : options . oldKey } ) ;
128+ } else {
129+ oldKeyFileAdapter = new FileSystemAdapter ( { filesSubDirectory : this . _filesDir } ) ;
130130 }
131- if ( options . fileNames !== undefined ) {
131+ if ( options . fileNames !== undefined ) {
132132 fileNames = options . fileNames ;
133- } else {
134- fileNames = fs . readdirSync ( applicationDir ) ;
135- fileNames = fileNames . filter ( fileName => fileName . indexOf ( '.' ) !== 0 ) ;
133+ } else {
134+ fileNames = fs . readdirSync ( applicationDir ) ;
135+ fileNames = fileNames . filter ( fileName => fileName . indexOf ( '.' ) !== 0 ) ;
136136 }
137- return new Promise ( ( resolve , _reject ) => {
138- var fileNamesNotRotated = fileNames ;
139- var fileNamesRotated = [ ] ;
140- var fileNameTotal = fileNames . length ;
141- var fileNameIndex = 0 ;
142- fileNames . forEach ( fileName => {
143- oldKeyFileAdapter
144- . getFileData ( fileName )
145- . then ( plainTextData => {
146- //Overwrite file with data encrypted with new key
147- this . createFile ( fileName , plainTextData )
148- . then ( ( ) => {
149- fileNamesRotated . push ( fileName ) ;
150- fileNamesNotRotated = fileNamesNotRotated . filter ( function ( value ) { return value !== fileName ; } )
151- fileNameIndex += 1 ;
152- if ( fileNameIndex == fileNameTotal ) {
153- resolve ( { rotated : fileNamesRotated , notRotated : fileNamesNotRotated } ) ;
154- }
155- } )
156- . catch ( ( ) => {
157- fileNameIndex += 1 ;
158- if ( fileNameIndex == fileNameTotal ) {
159- resolve ( { rotated : fileNamesRotated , notRotated : fileNamesNotRotated } ) ;
160- }
161- } )
162- } )
163- . catch ( ( ) => {
164- fileNameIndex += 1 ;
165- if ( fileNameIndex == fileNameTotal ) {
166- resolve ( { rotated : fileNamesRotated , notRotated : fileNamesNotRotated } ) ;
167- }
168- } ) ;
169- } ) ;
170- } ) ;
137+
138+ let fileNamesNotRotated = fileNames ;
139+ const fileNamesRotated = [ ] ;
140+ for ( const fileName of fileNames ) {
141+ try {
142+ const plainTextData = await oldKeyFileAdapter . getFileData ( fileName )
143+ // Overwrite file with data encrypted with new key
144+ await this . createFile ( fileName , plainTextData )
145+ fileNamesRotated . push ( fileName ) ;
146+ fileNamesNotRotated = fileNamesNotRotated . filter ( function ( value ) { return value !== fileName ; } ) ;
147+ } catch ( err ) {
148+ continue ;
149+ }
150+ }
151+ return { rotated : fileNamesRotated , notRotated : fileNamesNotRotated } ;
171152}
172153
173154FileSystemAdapter . prototype . getFileLocation = function ( config , filename ) {
@@ -177,20 +158,20 @@ FileSystemAdapter.prototype.getFileLocation = function(config, filename) {
177158/*
178159 Helpers
179160 --------------- */
180- FileSystemAdapter . prototype . _getApplicationDir = function ( ) {
161+ FileSystemAdapter . prototype . _getApplicationDir = function ( ) {
181162 if ( this . _filesDir ) {
182163 return path . join ( 'files' , this . _filesDir ) ;
183164 } else {
184165 return 'files' ;
185166 }
186- }
167+ }
187168
188169FileSystemAdapter . prototype . _applicationDirExist = function ( ) {
189170 return fs . existsSync ( this . _getApplicationDir ( ) ) ;
190171}
191172
192173FileSystemAdapter . prototype . _getLocalFilePath = function ( filename ) {
193- let applicationDir = this . _getApplicationDir ( ) ;
174+ const applicationDir = this . _getApplicationDir ( ) ;
194175 if ( ! fs . existsSync ( applicationDir ) ) {
195176 this . _mkdir ( applicationDir ) ;
196177 }
@@ -199,20 +180,19 @@ FileSystemAdapter.prototype._getLocalFilePath = function(filename) {
199180
200181FileSystemAdapter . prototype . _mkdir = function ( dirPath ) {
201182 // snippet found on -> https://gist.github.com/danherbert-epam/3960169
202- let dirs = dirPath . split ( pathSep ) ;
203- var root = "" ;
183+ const dirs = dirPath . split ( pathSep ) ;
184+ let root = "" ;
204185
205186 while ( dirs . length > 0 ) {
206- var dir = dirs . shift ( ) ;
187+ const dir = dirs . shift ( ) ;
207188 if ( dir === "" ) { // If directory starts with a /, the first path will be an empty string.
208189 root = pathSep ;
209190 }
210191 if ( ! fs . existsSync ( path . join ( root , dir ) ) ) {
211192 try {
212193 fs . mkdirSync ( path . join ( root , dir ) ) ;
213- }
214- catch ( e ) {
215- if ( e . code == 'EACCES' ) {
194+ } catch ( err ) {
195+ if ( err . code == 'EACCES' ) {
216196 throw new Error ( "PERMISSION ERROR: In order to use the FileSystemAdapter, write access to the server's file system is required." ) ;
217197 }
218198 }
0 commit comments