Skip to content

Instantly share code, notes, and snippets.

@sylvinus
Created December 15, 2012 21:10
Show Gist options
  • Save sylvinus/4299247 to your computer and use it in GitHub Desktop.
Save sylvinus/4299247 to your computer and use it in GitHub Desktop.
3D Model upload to Sketchfab from Node.js
node_modules
{
"name":"sketchfab-node",
"version":"0.0.1",
"dependencies":{
"request":"2.12.0",
"form-data":"0.0.5"
}
}
// Uses https://npmjs.org/package/request 2.12.0
// Uses https://npmjs.org/package/form-data 0.0.5
// Run "npm install request form-data" or add them to your project's package.json
var request = require("request");
var formData = require('form-data');
var fs = require("fs");
var uploadModel = function(callback) {
var form = new formData();
form.append("title","Uber Glasses");
form.append("description","Test of the api with a simple model");
form.append("fileModel",fs.createReadStream(__dirname + '/model.dae'));
form.append("filenameModel","model.dae");
form.append("tags","test collada glasses");
form.append("token","ff00ff");
form.append("private",1);
form.append("password","Tr0b4dor&3");
form.getLength(function(err,length) {
if (err) return callback(err);
var r = request.post('https://api.sketchfab.com/v1/models',
{headers: {'content-length': length}},
function(err, response, body) {
if (err) return callback(err);
try { body = JSON.parse(body); } catch (e) { return callback(e); }
if (body.success!==true) return callback(new Error(body.error));
callback(null,body.result.id);
}
);
r._form = form;
});
};
if (require.main === module) {
uploadModel(function(err, id) {
if (err) {
console.error("Couldn't upload:",err);
} else {
console.log("Upload success. Model id:",id);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment