cky-image-public
Version:
public image to public host
123 lines (109 loc) • 3.73 kB
JavaScript
const fs = require("fs");
const cheerio = require('cheerio');
const async = require('async');
const request = require('request').defaults({
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
Origin: 'https://imgbox.com',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,la;q=0.5',
Accept: 'application/json, text/javascript, */*; q=0.01',
Referer: 'https://imgbox.com/',
'X-Requested-With': 'XMLHttpRequest',
Connection: 'keep-alive',
DNT: 1
},
jar: true
});
let helper = require('../helper');
class Imgbox {
constructor (options = {}) {
let { api_key } = options;
this.api_key = api_key;
this.cookie = request.jar();
}
upload (imgPath, callback) {
this.generateToken((err, token) => {
if (err) return callback(err, token);
// {"ok":true,"token_id":467486223,"token_secret":"tLs7JnJ2xnlrwtMn","gallery_id":"q6vdFviLRY","gallery_secret":"f8GB0S9t1PFVWMO4"}
let { token_id, token_secret, gallery_id, gallery_secret, csrf_token } = token;
request({
url: 'https://imgbox.com/upload/process',
method: 'POST',
headers: {
'X-CSRF-Token': csrf_token,
'Content-Type': 'multipart/form-data'
},
jar: this.cookie,
formData: {
token_id,
token_secret,
content_type: 1,
thumbnail_size: '100c',
gallery_id,
gallery_secret,
comments_enabled: 0,
'files[]': {
value: fs.createReadStream(imgPath),
options: {
filename: imgPath,
contentType: 'image/jpeg'
}
},
}
}, (err, response, body) => {
if (err) return callback('EUPLOAD', err);
body = helper.safeParse(body);
return callback(null, body);
});
});
}
generateToken (callback) {
async.waterfall([
(next) => {
request({
url: 'https://imgbox.com/',
method: 'GET',
headers: {
'Upgrade-Insecure-Requests': 1,
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
},
jar: this.cookie
}, (err, response, body) => {
if (err) return callback('EGETHOMEPAGE', err);
let $ = cheerio.load(body);
let csrf_token = $('[name="csrf-token"]').attr('content');
if (!csrf_token || csrf_token.length == 0) return next('EGETCSRFTOKEN');
return next(null, csrf_token);
})
},
(csrf_token, next) => {
console.log('csrf_token=', csrf_token);
request({
url: 'https://imgbox.com/ajax/token/generate',
method: 'POST',
headers: {
'X-CSRF-Token': csrf_token,
'Content-Type': 'application/x-www-form-urlencoded'
},
jar: this.cookie,
form: {
gallery: true,
gallery_title: '',
comments_enabled: 1
}
}, (err, response, body) => {
if (err) return next('EGENERATETOKEN', err);
body = helper.safeParse(body);
if (!body) return next('EGENERATETOKEN_PARSEBODY');
body['csrf_token'] = csrf_token;
/*
{"ok":true,"token_id":467486223,"token_secret":"tLs7JnJ2xnlrwtMn","gallery_id":"q6vdFviLRY","gallery_secret":"f8GB0S9t1PFVWMO4"}
*/
return next(null, body);
});
}
], callback);
}
}
module.exports = Imgbox;