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

How to upload video thumbnail using PUT request? #90

Closed
1rosehip opened this issue Nov 14, 2018 · 6 comments
Closed

How to upload video thumbnail using PUT request? #90

1rosehip opened this issue Nov 14, 2018 · 6 comments

Comments

@1rosehip
Copy link

1rosehip commented Nov 14, 2018

Hello,
I'm trying to upload a video thumbnail following the https://developer.vimeo.com/api/upload/thumbnails#uploading-a-thumbnail docs.
What is the right way to implement Step 3 in documentation - PUT{link}?

I tried the following but It doesn't seem to work:

vimeoClient.request(
	{
		method: 'PUT',
		path: '/videos/300131978:1644b9398c/pictures/739089317',
		query: fs.readFileSync('./img/test.jpg'),
		headers: {
			'Content-Type': 'image/jpg', //	image/jpg, image/png, or image/gif, 
			'Accept': 'application/vnd.vimeo.*+json;version=3.4'
		}
	},
	(error, data , statusCode, headers) => {
		
		//...
		
	}
);

Thanks!

@1rosehip
Copy link
Author

1rosehip commented Nov 15, 2018

In case someone else needs it I've found a way around it using standard node.js https module. Please note that this is only step 3 of the whole upload process:

const https = require('https');
const Vimeo = require('vimeo').Vimeo;
const vimeoClient = new Vimeo(CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN);

/**
 * upload a thumbnail - step #3 in the docs
 * https://developer.vimeo.com/api/upload/thumbnails#uploading-a-thumbnail 
 * @param {string} url - received from the previous step
 * @param {string} pathToImg - path to the image, for example './img/test.jpg'
 * @param {Function} callback
 */
const uploadThumb(url, pathToImg, callback) => {
	
	const opt = vimeoClient._buildRequestOptions({
		method: 'PUT',
		port: 443,
		hostname: 'i.cloud.vimeo.com',
		path: url.replace('https://i.cloud.vimeo.com', ''),
		query: pathToImg,
		headers: {
			'Content-Type': 'image/jpg',
			Accept: 'application/vnd.vimeo.*+json;version=3.4',
		}
	});
		
	let output = '';
	const req = https.request(opt, function(res) {
		
	  //console.log('STATUS: ' + res.statusCode);
	  //console.log('HEADERS: ' + JSON.stringify(res.headers));
	  
	  res.setEncoding('utf8');
	  
	  res.on('end', () => {			
		  callback(output);
	  });

	  res.on('data', function (chunk) {
		output += chunk;
	  });
	});	
	
	req.on('error', function(e) {
		console.log('Error: ' + e.message);
	});

	// write data to request body
	//req.write('data\n');
	//req.write('data\n');
	//req.end();
	
	fs.createReadStream(pathToImg).pipe(req);
};

Anyway it will be great if documentation will be updated with examples about thumbnail upload. Ideally we need only method that do all the steps needed to upload a thumbnail instead 4 different requests.

Thanks

@erunion
Copy link
Contributor

erunion commented Dec 11, 2018

We'll be adding a dedicated method to support thumbnail uploads soon!

@erunion erunion closed this as completed Dec 11, 2018
@ariestav
Copy link

ariestav commented Apr 9, 2019

Is that module release? It should be similar to the .upload() where the asset is the first parameter in the function signature.

The documentation states:

The binary data of the image file goes in the body of the request.

How do you set the body using the NodeJS SDK you supply?

@ariestav
Copy link

ariestav commented Apr 10, 2019

@1rosehip I tried your function for step 3 of the vimeo upload, but couldn't get it to work. I get a 411 status error for the response. I think that has to do with setting the "Content-Length" of the request, but am not sure how to set that header properly. Any ideas?

Also, why do you use i.cloud.vimeo.com instead of api.vimeo.com?

@1rosehip
Copy link
Author

@1rosehip I tried your function for step 3 of the vimeo upload, but couldn't get it to work. I get a 411 status error for the response. I think that has to do with setting the "Content-Length" of the request, but am not sure how to set that header properly. Any ideas?

Also, why do you use i.cloud.vimeo.com instead of api.vimeo.com?

It seems that they changed their API since I implemented this code.

411 is content length status code. Maybe use can try something like this?

const opt = vimeoClient._buildRequestOptions({
		method: 'PUT',
		port: 443,
		hostname: 'i.cloud.vimeo.com',
		path: url.replace('https://i.cloud.vimeo.com', ''),
		query: pathToImg,
		headers: {
			'Content-Type': 'image/jpg',
			Accept: 'application/vnd.vimeo.*+json;version=3.4',
                         'Content-Length': length // Buffer.byteLength(p)
		}
});

...

@karlhorky
Copy link
Contributor

Anyone who is coming here to try to create thumbnails from a frame of the video, you can check out my comment over here:

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

4 participants