Skip to content

Instantly share code, notes, and snippets.

@hamzamu
Created June 21, 2020 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamzamu/cb6ef0a113b36875fb3e184bd4cbeec2 to your computer and use it in GitHub Desktop.
Save hamzamu/cb6ef0a113b36875fb3e184bd4cbeec2 to your computer and use it in GitHub Desktop.
Meteor Picker API: Upload files thru REST API.
import {
WebApp
} from 'meteor/webapp'
import bodyParser from 'body-parser'
const fs = require('fs');
const fse = require('fs-extra')
const path = require('path');
const multer = require('multer');
const upload = multer();
Picker.middleware(upload.any());
Picker.route('/api', function (params, req, res, next) {
// files
if (req.files && req.files.length > 0) {
console.log('Headers: ',JSON.stringify(req.headers))
console.log('File: ',req.files[0])
var file = req.files[0];
var outputPath = project.edifact_orders_encryped + file.originalname
fs.writeFileSync(outputPath, file.data, "binary", (err, result) => {
if (err) {
console.log(err);
} else {
console.log('Success: File is written:', file.name)
}
});
res.writeHead(200, {
'Content-Type': 'application/json'
})
var fileData = {
status: true,
message: 'File is uploaded',
header: JSON.stringify(req.headers),
data: {
name: file.originalname,
mimetype: file.mimetype,
size: file.size
}
}
res.end(JSON.stringify(fileData));
}
return
// Getting streamed data...
let body = ''
req.on('data', Meteor.bindEnvironment((data) => {
body += data;
})).on('end', function () {
//
var msg = {}
msg.id = req.headers["message-id"]
msg.to = req.headers["as2-to"]
msg.from = req.headers['as2-from']
let d = new Date()
msg.fileName = msg.id;
var doc = body;
// Writing A Message
if (req.headers && msg.id && msg.to && msg.from && body) {
console.log('Success: File Passed')
res.setHeader('Content-Type', 'application/json');
project.writeOrder(project.edifact_orders_encryped, msg.fileName, doc)
// project.writeOrder(project.edifact_orders, msg.fileName, doc)
var xml = Parse.renderEDI(doc)
// project.rm(project.edifact_orders + msg.fileName)
project.writeOrder(project.opentrans_orders, msg.fileName + ".xml", xml)
// project.writeOrder(project.edifact_orders_done, msg.fileName, doc)
console.log('Success: File is converted')
res.writeHead(200, {
'Content-Type': 'text/html'
})
// res.statusCode = 200
res.end();
} else {
console.log(JSON.stringify(req.headers));
console.log('Error: File is not passed')
res.writeHead(400, {
'Content-Type': 'text/html'
})
// res.statusCode = 400
res.end();
}
})
})
@hamzamu
Copy link
Author

hamzamu commented Jun 21, 2020

var multer = Meteor.npmRequire('multer'),
      upload = multer();

Picker.middleware(upload.any());

Picker.route('/incoming', function(params, req, res, next) {
    if (req.files && req.files.length > 0) {
    fileData = req.files[0].buffer
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment