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

Accept buffers on file prop using request module support #21

Merged
merged 1 commit into from
Aug 13, 2021

Conversation

luizstacio
Copy link
Contributor

We need send a image from a bas64 string, but the current kraken module don't support. It works only with file paths and read streams. But the module 'request' used on it, has support for buffer if you send the options with filename and contentType.

The propose solution is only enable to use the kraken API passing a object on the prop file with value buffer and options.

Use case:

const Kraken = require('./kraken-node');
const kraken = new Kraken({
    api_key: '<...>',
    api_secret: '<...>'
});

const data = body64.replace(/^data:image\/\w+;base64,/, '');
const buffer = new Buffer(data, 'base64');
const opts = {
    file: {
        value: buffer,
        options: {
            filename: `${Date.now()}.png`,
            contentType: 'image/png'
        }
    },
    wait: true
};

kraken.upload(opts, function(err, data) {
    if (err) {
        console.log('Failed. Error message: %s', err);
    } else {
        console.log('Success. Optimized image URL: %s', data.kraked_url);
    }
});

The solution without this is use directly the API:

request.post({
    url: 'https://api.kraken.io/v1/upload',
    json: true,
    strictSSL: false,
    formData: {
        data: JSON.stringify({
            auth: {
                api_key: "<...>",
                api_secret: "<...>"
            },
            wait: true
        }),
        file: {
            value: buffer,
            options: {
                filename: `${Date.now()}.png`,
                contentType: 'image/png'
            }
        }
    }
}, function optionalCallback(err, httpResponse, body) {
  	if (err || !body.success) {
	    console.log('Failed. Error message: %s', err || body);
	} else {
	    console.log('Success. Optimized image URL: %s', body);
	}
});

Thank you, and sorry my english. If have more questions please send me.

@adamreisnz
Copy link

This should be merged, see also #23 and #28

@samlevin
Copy link

samlevin commented Feb 6, 2020

any update on whether the file prop supports a buffer?

@ZloeSabo
Copy link
Contributor

Thank you for your contribution! Better late than never :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants