1 | 'use strict';
|
2 |
|
3 | const {
|
4 | isFunc,
|
5 | debugLog,
|
6 | moveFile,
|
7 | promiseCallback,
|
8 | checkAndMakeDir,
|
9 | saveBufferToFile
|
10 | } = require('./utilities');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const moveFromTemp = (filePath, options, fileUploadOptions) => (resolve, reject) => {
|
21 | debugLog(fileUploadOptions, `Moving temporary file ${options.tempFilePath} to ${filePath}`);
|
22 | moveFile(options.tempFilePath, filePath, promiseCallback(resolve, reject));
|
23 | };
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const moveFromBuffer = (filePath, options, fileUploadOptions) => (resolve, reject) => {
|
34 | debugLog(fileUploadOptions, `Moving uploaded buffer to ${filePath}`);
|
35 | saveBufferToFile(options.buffer, filePath, promiseCallback(resolve, reject));
|
36 | };
|
37 |
|
38 | module.exports = (options, fileUploadOptions = {}) => {
|
39 |
|
40 |
|
41 |
|
42 | if (!fileUploadOptions.useTempFiles && !options.buffer.length) return;
|
43 |
|
44 |
|
45 | return {
|
46 | name: options.name,
|
47 | data: options.buffer,
|
48 | size: options.size,
|
49 | encoding: options.encoding,
|
50 | tempFilePath: options.tempFilePath,
|
51 | truncated: options.truncated,
|
52 | mimetype: options.mimetype,
|
53 | md5: options.hash,
|
54 | mv: (filePath, callback) => {
|
55 |
|
56 | const moveFunc = fileUploadOptions.useTempFiles
|
57 | ? moveFromTemp(filePath, options, fileUploadOptions)
|
58 | : moveFromBuffer(filePath, options, fileUploadOptions);
|
59 |
|
60 | checkAndMakeDir(fileUploadOptions, filePath);
|
61 |
|
62 | return isFunc(callback) ? moveFunc(callback) : new Promise(moveFunc);
|
63 | }
|
64 | };
|
65 | };
|