Node.js / MongoDB / Mongoose: Buffer Comparison

First, a little background:

I am trying to check if binary image data is saved in Mongo. Given the following scheme:

var mongoose = require('mongoose') , Schema = mongoose.Schema; var imageSchema = new Schema({ mime: String, bin: { type: Buffer, index: { unique: true }}, uses : [{type: Schema.Types.ObjectId}] }); module.exports = mongoose.model('Image', imageSchema); 

... I want to ask if the image exists if it adds the link that my object uses and then updates it. If it is not, I want to create (update) it.

Given that it does not exist, the code below works fine. If so, the following code does not and adds another image document to Mongo. I feel this is probably a comparison problem for type Mongo Buffer vs node Buffer, but I can't figure out how to compare them correctly. Please let me know how to update below! Thanks!

 Image.findOneAndUpdate({ mime : contentType, bin : image }, { $pushAll : { uses : [ myObject._id ] } }, { upsert : true }, function(err, image) { if (err) console.log(err); // !!!image is created always, never updated!!! }); 
+4
source share
3 answers

Mongoose converts buffer elements intended for storage in mongodb Binary , but executes corresponding casts when executing queries. The expected behavior is also checked in the test blocks (also storing and retrieving the node.js buffer).

Are you sure you are using the node.js buffer?

In any case, I believe that the best approach for handling the original problem (check if the image is already in db) would be to store strong hash digest (sha1, sha256, ...) of binary data and verify that (using the cryptographic module) . When requested as a preliminary test, you can also check the binary length to avoid unnecessary calculations.

An example of how to get a digest for your image before saving / requesting it:

 var crypto = require('crypto'); ... // be sure image is a node.js Buffer var image_digest = crypto.createHash('sha256'); image_digest.update(image); image_digest = image_digest.digest('base64'); 
+6
source

It is not recommended to request an image using the node.js buffer, which contains image data. You are right that this is probably the problem between the BSON binary data type and the node buffer, but does your application really require such a comparison?

Instead, I will add an imageID or slug field to my schema, add an index to this field, and query it instead of bin in your findOneAndUpdate call:

 var imageSchema = new Schema({ imageID: { type: String, index: { unique: true }}, mime: String, bin: Buffer, uses : [{type: Schema.Types.ObjectId}] }); 
0
source

the hash works, another filter that I used is exif data for the image. Since this is structured information, if you have matching according to exif data, you can proceed to the next step of checking compliance by hash or file size ...

heaps of node modules to get exif data with ease and ease for your repository :) sample code for getting exif data for node

0
source

Source: https://habr.com/ru/post/1501514/


All Articles