Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert Node Buffer into a BLOB #6627

Closed
khalilTN opened this issue Jan 29, 2015 · 0 comments
Closed

Insert Node Buffer into a BLOB #6627

khalilTN opened this issue Jan 29, 2015 · 0 comments

Comments

@khalilTN
Copy link

I'm trying to insert a buffer into a binary field (BLOB in MySQL), in /lib/utils.js prepareValue function, the buffer is treated as an object and is stringified (json.stringify()) and so data in the buffer is lost.
I think the buffer type should be handled inside utils.prepareValue() function.
I tried to read an image from disk, create a buffer of by the image bytes, save it in the database and then get it back from the database and write it. The result file is corrupted.
Suggested solution is to add a test on the object in the prepareValue function, if it's a buffer only escape it.

//Suggested Solution in /lib/utils.prepareValue() 
if(Buffer.isBuffer(value))
      return mysql.escape(value);




//This is my test code
var fs = require('fs');
var buffer = fs.readFileSync('/tmp/test/test.jpg')
attachment.create({id : 20001,data : buffer}).exec(function(err,res){
console.log(err||'-#success');
attachment.findOne().where({id:20001}).exec(function(err,res){
   if(!err) {
      fs.writeFile('/tmp/test/result.jpg', res.data, function(err) {
              if(err) {
                  console.log(err);
              }else{
                        console.log("The file was saved!");
              }
      }); 
   }
});
});//result.jpg is corrupted
//This is my Model :
{
    "identity": "attachment",
    "tableName": "attachment",
    "attributes": {
        "id": {
            "type": "integer",
            "primaryKey": true
        },
        "data": {
            "type": "binary"
        }
     }
}


@johnabrams7 johnabrams7 transferred this issue from balderdashy/sails-mysql Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant