Need to validate file inputs in Node.js - node.js

I m using Node + Express + MongoDB to develop my application. In my application there are many file uploading forms. The uploading process is done perfecly. But my problem is , currently there is no validation for the file inputs. I need to validate these file inputs , like
Need to upload only .jpeg, .png files only.
File size must be less than 1MB.
But dont know how to solve this. I used mongoose.js' model schemas to validate strings and numbers. Searched a lot for finding a solution for file validation, but failed. Anyone who knows how to handle this problem, please help me

You should use npm module 'mmmagic' to check file type
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING);
// the above flags can also be shortened down to just: mmm.MAGIC_MIME_TYPE
magic.detectFile('path/to/your/file.jpg', function(err, result) {
if (err) throw err;
console.log(result);//result is an object containig the file type
});
You can check file size using 'fs' node module

Related

How to read and write xlxs and csv file using nodejs

first, I want to upload .xlxs and .csv file in node.js. then I want to read this file, edit this file, and also want to save this file in mongoose schema in the database.
and I am new in node.js and I want to learn node backend
please guide me, how I can do this and is this possible?
Reading and writing files in node uses the fs module. Its already included in core node so simply include it
const fs = require('fs')
// synchronous version that will wait until the whole file is read
let file_data = fs.readFileSync('/file/path/file.csv' , 'utf-8')
// async version
fs.readFile('/file/path/file.csv' , 'utf-8', (err, file_data) => {
if (err)
console.error(err)
else
console.info(file_data)
// file_data is your data as string
})
If you specifically need to read and edit csv/xlsx you'll need to browse NPM for packages that help out with that.
You can do it easily by using sheetjs
See this example : https://github.com/SheetJS/sheetjs/tree/master/demos/server

how to prompt where to download zip file created with archiver in node

I am trying to create a zip file in node using the code provided from how to create a zip file in node given multiple downloadable links, as shown below:
var fs = require('fs');
var archiver = require('archiver');
var output = fs.createWriteStream('./example.zip');
var archive = archiver('zip', {
gzip: true,
zlib: { level: 9 } // Sets the compression level.
});
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the output file
archive.pipe(output);
// append files
archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
archive.file('/path/to/README.md', {name: 'foobar.md'});
//
archive.finalize();
When I use this suggestion, the zip file is downloaded without any kind of prompt asking me where I would like to save the file - is there any way I can make it so that a prompt is created asking me where I would like to save the file, which is quite normal these days?
If this is absolutely not possible, would it be possible to always save the file in the downloads folder (regardless of whether on mac or windows or any other operating system)?
So there's a couple of things here. In terms of a 'prompt' or 'pop-up' you won't find anything along the lines of WinForms out of the box, there are options for the command line such as prompts You can use that as your user input.
https://www.npmjs.com/package/prompts
You'll want to use path and more specifically path.join() to combat the mac/windows/linux issue.
Do you need to use path.join in node.js?
You can run an express server and create a route that uses res.download() in which you would provide the zipped file.
https://expressjs.com/en/api.html#res.download

How do I extract information from text file using node.js

How can i search key words into a text and return the data just after this key word with node js i find it with php and c# but not with javascript or node js?
We can implement the above scenario using node's fs module.With the help of this we can read the file's data and perform the keyword search operations on the processed data.
var fs = require("fs")
fs.readFile("somefile.txt", function(err, content) {
if (err) throw err;
// Do Required Operations with the content which is the file data
});
Take a look at npm config.
It sounds like you are trying to parse an ini file, or something like that. If so, you can use the .properties file format.
So assuming you store your text file as config/default.properties, you could do something like:
const config = require('config');
const MYVALUE = config.get('MYKEY');

How can I display files that have been saved as a Buffer?

I am saving files as Buffers in my mongo database (using mongoose, nodejs, electron). For now, I'm keeping it simple with text-only files. I read a file in using
fs.readFile(filePath, function(err, data) {
if (err) {console.log(err);}
typeof callback == "function" && callback(data);
});
Then I create a new file in my database using the data variable. And, now I have something that looks like BinData(0,"SGVsbG8gV29ybGQK") stored in my mongodb. All is fine so far.
Now, what if I wanted to display that file in the UI? In this case, in Electron? I think there are two steps.
Step 1 The first is bringing this variable out of the DB and into the front-end. FYI: The model is called File and the variable that stores the file contents is called content.
So, I've tried File.content.toString() which gives me Object {type: "Buffer", data: Array[7]} which is not the string I'm expecting. What is happening here? I read here that this should work.
Step 2 Display this file. Now, since I'm only using text files right now, I can just display the string I get once Step 1 is working. But, is there a good way to do this for more complex files? Images and GIFs and such?
You should save the file mime.
And then set response.header MIME
response.setHeader("Content-Type", "image/jpeg");
response.write(binary)
response.end()

How can you persist user data for command line tools?

I'm a front-end dev just venturing into the Node.js, particularly in using it to create small command line tools.
My question: how do you persist data with command line tools? For example, if I want to keep track of the size of certain files over time, I'd need to keep a running record of changes (additions and deletions) to those files, and relevant date/time information.
On the web, you store that sort of data in a database on a server, and then query the database when you need it again. But how do you do it when you're creating a Node module that's meant to be used as a command line tool?
Some generic direction is all I'm after. I don't even know what to Google at this point.
It really depends on what you're doing, but a simple approach is to just save the data that you want to persist to a file and, since we're talking node, store it in JSON format.
Let's say you have some data like:
var data = [ { file: 'foo.bar', size: 1234, date: '2014-07-31 00:00:00.000'}, ...]
(it actually doesn't matter what it is, as long as it can be JSON.stringifiy()d)
You can just save it with:
fs.writeFile(filename, JSON.stringify(data), {encoding: 'utf8'}, function(err) { ... });
And load it again with:
fs.readFile(filename, {encoding: 'utf8'}, function(err, contents) {
data = JSON.parse(contents);
});
You'll probably want to give the user the ability to specify the name of the file you're going to persist the data to via an argument like:
node myscript.js <data_file>
You can get that passed in parameter with process.argv:
var filename = process.argv[2]; // Be sure to check process.argv.length and have a default
Using something like minimist can be really helpful if you want to get more complex like:
node myscript.js --output <data_file>
You also can store files in temporary directory, for example /tmp directory on linux and give user the option to change the directory.
To get path to temporary directory you can use os module in nodejs:
const os = require('os');
const tmp = os.tmpdir();

Resources