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

Creating png file from buffer | content is invalid #205

Closed
anirbanctts opened this issue Sep 28, 2018 · 9 comments
Closed

Creating png file from buffer | content is invalid #205

anirbanctts opened this issue Sep 28, 2018 · 9 comments

Comments

@anirbanctts
Copy link

anirbanctts commented Sep 28, 2018

Getting: StatusCodeError: 400 - {"error":"content is invalid"}
using

client.RepositoryFiles.create(projectid, path, branch, {content: content, encoding: 'base64', commit_message: message}).then(callback);

When buffer was printed in console - <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 05 01 00 00 03 0f 08 06 00 00 00 65 a0 13 26 00 00 20 00 49 44 41 54 78 5e ec dd 09 dc 95 73 fe ... >

Please suggest

buffer was created with....
At Client

            // converts blob to base64
                var blobToBase64 = function(blob, cb) {
                    var reader = new FileReader();
                    reader.onload = function() {
                    var dataUrl = reader.result;
                    var base64 = dataUrl.split(',')[1];
                    cb(base64);
                    };
                    reader.readAsDataURL(blob);
                };
                
                blobToBase64(blob, function(base64) { // encode

                    var updateBlob = {'blob': base64};
                    //calling save image blob API

At Server

var client = gitlab({url:url,oauth_token:accessToken});
 var content = new Buffer(modelInfo.body.blob, 'base64'); // decode

Originally posted by @anirbanctts in #200 (comment)

@jdalrymple
Copy link
Owner

Ill look into it!

@anirbanctts
Copy link
Author

Hi, Any update?

@jdalrymple
Copy link
Owner

Not yet sorry! Work has been pretty hectic :(

@anirbanctts
Copy link
Author

Let me know when you can, still waiting for the resolution. Have only few days left for going live

@jdalrymple
Copy link
Owner

So ive tested this code with text content and it seems to work. It only fails when i test with base64 data. Im going to keep testing and see what i can come up with.

@jdalrymple
Copy link
Owner

Here got it to work, just needed to encode my image properly:

	const Gitlab = require('../../node-gitlab').default;

	const { RepositoryFiles } = new Gitlab({
		token: 'token',
	});

	const content = await Fs.readFile('small.png');

	await RepositoryFiles.create(id, 'fake2.png', 'master', {
		content: content.toString('base64'),
		encoding: 'base64',
		commit_message: 'message',
	});

@jetersen
Copy link
Contributor

Perhaps make it a unit test and provide the example in readme? 😇 🙌

@anirbanctts
Copy link
Author

Thanks a lot. Closing the issue. will try to add to the documentation

@jdalrymple
Copy link
Owner

Add file creation to documentation #39

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

No branches or pull requests

3 participants