Commit 06b7c1b3 authored by rockyl's avatar rockyl

rewrite by ts

parent d604203b
...@@ -4,16 +4,16 @@ ...@@ -4,16 +4,16 @@
*/ */
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.compressFile = exports.compressBuffer = void 0; exports.compressFile = exports.compressBuffer = void 0;
const form_data_1 = require("form-data"); const FormData = require('form-data');
const fs_extra_1 = require("fs-extra"); const fs = require('fs-extra');
const is_png_1 = require("is-png"); const isPng = require("is-png");
const is_jpg_1 = require("is-jpg"); const isJpg = require("is-jpg");
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify'; const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
async function compressBuffer(buffer) { async function compressBuffer(buffer) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (buffer && buffer.length > 0) { if (buffer && buffer.length > 0) {
let extname = is_png_1.default(buffer) ? 'png' : 'jpg'; let extname = isPng(buffer) ? 'png' : 'jpeg';
let form = new form_data_1.default(); let form = new FormData();
form.append('file', buffer, { form.append('file', buffer, {
filename: 'image.' + extname, filename: 'image.' + extname,
contentType: 'image/' + extname, contentType: 'image/' + extname,
...@@ -29,7 +29,7 @@ async function compressBuffer(buffer) { ...@@ -29,7 +29,7 @@ async function compressBuffer(buffer) {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length); resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
}); });
res.on('end', () => { res.on('end', () => {
if (is_png_1.default(resBuffer) || is_jpg_1.default(resBuffer)) { if (isPng(resBuffer) || isJpg(resBuffer)) {
resolve(resBuffer); resolve(resBuffer);
} }
else { else {
...@@ -51,7 +51,7 @@ async function compressBuffer(buffer) { ...@@ -51,7 +51,7 @@ async function compressBuffer(buffer) {
} }
exports.compressBuffer = compressBuffer; exports.compressBuffer = compressBuffer;
async function compressFile(file) { async function compressFile(file) {
let buffer = await fs_extra_1.default.readFile(file); let buffer = await fs.readFile(file);
return compressBuffer(buffer); return compressBuffer(buffer);
} }
exports.compressFile = compressFile; exports.compressFile = compressFile;
......
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,yCAAiC;AACjC,uCAA0B;AAC1B,mCAA2B;AAC3B,mCAA2B;AAE3B,MAAM,SAAS,GAAG,mCAAmC,CAAC;AAE/C,KAAK,UAAU,cAAc,CAAC,MAAM;IAC1C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YAChC,IAAI,OAAO,GAAG,gBAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;YAC5C,IAAI,IAAI,GAAG,IAAI,mBAAQ,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;gBAC3B,QAAQ,EAAE,QAAQ,GAAG,OAAO;gBAC5B,WAAW,EAAE,QAAQ,GAAG,OAAO;aAC/B,CAAC,CAAC;YAEH,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,GAAG,EAAE,GAAG;gBACxC,IAAI,GAAG,EAAE;oBACR,MAAM,CAAC,GAAG,CAAC,CAAC;iBACZ;qBAAM;oBACN,GAAG,CAAC,MAAM,EAAE,CAAC;oBACb,IAAI,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;oBAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE;wBACpB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;oBACxE,CAAC,CAAC,CAAC;oBACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;wBACl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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;AArCD,wCAqCC;AAEM,KAAK,UAAU,YAAY,CAAC,IAAI;IACtC,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACrC,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC;AAHD,oCAGC"}
\ No newline at end of file \ No newline at end of file
...@@ -2,17 +2,17 @@ ...@@ -2,17 +2,17 @@
* Created by rockyl on 2020-02-13. * Created by rockyl on 2020-02-13.
*/ */
import FormData from 'form-data'; const FormData = require('form-data');
import fs from 'fs-extra'; const fs = require('fs-extra');
import isPng from "is-png"; const isPng = require("is-png");
import isJpg from "is-jpg"; const isJpg = require("is-jpg");
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify'; const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
export async function compressBuffer(buffer) { export async function compressBuffer(buffer) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (buffer && buffer.length > 0) { if (buffer && buffer.length > 0) {
let extname = isPng(buffer) ? 'png' : 'jpg'; let extname = isPng(buffer) ? 'png' : 'jpeg';
let form = new FormData(); let form = new FormData();
form.append('file', buffer, { form.append('file', buffer, {
filename: 'image.' + extname, filename: 'image.' + extname,
......
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