2
2
3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
- const Readable = require ( 'stream' ) . Readable ;
5
+ const { Readable } = require ( 'stream' ) ;
6
6
7
7
// Parameters for safe file name parsing.
8
8
const SAFE_FILE_NAME_REGEX = / [ ^ \w - ] / g;
@@ -27,17 +27,17 @@ const debugLog = (options, msg) => {
27
27
} ;
28
28
29
29
/**
30
- * Generates unique temporary file name like: tmp-5000-156788789789.
30
+ * Generates unique temporary file name. e.g. tmp-5000-156788789789.
31
31
* @param {string } prefix - a prefix for generated unique file name.
32
32
* @returns {string }
33
33
*/
34
- const getTempFilename = ( prefix ) => {
34
+ const getTempFilename = ( prefix = TEMP_PREFIX ) => {
35
35
tempCounter = tempCounter >= TEMP_COUNTER_MAX ? 1 : tempCounter + 1 ;
36
- return `${ prefix || TEMP_PREFIX } -${ tempCounter } -${ Date . now ( ) } ` ;
36
+ return `${ prefix } -${ tempCounter } -${ Date . now ( ) } ` ;
37
37
} ;
38
38
39
39
/**
40
- * isFunc- check if argument is a function.
40
+ * isFunc: Checks if argument is a function.
41
41
* @returns {boolean } - Returns true if argument is a function.
42
42
*/
43
43
const isFunc = func => func && func . constructor && func . call && func . apply ? true : false ;
@@ -60,7 +60,7 @@ const promiseCallback = (resolve, reject) => {
60
60
* Builds instance options from arguments objects(can't be arrow function).
61
61
* @returns {Object } - result options.
62
62
*/
63
- const buildOptions = function ( ) {
63
+ const buildOptions = function ( ) {
64
64
const result = { } ;
65
65
[ ...arguments ] . forEach ( options => {
66
66
if ( ! options || typeof options !== 'object' ) return ;
@@ -107,17 +107,18 @@ const checkAndMakeDir = (fileUploadOptions, filePath) => {
107
107
// Check whether folder for the file exists.
108
108
if ( ! filePath ) return false ;
109
109
const parentPath = path . dirname ( filePath ) ;
110
- // Create folder if it is not exists .
110
+ // Create folder if it doesn't exist .
111
111
if ( ! fs . existsSync ( parentPath ) ) fs . mkdirSync ( parentPath , { recursive : true } ) ;
112
112
// Checks folder again and return a results.
113
113
return fs . existsSync ( parentPath ) ;
114
114
} ;
115
115
116
116
/**
117
- * Delete file.
117
+ * Deletes a file.
118
118
* @param {string } file - Path to the file to delete.
119
+ * @param {Function } callback
119
120
*/
120
- const deleteFile = ( file , callback ) => fs . unlink ( file , err => err ? callback ( err ) : callback ( ) ) ;
121
+ const deleteFile = ( file , callback ) => fs . unlink ( file , callback ) ;
121
122
122
123
/**
123
124
* Copy file via streams
@@ -147,15 +148,15 @@ const copyFile = (src, dst, callback) => {
147
148
} ;
148
149
149
150
/**
150
- * moveFile - moves the file from src to dst.
151
+ * moveFile: moves the file from src to dst.
151
152
* Firstly trying to rename the file if no luck copying it to dst and then deleteing src.
152
153
* @param {string } src - Path to the source file
153
154
* @param {string } dst - Path to the destination file.
154
155
* @param {Function } callback - A callback function.
155
156
*/
156
- const moveFile = ( src , dst , callback ) => fs . rename ( src , dst , err => ( ! err
157
- ? callback ( )
158
- : copyFile ( src , dst , err => err ? callback ( err ) : deleteFile ( src , callback ) )
157
+ const moveFile = ( src , dst , callback ) => fs . rename ( src , dst , err => ( err
158
+ ? copyFile ( src , dst , err => err ? callback ( err ) : deleteFile ( src , callback ) )
159
+ : callback ( )
159
160
) ) ;
160
161
161
162
/**
@@ -176,7 +177,7 @@ const saveBufferToFile = (buffer, filePath, callback) => {
176
177
} ;
177
178
// Setup file system writable stream.
178
179
let fstream = fs . createWriteStream ( filePath ) ;
179
- fstream . on ( 'error' , error => callback ( error ) ) ;
180
+ fstream . on ( 'error' , err => callback ( err ) ) ;
180
181
fstream . on ( 'close' , ( ) => callback ( ) ) ;
181
182
// Copy file via piping streams.
182
183
readStream . pipe ( fstream ) ;
@@ -252,18 +253,18 @@ const parseFileName = (opts, fileName) => {
252
253
} ;
253
254
254
255
module . exports = {
255
- debugLog,
256
256
isFunc,
257
+ debugLog,
258
+ copyFile, // For testing purpose.
259
+ moveFile,
257
260
errorFunc,
258
- promiseCallback,
259
- buildOptions,
261
+ deleteFile, // For testing purpose.
260
262
buildFields,
261
- checkAndMakeDir,
262
- deleteFile, // For testing purpose.
263
- copyFile, // For testing purpose.
264
- moveFile,
265
- saveBufferToFile,
263
+ buildOptions,
266
264
parseFileName,
267
265
getTempFilename,
266
+ promiseCallback,
267
+ checkAndMakeDir,
268
+ saveBufferToFile,
268
269
uriDecodeFileName
269
270
} ;
0 commit comments