Using busboy
MODIFY
package.json
package.json
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"body-parser-xml": "^2.0.0",
+ "busboy": "^0.3.1",
"cf-nodejs-logging-support": "^6.5.3",
"cfenv": "^1.2.3",
"cors": "^2.8.5",
"country-language": "^0.1.7",
"ejs": "^3.1.5",
"emailjs": "^3.3.0",
+ "event-stream": "^4.0.1",
"express": "^4.17.1",
"express-sanitizer": "^1.0.5",
"firebase-admin": "^9.4.2",
+ "gridfs-stream": "^1.1.1",
"helmet": "^4.2.0",
"hpp": "^0.2.3",
"http-status-codes": "^2.1.4",
"i18n-iso-countries": "^6.4.0",
"i18n-js": "^3.8.0",
"intl": "^1.2.5",
+ "json-array-streams": "^1.0.1",
"jsonwebtoken": "^8.5.1",
"locale": "^0.1.0",
"lodash": "^4.17.20",
"mongo-uri-builder": "^3.2.2",
"mongodb": "^3.6.3",
"morgan": "^1.10.0",
+ "multer": "^1.4.2",
"mustache": "^4.0.1",
"node-cron": "^2.0.3",
"object-sizeof": "^1.6.1",

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -9,6 +9,7 @@ import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
import Authorizations from '../../../../authorization/Authorizations';
import BillingFactory from '../../../../integration/billing/BillingFactory';
+import Busboy from 'busboy';
import { Car } from '../../../../types/Car';
import CarStorage from '../../../../storage/mongodb/CarStorage';
import ConnectionStorage from '../../../../storage/mongodb/ConnectionStorage';
@@ -38,7 +39,11 @@ import UserStorage from '../../../../storage/mongodb/UserStorage';
import Utils from '../../../../utils/Utils';
import UtilsService from './UtilsService';
import fs from 'fs';
+import { inspect } from 'util';
import moment from 'moment';
+import os from 'os';
+import path from 'path';
+import through from 'through';
const MODULE_NAME = 'UserService';
@@ -107,15 +112,74 @@ export default class UserService {
public static async import(action: ServerAction, req: Request, res: Response, next: NextFunction) {
- const stream = Readable.from(req); // Rows, ANYTHING, doc
+ const jStream = JSONStream.parse('users.*');
- stream.on('data', function(data) {
- console.log(
- data.toString());
- // UserStorage.saveImportedUser(req.user.tenantID, JSON.parse(body.toString().substring(body.indexOf('['), body.indexOf(']') + 1)));
- });
+ // Const upload = multer({ dest: 'uploads/' });
+
+ const busboy = new Busboy({ headers: req.headers });
+
+ // Stream.on('file', function(data) {
+ // console.log('received:', data);
+ // });
res.send('success');
+
+ jStream.on('data', async function(data) {
+ // Await UserStorage.saveImportedUser(req.user.tenantID, data);
+ return data.toString();
+ });
+
+
+ busboy.on('file', async function(fieldname, file, filename, encoding, mimetype) {
+ file.pipe(jStream);
+ });
+ busboy.on('finish', function() {
+ res.end('That\'s all folks!');
+ });
+
+ busboy.pipe(process.stdout);
+
+ // Return req.pipe(busboy);
+
+ // JStream.on(function(data) {
+ // console.log('received:', data);
+ // });
+
+ // busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
+ // console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
+ // file.on('data', function(data) {
+ // console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
+ // console.log(data);
+ // });
+ // file.on('end', function() {
+ // console.log('File [' + fieldname + '] Finished');
+ // });
+ // });
+ // busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
+ // console.log('Field [' + fieldname + ']: value: ' + val);
+ // });
+ // busboy.on('finish', function() {
+ // console.log('Done parsing form!');
+ // res.end();
+ // });
+ req.pipe(busboy);
+
+
+ // Upload(req, res, function(err) {
+ // if (err) {
+ // res.redirect(req.headers.referer + '/error.html');
+ // return;
+ // }
+
+ // if (!req) {
+ // res.redirect(req.headers.referer + '/error.html');
+
+ // } else {
+ // // Implement your own logic if needed. Like moving the file, renaming the file, etc.
+ // res.redirect(req.headers.referer);
+ // }
+ // }).single('test');
+
}
public static async handleAssignSitesToUser(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -270,10 +270,10 @@ export default class UserStorage {
return userMDB._id.toHexString();
}
- public static saveImportedUser(tenantID: string, usersToSave): void {
- void global.database.getCollection<any>(tenantID, 'userImport2').insertMany(
+ public static async saveImportedUser(tenantID: string, usersToSave): Promise<void> {
+ await global.database.getCollection<any>(tenantID, 'userImport3').insertOne(
usersToSave);
- console.log('saved:', usersToSave);
+ // Console.log('saved:', usersToSave);
}
public static async saveUserPassword(tenantID: string, userID: string,

Using busboy