Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.uploadFactory = (req, res) => {
if (!req.body) {
return res.send('No files were uploaded.');
}
const factoryFile = req.body;
const factories = [];
const { file_name, uid, name } = req.params;
const newTimestamp = new Date().getTime();
const fileName = `${file_name}_${newTimestamp}`;
let sourceId;
csv
.fromString(factoryFile.toString(), {
headers: true,
ignoreEmpty: true
})
.on("data", data => {
data['_id'] = new mongoose.Types.ObjectId();
factories.push(data);
})
.on("end", () => {
// Find if there is a source with this name and uid(firebase id), push fileName to file_name[], return source._id (mongo id)
// If not, create a new source
Source.findOneAndUpdate(
{ 'uid': uid, name },
{
$addToSet: { 'file_name': fileName },
updated: new Date()
const { user_name, file_description, user_type } = req.body;
let { file_name } = req.body;
// Remove special charactors other than a-z, 0-9, A-Z
file_name = file_name.replace(/[^a-zA-Z0-9 ]/g, "");
if (!uid || !file_description || !file_name || !user_name) {
return res.send('Missing uid or user name or file name or file description');
}
const factoryFile = JSON.parse(req.body.file);
const factories = [];
const newTimestamp = new Date().getTime();
file_name = `${file_name}_${newTimestamp}`
csv
.fromString(factoryFile.toString(), {
headers: true,
ignoreEmpty: true
})
.on("data", data => {
let temp = {
_id: new mongoose.Types.ObjectId(),
data,
file_name,
uid,
user_name,
file_description,
user_type
};
factories.push(temp);
})
return new Promise((resolve, reject) => {
csv
.fromString(headerLine, {headers: false, delimiter})
.on('data', data => {
resolve(data)
})
})
}
function parseCSVFile(res, responseString, delimiter, callback) {
var data = [];
var options = {
delimiter: delimiter,
headers: true,
discardUnmappedColumns: true,
quote: null,
ignoreEmpty: true,
trim: true
};
csv.fromString(responseString, options)
.on('data', function (value) {
data.push(value);
})
.on('end', function () {
var items = {};
items.data = data;
return callback(null, items);
})
.on('error', function (error) {
debug('error ', error);
return callback(error);
});
}
return new Promise((resolve, reject) => {
let results = [];
csv
.fromString(csvString, {headers: true})
.on("data", parsedObject => {
results.push(parsedObject);
})
.on("end", () => {
resolve(results);
})
.on("error", err => {
reject(err);
})
})
}
imports: {
nl2br: str => {
return str.replace(/\n/gi, '<br>\n');
},
escapehtml: str => {
return str
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
},
escapeurl: encodeURIComponent
}
};
csv
.fromString(fileContent, { headers: true })
.on('data', data => {
var tmpl = data.template;
var dest = data.destination;
delete data.template;
delete data.destination;
gulp
.src(tmpl)
.pipe(plumber())
.pipe(template(data, options))
.pipe(rename(dest))
.pipe(gulp.dest('./'));
})
.on('end', () => {
cb();
});