-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
165 lines (148 loc) · 4.82 KB
/
importer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const formidable = require('formidable');
const api = require('./api'),
file = require('./file'),
sockets = require('./sockets'),
dev = require('./dev-log');
module.exports = (function() {
const API = {
handleForm: ({ req, res, type, slugFolderName }) => {
return new Promise(function(resolve, reject) {
dev.logfunction(
`IMPORTER — handleForm : type = ${type}, slugFolderName = ${slugFolderName}`
);
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
form.maxFileSize = 1000 * 1024 * 1024;
let socketid = '';
// store all uploads in the folder directory
form.uploadDir = api.getFolderPath(slugFolderName);
let allFilesMeta = [];
let fieldValues = {};
form.on('field', function(name, value) {
console.log(`Got field with name = ${name} and value = ${value}.`);
if (name === 'socketid') {
socketid = value;
}
try {
fieldValues[name] = JSON.parse(value);
} catch (e) {
// didn’t get an object as additional meta
}
});
// every time a file has been uploaded successfully,
form.on('file', function(field, uploadedFile) {
dev.logverbose(
`File uploaded:\nfield: ${field}\nfile: ${JSON.stringify(
uploadedFile,
null,
4
)}.`
);
// add addiontal meta from 'field' to the array
let newFile = uploadedFile;
for (let fileName in fieldValues) {
if (fileName === newFile.name) {
newFile = Object.assign({}, newFile, {
additionalMeta: fieldValues[fileName]
});
}
}
// dev.logverbose(`Found matching filenames, new meta file is: ${JSON.stringify(newFile,null,4)}`);
allFilesMeta.push(newFile);
});
// log any errors that occur
form.on('error', function(err) {
console.log(`An error has happened: ${err}`);
});
form.on('aborted', function(err) {
console.log(`File upload aborted: ${err}`);
});
// once all the files have been uploaded
form.on('end', function() {
let msg = {};
msg.msg = 'success';
// msg.medias = JSON.stringify(allFilesMeta);
res.end(JSON.stringify(msg));
if (allFilesMeta.length > 0) {
var m = [];
for (var i in allFilesMeta) {
m.push(
renameAndConvertMediaAndCreateMeta({
uploadDir: form.uploadDir,
slugFolderName,
fileMeta: allFilesMeta[i],
socketid,
type
})
);
}
Promise.all(m).then(() => {
resolve();
});
}
});
// parse the incoming request containing the form data
form.parse(req);
});
}
};
function renameAndConvertMediaAndCreateMeta({
uploadDir,
slugFolderName,
fileMeta,
socketid,
type
}) {
return new Promise(function(resolve, reject) {
dev.logfunction('ROUTER — renameAndConvertMediaAndCreateMeta');
api.findFirstFilenameNotTaken(uploadDir, fileMeta.name).then(
function(newFileName) {
dev.logverbose(`Following filename is available: ${newFileName}`);
if (fileMeta.hasOwnProperty('additionalMeta')) {
dev.logverbose(
`Has additional meta: ${JSON.stringify(
fileMeta.additionalMeta,
null,
4
)}`
);
} else {
fileMeta.additionalMeta = {};
}
file
.convertAndSaveMedia({
uploadDir,
tempPath: fileMeta.path,
newFileName,
socketid
})
.then(newFileName => {
fileMeta.additionalMeta.media_filename = newFileName;
sockets.createMediaMeta({
type,
slugFolderName,
additionalMeta: fileMeta.additionalMeta
});
resolve();
})
.catch(err => {
dev.error(err);
fileMeta.additionalMeta.media_filename = newFileName;
sockets.createMediaMeta({
type,
slugFolderName,
additionalMeta: fileMeta.additionalMeta
});
resolve();
});
},
function(err) {
reject(err);
}
);
});
}
return API;
})();