Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a file is really an image in express/node.js

How can I check if the file uploaded on my server is really an image? Not just a file with (jpg,png,gif) extension just to make it as "an image" file. I created an image compression service using imagemin, but I'm afraid if the uploaded file is really an image.

like image 765
user3569641 Avatar asked Sep 06 '15 13:09

user3569641


People also ask

How do I validate an image using Joi?

// node backend schema validation const Joi = require("joi-browser"); Joi. image = require("joi-image-extension"); const image = new mongoose. Schema({ image: { type: String, required: true, }, }) // validation function function validateEmployee(employee) { const schema = { image: Joi. image().


2 Answers

I have used the mmmagic module for this, it discovers mime types: mmmagic on Github

like image 180
Dimitri Avatar answered Oct 11 '22 10:10

Dimitri


Another option is https://github.com/sindresorhus/image-type. Looks like mmmagic relies on libmagic which is a C lib and might be a lot to pull in...

like image 29
BenO Avatar answered Oct 11 '22 10:10

BenO