Files
NodeBB/src/imgur.js

40 lines
794 B
JavaScript
Raw Normal View History

2013-11-23 17:56:03 -05:00
var request = require('request'),
winston = require('winston');
2013-07-21 12:29:57 -04:00
2013-09-18 10:50:02 -04:00
(function (imgur) {
"use strict";
2013-11-23 17:56:03 -05:00
imgur.upload = function (clientID, image, type, callback) {
2013-07-21 12:29:57 -04:00
var options = {
url: 'https://api.imgur.com/3/upload.json',
headers: {
'Authorization': 'Client-ID ' + clientID
2013-09-17 13:09:37 -04:00
}
2013-07-21 12:29:57 -04:00
};
2013-09-17 13:09:37 -04:00
2013-09-18 10:50:02 -04:00
var post = request.post(options, function (err, req, body) {
2013-11-23 17:56:03 -05:00
if(err) {
return callback(err, null);
}
2013-09-17 13:09:37 -04:00
try {
2013-11-23 17:56:03 -05:00
var response = JSON.parse(body);
if(response.success) {
callback(null, response.data);
} else {
callback(new Error(response.data.error.message), null);
}
} catch(e) {
winston.error('Unable to parse Imgur json response. [' + body +']');
callback(e, null);
2013-07-21 12:29:57 -04:00
}
});
2013-09-17 13:09:37 -04:00
2013-11-23 17:56:03 -05:00
post.form({
2013-09-17 13:09:37 -04:00
type: type,
image: image
});
2013-09-18 10:50:02 -04:00
};
2013-09-17 13:09:37 -04:00
}(exports));