hgourvest / node-firebird

Pure javascript and asynchronous Firebird client for Node.js.
Mozilla Public License 2.0
257 stars 128 forks source link

Convert sequentially selected blob binary files to image files #81

Open ghost opened 9 years ago

ghost commented 9 years ago

I would like to convert sequentially selected blob subtype 0 files from Firebird DB to base64 string and convert this base64 string to image files.

When I was running my code, I got these errors:

response.data = new Buffer2(matches[2], 'base64'); ^ TypeError: object is not a function at decodeBase64Image (/Users/bla/upload.js:59:37) at EventEmitter. (/Users/bla/upload.js:64:35) at EventEmitter.emit (events.js:107:17) at /Users/node_modules/node-firebird/lib/index.js:3840:31 at doCallback (/Users/pilotworkstation/node_modules/node-firebird/lib/index.js:1237:5) at Socket. (/Users/node_modules/node-firebird/lib/index.js:2909:17) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)

Any suggestions?

var Firebird = require('node-firebird');
var base64 = require('node-base64-image');
var fs = require('fs');
var Buffer2 = require('buffer');
var firebirddb = {};
firebirddb.database = '/Users/bla/mydata.FDB';
firebirddb.user = 'SYSDBA';
firebirddb.password = 'masterkey';
var pool = Firebird.pool(10, firebirddb);
pool.get(function (err, db) {
    if (err)
        throw err;
    var k = 0;
    db.sequentially('SOME QUERY', function (row, index) {
        row.myblob(function (err, name, e) {
            if (err)
                throw err;
            e.on('data', function (chunk) {
                var data = 'data:image/jpeg;base64,' + chunk.toString('base64');
                function decodeBase64Image(dataString) {
                    var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
                        response = {};

                    if (matches.length !== 3) {
                        return new Error('Invalid input string');
                    }

                    response.type = matches[1];
                    response.data = new Buffer2(matches[2], 'base64');

                    return response;
                }

                var imageBuffer = decodeBase64Image(data);
                console.log(imageBuffer);
            });
            e.on('end', function () {

            });
        });
    }, function (err) {
        db.detach();
    });
});
pool.destroy();
felipeoliveirar commented 5 years ago

Hi, I solve the issue with the following work arround.

   rows[i].image(function(err, name, eventEmitter) {
        let buffers = [];
        eventEmitter.on('data', function(chunk) {
           buffers.push(chunk);
        });
        eventEmitter.on('end', function() {
        let buffer = Buffer.concat(buffers);

         await fs.writeFile(fileName, data, async function (err) {
             if (err) {
               console.log(err);
              } else {
               // here you already have the imagem on the path(fileName)
                }
              });
        });
      });`

there are another options here: https://stackoverflow.com/questions/46574826/firebird-blob-to-base64-node-js, but this on solve the issue. In the event on.('data', ) was calling several times, because the file was big. then it was necessary to "mount" first, and after when the event 'end' was called, you need just concact the array.