UNPKG

cky-image-public

Version:

public image to public host

43 lines (36 loc) 1.05 kB
const fs = require("fs"); const request = require('request'); let helper = require('../helper'); class Imageshack { constructor (options = {}) { let { api_key } = options; this.api_key = api_key; } upload (imgPath, callback) { if (!this.api_key) return callback('ENOAPIKEY', 'Invalid Api Key'); request({ url: 'https://post.imageshack.us/upload_api.php', method: 'POST', headers: { 'Content-type': 'application/x-www-form-urlencoded' }, formData: { key: this.api_key, format: 'json', fileupload: { value: fs.createReadStream(imgPath), options: { filename: imgPath, contentType: 'image/jpeg' } }, } }, (err, response, body) => { if (err || (response && response.statusCode != 200)) return callback('EUPLOAD', err); body = helper.safeParse(body); if (!body) return callback('EPARSEBODY', body); return callback(null, body); }); } } module.exports = Imageshack;