uploaded files displaying base64 data in the browser - node.js

I'm using couchdb to store attachments that I need to display in the browser.
The data is uploaded from an html input and then processed when saveDoc is called:
getFileData: function(file){
var reader = new FileReader();
return new Promise(function(accept, reject){
reader.onload = (e) => {
accept(e.target.result)
};
reader.readAsDataURL(file);
})
},
saveDoc: function(name, type, filedata, url){
console.log(filedata)
var self=this
return new Promise(function(accept, reject){
self.getData(url).then(data => {
var rev = data['_rev']
console.log(url + ' is the url')
console.log(name + ' is the filename')
documentation.attachment.insert(url, name, filedata, type,
{ rev: rev }, function(err, body) {
if (!err){
console.log(body);
}
else {
console.log(err)
}
})
}).catch(err => {
console.log(err)
})
})
},
I don't get any errors while uploading from the console. But when I navigate to where the attachment should be in the console, I see a browser message telling me the data can't be displayed (for pdf/images), or I see a base64 string that looks like this:
data:image/png;base64,iVBOR...
when the attachment is an html document.
(The data being logged on saveDoc looks like this:
data:application/pdf;base64,JVBER...)
The correct content type as well as a reasonable length is being displayed in my couchdb admin with metadata on the files, so there are no obvious header problems. Can anyone think of any other reason this might not be working in the browser?
Edit
To give some more detail, I uploaded a pdf in Fauxton, which works as expected and displays in teh browser. I then uploaded the same pdf using my saveDoc function, and it somehow added a huge amount of data to the length of the document.
version uploaded in Fauxton:
"_attachments": {
"03_IKB-RH_FUB_mitDB.pdf": {
"content_type": "application/pdf",
"revpos": 5,
"digest": "md5-tX7qKPT6b7Ek90GbIq9q8A==",
"length": 462154,
"stub": true
}
}
version uploaded programmatically:
"_attachments": {
"03_IKB-RH_FUB_mitDB.pdf": {
"content_type": "application/pdf",
"revpos": 4,
"digest": "md5-Zy8zcwHmXsfwtleJNV5xHw==",
"length": 616208,
"stub": true
}
}

The data property of a particular ._attachments{} member should be base64, not dataURL.
When you convert your file to dataURL, you do get base64-encoded binary, but with special prefix. Truncate the prefix, and save only base64-encoded tail.
Basically, to obtain valid base64 you should remove data:*/*;base64, prefix from the beginning of a dataURL-encoded string.
UPD. After diving deeper it turned out, that .attachment.insert(url, name, filedata, type) method accepts binary (not base64) as filedata. To obtain binary from FileReader, fr.readAsArrayBuffer() should be used instead of fr.readAsDataURL().

Related

How to delete the first object from a JSON file

I'm trying to make a queue in node js and want to delete the first JSON object in my JSON file.
I create and store users in a JSON file and then add them in the queue JSON file by reading the users.json. The problem is that when i get the user from user.json it comes as an object within an array, and i cant filter to compare the id. So how can i do this?
// helper method to read JSON FILE.
const readFile = (callback, returnJson = false, filePath = dataPath, encoding = 'utf8') => {
fs.readFile(filePath, encoding, (err, data) => {
if (err) {
throw err;
}
callback(returnJson ? JSON.parse(data) : data);
});
};
// helper method to write on JSON FILE.
const writeFile = (fileData, callback, filePath = dataPath, encoding = 'utf8') => {
fs.writeFile(filePath, fileData, encoding, (err) => {
if (err) {
throw err;
}
callback();
});
};
// thats how i try to delete first user from the queue, but it deletes the user with index 1.
app.delete('/popLine', (req, res) => {
readFile(data => {
//const userId = req.params["id"];
delete data[1]; // remove the first element from the Line.
writeFile(JSON.stringify(data, null, 2), () => {
res.status(200).send(`queue id: removed`);
});
},
true);
});
// thats how an user is stored in queue.json
"5": [
{
"name": "teresa may",
"email": "parliament",
"gender": "male",
"id": 3
}
],
I would prefer loading the JSON file as a JSON object (if its manageable) then delete the first entry by key and then persisting the file back on the disk (over-write) periodically or instantaneously which be necessary.
You can do a require to load a JSON file as a JSON object. But note, if you do a re-require of the changed file to reload it again during runtime, it will not get you the changes you made to the file in between but the older content from last require. In that case you need to clear the item from the require cache before getting the changes.
I am assuming the file is in order of KBs.. for larger files I wouldn’t prefer this approach, rather I would figure a way out to get that file data in a NoSQL document database.
Hope this helps :)

How convert word document to pdf in Nodejs

I have method on Node js Return Word document , i want pass parameter called file type to convert this word document
to PDF.
the output from buf var it file like below
PK
! ߤ�l [Content_Types].xml<?xml version="1.0" encoding="UTF-8" standalone="yes"?>...
How can convert this word document to pdf ?
router.get("/hello", function(request, response, next,fileType) {
var zip = new JSZip(
"UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRf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AW239kwkBAADxAQAAFAAAAAAAAAAAAAAAAABsGAAAd29yZC93ZWJTZXR0aW5ncy54bWxQSwECLQAUAAYACAAAACEAAR0f33EBAADHAgAAEAAAAAAAAAAAAAAAAACnGQAAZG9jUHJvcHMvYXBwLnhtbFBLAQItABQABgAIAAAAIQDWVAxaegEAAAMDAAARAAAAAAAAAAAAAAAAAE4cAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQALRmoQGwsAAARwAAAPAAAAAAAAAAAAAAAAAP8eAAB3b3JkL3N0eWxlcy54bWxQSwUGAAAAAAsACwDBAgAARyoAAAAA",
{ base64: true }
);
var doc = new Docxtemplater();
doc.loadZip(zip);
//set the templateVariables
doc.setData({});
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
var buf = doc.getZip().generate({ type: "nodebuffer" });
if(fileType=='docx'){
response.writeHead(200, {
"Content-Disposition": "attachment;filename=" + "template.docx",
"Content-Type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
}else if(fileType=='pdf'){
//here should convert
}
response.end(buf);
} catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
console.log(JSON.stringify({ error: e }));
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
throw error;
}
});
You could use a package for this. Although the package I found, require the file to be read from disk it seems and not from buffer.
docx-pdf: npm i docx-pdf
const docxConverter = require('docx-pdf');
docxConverter('./input.docx','./output.pdf', (err, result) => {
if (err) console.log(err);
else console.log(result); // writes to file for us
});
Another option is to use an external API like https://github.com/Api2Pdf/api2pdf.node#readme (you could maybe even host it yourself, if you're handling sensitive data).
Its pretty simple if you use libreoffice-convert library.
Code :
const libre = require('libreoffice-convert');
const fs = require('fs');
function convertToPdf(filePath) {
const file = fs.readFileSync(filePath);
libre.convert(file, extend, undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
}
fs.writeFileSync('outputFile.pdf', done);
});
}
You can find more documentation here.

NodeJS Multiparty file upload to Azure and a text field

I need to verify that a mongodb document exists before continuing with Azure file upload.
The form consists of a file and a text field, the logic that is needed is the following:
Form submission
Get the text field
Search in mongodb for a document with the text field data
If the item exist continue with file upload to Azure else return
File upload to Azure
Save the URL to the file in the same MongoDB document that was found in
The problem I'm facing is that I can't touch field data in form.on('part') and can't get it to work with using form.parse first.
This is my code, I'm willing to change libraries and do whatever it takes to get it working.
var form = new multiparty.Form();
var formField = "";
form.parse(req, function(err, fields, files) {
formField = fields.fieldinform[0];
});
console.log(formField); //empty async?
model
.findOne({ name: formField })
.then(obj => {
form.on("part", function(part) {
if (!part.filename) return;
var size = part.byteCount;
var name = part.filename;
var container = "test";
blobService.createBlockBlobFromStream(
container,
name,
part,
size,
function(error) {
if (error) {
console.log("failed");
}
}
);
});
})
.catch(e => {
//do not continue
});
Help would be highly appreciated!
After a lot of searching and not coming up with a proper answer I decided to go with jquery that changed the action URL of the form to /upload/textintextfield before submission and then grab that with req.params.textfield in node.
<script>
$('#fileUploadForm').submit(function() {
$('#fileUploadForm').attr('action', '/upload/addvideo/' + $('#textfield').val())
return true;
});
</script>

Updating a txt file in AWS S3 - Converting Uint8Array to String

I am trying to create a feedback button for my app. It basically just writes to a text file but it currently replaces the existing text file with the new feedback string. So basically I need to get the current text file and concatenate the text inside that text file with the new feedback so it doesn't overwrite anything.
Problem is whenever I get this text file it gives me what appears to be a Uint8Arrayobject:
The last line of these logs is the String to be added to the text file. Right above that is the object which was returned from the call to aws. As you can see it is some object and not a string. So in my .txt file it is uploading a [object Object]. The second log leads me to believe that the object is a Uint8Array because of the Body.
My code looks something like this:
function uploadLog(message, key) {
var moment = {key: key};
var deferred = $q.defer();
awsServices.getObject(key).then(function(data) {
console.log("NEW MESSAGE");
console.log(data);
data = new TextDecoder("utf-8").decode(data.Body);
// data = Utf8ArrayToStr(data.Body);
console.log(data);
console.log(message);
newMessage = message.toString() + '\r\n\r\n' + data;
var blob = new Blob([newMessage.toString()], {type: "text"});
var file = new File([blob], key);
awsServices.upload(file, moment.key, moment).then(function() {
deferred.resolve();
}, function(error) {
console.log("UPLOAD LOG REJECT");
deferred.reject();
});
}, function(error) {
console.log("ERROR");
console.log(error);
deferred.reject();
});
return deferred.promise;
};
I am basically just getting the text file from AWS, reading it, concatating the new string onto the file and then reuploading it.
Now I have tried a variety of functions to convert a Uint8Array to a String such as this one:
https://ourcodeworld.com/articles/read/164/how-to-convert-an-uint8array-to-string-in-javascript
and this one:
Conversion between UTF-8 ArrayBuffer and String
Niether seems to work(Still prints as an [object Object] and I have also tried TextDecoder.
Has anyone ever updated text or a log file in AWS S3? How did you do it? I have a slight suspicious that this file is not a Uint8Array file otherwise these conversion methods would have worked.
EDIT:
awsServices.getObject(key).then(function(data) {
console.log("UPLOAD LOG");
console.log(typeof(data)); //object
console.log(data); //*Screenshot above
console.log(data instanceof Blob); //false
console.log(data instanceof ReadableStream); //false
console.log(data instanceof Object); //true
data = uintToString(data.Body);
console.log(typeof(data)); //string
console.log(data); //[object Object]
newMessage = message.toString() + '\r\n\r\n' + data;
var blob = new Blob([newMessage.toString()], {type: "text"});
var file = new File([blob], key);
awsServices.upload(file, moment.key, moment).then(function() {
deferred.resolve();
}, function(error) {
console.log("UPLOAD LOG REJECT");
deferred.reject();
});
}, function(error) {
Ensure that the S3 object metadata has the correct content type: set Content-Type = text/plain.
Then use data.Body.toString('ascii'), or some other encoding such as utf-8.

How to send binary data in response from node.js server

I am new to mongodb.
I stored binary data with below code snippet:
var data = fs.readFileSync(path);
var image = new mongodb.Binary(data);
//open connection/collection
var record = {picname: id, content: image };
collection.save(record, {safe: true}, function(err,result){
if(err)
console.log(err.stack);
});//save
I can see the record size in db. there is binary data. record size also matched with file size. am happy.
Now, retrieved same binary data from mongo and trying to send it in response:
var record = {picname: id};
collection.findOne(record, function(err,result){
if(err)
console.log(err.stack);
else
{
console.log('before read from db for download.');
//HOW TO READ IMAGE/BINARY DATA FROM RESULT?
//I need to send result in response. Any Idea?
console.log('before read from db for download');
}
});
I am sending binary data with below code snippet. It's not working for all the files. What could be the issue:
collection.findOne(record, function(err,result){
if(err)
console.log(err.stack);
else
{
console.log('before read from db for download. result: [' + result.picname + " ], type: " + result.imagetype);
res.end(result.content.buffer, "binary");
console.log('Responded SUCCESS: ' + id );
}
});//findOne
Please let me know how to retrieve and send via response.
Thanks in advance
DD.
Your problem here is not so much with storing and reading the data, but is actually all about content types. So ideally you want to store this with your data as well as return the correct header information when you send the response.
So part of this would be mime type detection. There are modules available, mmmagic is one of them
var Magic = require('mmmagic').Magic;
var magic = new Magic();
var data = fs.readFileSync(path);
var image = new mongodb.Binary(data);
//open connection/collection
magic.detect(data,function(err,result) {
var record = {picname: id, content: image, mimeType: result };
collection.save(record, {safe: true}, function(err,result){
if(err)
console.log(err.stack);
});//save
});
Methods for writing the header vary, but with the base "http" for node you call as shown:
var record = {picname: id};
collection.findOne(record, function(err,result){
if(err)
console.log(err.stack);
else {
res.writeHead(200, {
'Content-Type': result.mimeType,
'Content-Length': result.content.length
});
res.write(result.content.buffer);
res.end();
}
});
So what effectively gets returned here is the binary data identified by it's correct mime type. So you can access this from an URL where you supply the means to lookup the document and view directly in a browser just as if it was a regular image file being served.

Resources