Modify metadata of file - javascript

I would like to create an app that allows me to easily modify the metadata of any given file.
Is there a way to modify the metadata of any given file type?
Are there any NPM packages that facilitate this?

You can use node-ffmetadata npm package for reading & writings file properties.
var ffmetadata = require("ffmetadata");
// Read song.mp3 metadata
ffmetadata.read("song.mp3", function(err, data) {
if (err) console.error("Error reading metadata", err);
else console.log(data);
});
// Set the artist for song.mp3
var data = {
artist: "Me",
};
ffmetadata.write("song.mp3", data, function(err) {
if (err) console.error("Error writing metadata", err);
else console.log("Data written");
});

Related

Fetching data from Google Drive using Node JS [duplicate]

Im trying to get the contents of a file using the google drive API v3 in node.js.
I read in this documentation I get a stream back from drive.files.get({fileId, alt: 'media'})but that isn't the case. I get a promise back.
https://developers.google.com/drive/api/v3/manage-downloads
Can someone tell me how I can get a stream from that method?
I believe your goal and situation as follows.
You want to retrieve the steam type from the method of drive.files.get.
You want to achieve this using googleapis with Node.js.
You have already done the authorization process for using Drive API.
For this, how about this answer? In this case, please use responseType. Ref
Pattern 1:
In this pattern, the file is downloaded as the stream type and it is saved as a file.
Sample script:
var dest = fs.createWriteStream("###"); // Please set the filename of the saved file.
drive.files.get(
{fileId: id, alt: "media"},
{responseType: "stream"},
(err, {data}) => {
if (err) {
console.log(err);
return;
}
data
.on("end", () => console.log("Done."))
.on("error", (err) => {
console.log(err);
return process.exit();
})
.pipe(dest);
}
);
Pattern 2:
In this pattern, the file is downloaded as the stream type and it is put to the buffer.
Sample script:
drive.files.get(
{fileId: id, alt: "media",},
{responseType: "stream"},
(err, { data }) => {
if (err) {
console.log(err);
return;
}
let buf = [];
data.on("data", (e) => buf.push(e));
data.on("end", () => {
const buffer = Buffer.concat(buf);
console.log(buffer);
});
}
);
Reference:
Google APIs Node.js Client

How to protect rabbitmq passwords when using rascal config file on nodejs

I'm trying to implement a NodeJS microservice, and I need it to use RabbitMQ.
For the rabbit integration, I'm using 'rascal' (https://github.com/guidesmiths/rascal) since it solves lots of my concerns out of the box.
I noticed that rascal is driven by a config file where you declare the rabbit URL, username, password, and more.
My question is, what is the best practice for protecting those passwords in the rascal config file so it will be
a) not be pushed to git
b) not be exposed so easily
I suggest that loading secrets as environment variables. Rather than JSON, Rascal configurations can also be defined as JavaScript files (see the example from Rascal's repository). Then, secrets can be accessed inside of config file at runtime via process.env.<SECRET_NAME>.
Use amqplib npm module to use a rabbitmq integration on nodejs.
I give sample publish consume code on rabbitmq.
Publish code:
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://username:password#localhost:5672', function(err, connection) {
if (err) throw err;
connection.createChannel(function(err, channel) {
if (err) throw err;
var exchange = 'data';
var msg = "Welcome to Node and rabbitMQ";
channel.assertExchange(exchange, 'fanout', {
durable: false
})
channel.publish(exchange, '', Buffer.from(msg));
console.log(msg)
})
})
Consume Code
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://username:password#localhost:5672', function(err0, connection) {
if (err0) throw err0;
connection.createChannel(function(err1, channel) {
if (err1) throw err1;
var exchange = 'data';
channel.assertExchange(exchange, 'fanout', { durable: false })
channel.assertQueue('', {
exclusive: true
}, function(err2, value) {
channel.bindQueue(value.queue, exchange, '');
channel.consume(value.queue, function(msg) {
console.log("message:", msg.content.toString())
}, { noAck: true })
})
})
})

Can't generate PDF from html

tried to use several libraries for pdf generation but I catch always one error:
Fatal Error: spawn UNKNOWN
code, something like is:
mammoth.convertToHtml({
path: './backend/common/template.docx'
})
.then(function (result) {
var html = result.value; // The generated HTML
pdf.create(html).toFile("./backend/common/vvvv.pdf", function (err, res) {
if (err) {
callback(err);
console.log(err);
return;
}
callback(null, res);
});
var messages = result.messages; // Any messages, such as warnings during conversion
console.log(messages);
})
.done();
Help please, what is wrong
I think you miss the import
var pdf = require('html-pdf');
Make sure you have installed the html to pdf converter through node and imported/required the file!

node.js code to append data to a file

How can I append data to a file using node.js
I already have a file named myfile.json with data. I want to check if the file name exists and then append some data to that file.
I'm using following code
var writeTempFile = function (reportPath, data, callback) {
fs.writeFile(reportPath, data, function (err) {
//if (err) //say(err);
callback(err);
});
}
writeTempFile(reportDir + '_' + query.jobid + ".json", data, function (err) {
context.sendResponse(data, 200, {
'Content-Type': 'text/html'
});
You can use jsonfile
var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
if(!err) {
var finalData = merge(obj, yourdata);
jf.writeFile(file, finalData, function(err) {
console.log(err);
});
}
});
You need to implement your merging logic in merge(object1, object2)
https://npmjs.org/package/jsonfile
Check out the following code.
function addToFile(reportPath, data, callback){
fs.appendFile(reportPath, data, function (err) {
callback(err);
});
}
Node offers fs module to work with file system.
To use this module do
var fs = require('fs')
To append some data to file you can do:
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation

How to retrieve the Metadata from nodejs aws s3 getObject callback data?

I am trying to upload/download an audio chunk file to/from S3 using AWS node SDK. I have tried base64 approach and it works fine. But I am not able to get the Metadata back which I have bundled as part of upload params.
Below is the code snippet for upload along with meta info:
var myMetaInfo = "AdditionalInfo", dataToUpload = {Bucket: bucketName, Key:storageFolderFullPath , Body: myAudioFile.toString('base64'), Metadata: {metaInfo: myMetaInfo}};
s3.client.putObject(dataToUpload, function(err, data) {
if (!err) {
console.log("Successfully uploaded the file to ::" + dataToUpload.Bucket);
} else {
console.log(" **** ERROR while uploading ::"+err);
}
});
And this is the snippet for downloading the file. Metadata is not part of the callback data.
I tried printing the callback 'data' to console and noticed that only the following params are available
LastModified, ContentType, ContentLength, ETag, Body, RequestId
var dataToDownload = {Bucket: bucketName, Key: storageFolderFullPath}, originalFile, myMetaInfo;
s3.client.getObject(dataToDownload, function(err, data) {
if (!err) {
originalFile = new Buffer(data.Body, 'base64');
myMetaInfo = data.Metadata.metaInfo;
console.log(" Meta info:: " + myMetaInfo);
fs.writeFile(fileStoragePath, originalFile, function(err) {
if (!err) {
console.log(" File written!! ");
} else {
console.log(" Error while writing the file !!" + err);
}
});
} else {
console.log(" **** ERROR while downloading ::"+err);
}
});
Any pointers on what is wrong with my implementation? I have followed the documentation mentioned here
Any help is appreciated.
Is your metaInfo value a string?
Referencing the sdk api docs, Metadata is a string map (ala ~ Metadata: {metaInfo: "myMetaInfoString"}. I've tested your code using a string as the value for metaInfo and it does return correctly under the data.Metadata.metaInfo reference.

Categories