mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-11-03 20:45:58 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			795 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			795 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
var request = require('request'),
 | 
						|
	winston = require('winston');
 | 
						|
 | 
						|
 | 
						|
(function (imgur) {
 | 
						|
	"use strict";
 | 
						|
 | 
						|
	imgur.upload = function (clientID, image, type, callback) {
 | 
						|
		var options = {
 | 
						|
			url: 'https://api.imgur.com/3/upload.json',
 | 
						|
			headers: {
 | 
						|
				'Authorization': 'Client-ID ' + clientID
 | 
						|
			}
 | 
						|
		};
 | 
						|
 | 
						|
		var post = request.post(options, function (err, req, body) {
 | 
						|
			if(err) {
 | 
						|
				return callback(err, null);
 | 
						|
			}
 | 
						|
 | 
						|
			try {
 | 
						|
				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);
 | 
						|
			}
 | 
						|
		});
 | 
						|
 | 
						|
		post.form({
 | 
						|
			type: type,
 | 
						|
			image: image
 | 
						|
		});
 | 
						|
	};
 | 
						|
 | 
						|
}(exports)); |