Commit 083b8b1d authored by rockyl's avatar rockyl

改回pngquant

parent 1ab48452
/**
* Created by rockyl on 2020/11/30.
* Created by rockyl on 2020-02-13.
*/
/// <reference types="node" />
/**
......
"use strict";
/**
* Created by rockyl on 2020/11/30.
* Created by rockyl on 2020-02-13.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compress = void 0;
const FormData = require('form-data');
const fs = require('fs-extra');
const https = require('https');
const { URL } = require('url');
const options = {
method: 'POST',
hostname: 'tinypng.com',
path: '/web/shrink',
headers: {
rejectUnauthorized: false,
'Postman-Token': Date.now(),
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
};
const isPng = require("is-png");
const isJpg = require("is-jpg");
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
/**
* 压缩图片
* @param {Buffer|string} bufferOrFile
......@@ -29,47 +19,50 @@ async function compress(bufferOrFile) {
if (typeof bufferOrFile === 'string') {
buffer = await fs.readFile(bufferOrFile);
}
const result = await uploadImg(buffer);
return result;
}
exports.compress = compress;
function uploadImg(buffer) {
return new Promise((resolve, reject) => {
options.headers['X-Forwarded-For'] = Array.from(Array(4)).map(() => Math.floor(Math.random() * 255)).join('.');
var req = https.request(options, function (res) {
res.on('data', buf => {
let obj = JSON.parse(buf.toString());
if (obj.error) {
console.log(`[压缩失败:${obj.message}`);
if (buffer && buffer.length > 0) {
let extname = isPng(buffer) ? 'png' : 'jpeg';
let form = new FormData();
form.append('file', buffer, {
filename: 'image.' + extname,
contentType: 'image/' + extname,
});
form.submit(tinifyUrl, function (err, res) {
if (err) {
reject(err);
}
else {
resolve(downloadImg(obj));
res.resume();
let resBuffer = Buffer.alloc(0);
res.on('data', (d) => {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
});
res.on('end', () => {
if (resBuffer.length > 256 || isPng(resBuffer) || isJpg(resBuffer)) {
resolve(resBuffer);
}
else {
let str = resBuffer.toString();
let json;
try {
json = JSON.parse(str);
}
catch (e) {
console.log(e);
}
reject(json.msg);
}
});
res.on('abort', () => {
reject('abort');
});
}
});
});
req.write(buffer);
req.on('error', e => {
reject(e);
});
req.end();
});
}
function downloadImg(obj) {
return new Promise((resolve, reject) => {
let options = new URL(obj.output.url);
let req = https.request(options, res => {
let body = Buffer.alloc(0);
res.on('data', function (data) {
body = Buffer.concat([body, data], body.length + data.length);
});
res.on('end', function () {
resolve(body);
});
});
req.on('error', e => {
reject(e);
});
req.end();
}
else {
reject('empty buffer');
}
});
}
exports.compress = compress;
//# sourceMappingURL=index.js.map
\ No newline at end of file
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAC/B,MAAM,EAAC,GAAG,EAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;AAE7B,MAAM,OAAO,GAAG;IACf,MAAM,EAAE,MAAM;IACd,QAAQ,EAAE,aAAa;IACvB,IAAI,EAAE,aAAa;IACnB,OAAO,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,eAAe,EAAE,IAAI,CAAC,GAAG,EAAE;QAC3B,eAAe,EAAE,UAAU;QAC3B,cAAc,EAAE,mCAAmC;QACnD,YAAY,EACX,8GAA8G;KAC/G;CACD,CAAC;AAEF;;;;GAIG;AACI,KAAK,UAAU,QAAQ,CAAC,YAA6B;IAC3D,IAAI,MAAM,GAAG,YAAY,CAAC;IAC1B,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;QACrC,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;KACzC;IACD,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,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;4BACT,IAAI;gCACH,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;6BACvB;4BAAC,OAAO,CAAC,EAAE;gCACX,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;6BACf;4BACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;yBACjB;oBACF,CAAC,CAAC,CAAC;oBACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;wBACpB,MAAM,CAAC,OAAO,CAAC,CAAA;oBAChB,CAAC,CAAC,CAAC;iBACH;YACF,CAAC,CAAC,CAAC;SACH;aAAM;YACN,MAAM,CAAC,cAAc,CAAC,CAAA;SACtB;IACF,CAAC,CAAC,CAAA;AACH,CAAC;AA9CD,4BA8CC"}
\ No newline at end of file
{
"name": "tiny-image",
"version": "1.0.4",
"version": "1.0.6",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"fs-extra": "^8.1.0"
"form-data": "^4.0.0",
"fs-extra": "^8.1.0",
"is-jpg": "^2.0.0",
"is-png": "^2.0.0"
},
"scripts": {
"dev": "tsc -w",
......
/**
* Created by rockyl on 2020/11/30.
* Created by rockyl on 2020-02-13.
*/
const FormData = require('form-data');
const fs = require('fs-extra');
const https = require('https');
const {URL} = require('url');
const isPng = require("is-png");
const isJpg = require("is-jpg");
const options = {
method: 'POST',
hostname: 'tinypng.com',
path: '/web/shrink',
headers: {
rejectUnauthorized: false,
'Postman-Token': Date.now(),
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
};
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
/**
* 压缩图片
......@@ -30,49 +19,45 @@ export async function compress(bufferOrFile: Buffer | string) {
if (typeof bufferOrFile === 'string') {
buffer = await fs.readFile(bufferOrFile);
}
const result = await uploadImg(buffer);
return result;
}
function uploadImg(buffer) {
return new Promise((resolve, reject) => {
options.headers['X-Forwarded-For'] = Array.from(Array(4)).map(() => Math.floor(Math.random() * 255)).join('.')
if (buffer && buffer.length > 0) {
let extname = isPng(buffer as Buffer) ? 'png' : 'jpeg';
let form = new FormData();
form.append('file', buffer, {
filename: 'image.' + extname,
contentType: 'image/' + extname,
});
var req = https.request(options, function (res) {
res.on('data', buf => {
let obj = JSON.parse(buf.toString());
if (obj.error) {
console.log(`[压缩失败:${obj.message}`);
form.submit(tinifyUrl, function (err, res) {
if (err) {
reject(err);
} else {
resolve(downloadImg(obj));
res.resume();
let resBuffer = Buffer.alloc(0);
res.on('data', (d) => {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
});
res.on('end', () => {
if (resBuffer.length > 256 || isPng(resBuffer) || isJpg(resBuffer)) {
resolve(resBuffer);
} else {
let str = resBuffer.toString();
let json;
try {
json = JSON.parse(str);
} catch (e) {
console.log(e);
}
reject(json.msg);
}
});
res.on('abort', () => {
reject('abort')
});
}
});
});
req.write(buffer);
req.on('error', e => {
reject(e);
});
req.end();
})
}
function downloadImg(obj) {
return new Promise((resolve, reject) => {
let options = new URL(obj.output.url);
let req = https.request(options, res => {
let body = Buffer.alloc(0);
res.on('data', function (data) {
body = Buffer.concat([body, data], body.length + data.length);
});
res.on('end', function () {
resolve(body);
});
});
req.on('error', e => {
reject(e);
});
req.end();
} else {
reject('empty buffer')
}
})
}
......@@ -14,6 +14,32 @@
resolved "http://npm.dui88.com:80/@types%2fnode/-/node-14.14.5.tgz#e92d3b8f76583efa26c1a63a21c9d3c1143daa29"
integrity sha1-6S07j3ZYPvomwaY6IcnTwRQ9qik=
asynckit@^0.4.0:
version "0.4.0"
resolved "http://npm.dui88.com:80/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=
combined-stream@^1.0.8:
version "1.0.8"
resolved "http://npm.dui88.com:80/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha1-w9RaizT9cwYxoRCoolIGgrMdWn8=
dependencies:
delayed-stream "~1.0.0"
delayed-stream@~1.0.0:
version "1.0.0"
resolved "http://npm.dui88.com:80/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
form-data@^4.0.0:
version "4.0.0"
resolved "http://npm.dui88.com:80/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha1-k5Gdrq82HuUpWEubMWZNwSyfpFI=
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"
fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.npm.taobao.org/fs-extra/download/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
......@@ -28,6 +54,16 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0:
resolved "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha1-ShL/G2A3bvCYYsIJPt2Qgyi+hCM=
is-jpg@^2.0.0:
version "2.0.0"
resolved "http://npm.dui88.com:80/is-jpg/-/is-jpg-2.0.0.tgz#2e1997fa6e9166eaac0242daae443403e4ef1d97"
integrity sha1-LhmX+m6RZuqsAkLarkQ0A+TvHZc=
is-png@^2.0.0:
version "2.0.0"
resolved "http://npm.dui88.com:80/is-png/-/is-png-2.0.0.tgz#ee8cbc9e9b050425cedeeb4a6fb74a649b0a4a8d"
integrity sha1-7oy8npsFBCXO3utKb7dKZJsKSo0=
jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.npm.taobao.org/jsonfile/download/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
......@@ -35,6 +71,18 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"
mime-db@1.46.0:
version "1.46.0"
resolved "http://npm.dui88.com:80/mime-db/-/mime-db-1.46.0.tgz#6267748a7f799594de3cbc8cde91def349661cee"
integrity sha1-Ymd0in95lZTePLyM3pHe80lmHO4=
mime-types@^2.1.12:
version "2.1.29"
resolved "http://npm.dui88.com:80/mime-types/-/mime-types-2.1.29.tgz#1d4ab77da64b91f5f72489df29236563754bb1b2"
integrity sha1-HUq3faZLkfX3JInfKSNlY3VLsbI=
dependencies:
mime-db "1.46.0"
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.npm.taobao.org/universalify/download/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment