aheckmann / gm

GraphicsMagick for node
http://aheckmann.github.com/gm/
6.95k stars 616 forks source link

Abort when file is not an image #329

Open kokujin opened 10 years ago

kokujin commented 10 years ago

I am storing avatars and creating thumbnails with the gm lib, I use ".identify()" to make sure that the file uploaded is one of type JPEG or PNG. How do I abort other chained functions in case the file type does not match the types that I have white-listed? Thanks

gm(data.avatar, 'img.jpg')
                    .identify({
                        bufferStream: true
                    }, function(err, info) {
                        console.log('info: ', info);
                        if (info.format == 'JPEG' || info.format == 'PNG') {
                            // valid image
                        } else {
                           // BAD FILE
                        }
                    }).resize('200', '200') 
                    .stream(function(err, stdout, stderr) {
                        var writeStream = fs.createWriteStream(path);
                        stdout.pipe(writeStream);
                    });
meteormatt commented 9 years ago

I think you can lookup MIME type self. not by gm.

chinajuanbob commented 9 years ago

Try two gm calls, one for type, if it callback the type you want, then one more call for other operations.