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 | const moveFromTemp = (filePath, options, fileUploadOptions) => (resolve, reject) => {
|
20 | debugLog(fileUploadOptions, `Moving temporary file ${options.tempFilePath} to ${filePath}`);
|
21 | moveFile(options.tempFilePath, filePath, promiseCallback(resolve, reject));
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const moveFromBuffer = (filePath, options, fileUploadOptions) => (resolve, reject) => {
|
32 | debugLog(fileUploadOptions, `Moving uploaded buffer to ${filePath}`);
|
33 | saveBufferToFile(options.buffer, filePath, promiseCallback(resolve, reject));
|
34 | };
|
35 |
|
36 | module.exports = (options, fileUploadOptions = {}) => {
|
37 |
|
38 |
|
39 |
|
40 | if (!fileUploadOptions.useTempFiles && !options.buffer.length) return;
|
41 |
|
42 |
|
43 | return {
|
44 | name: options.name,
|
45 | data: options.buffer,
|
46 | size: options.size,
|
47 | encoding: options.encoding,
|
48 | tempFilePath: options.tempFilePath,
|
49 | truncated: options.truncated,
|
50 | mimetype: options.mimetype,
|
51 | md5: options.hash,
|
52 | mv: (filePath, callback) => {
|
53 |
|
54 | const moveFunc = (options.buffer.length && !options.tempFilePath)
|
55 | ? moveFromBuffer(filePath, options)
|
56 | : moveFromTemp(filePath, options);
|
57 |
|
58 | checkAndMakeDir(fileUploadOptions, filePath);
|
59 |
|
60 | return isFunc(callback) ? moveFunc(callback) : new Promise(moveFunc);
|
61 | }
|
62 | };
|
63 | };
|