Commit 050f5ae3 authored by rockyl's avatar rockyl

修改错误返回判断阈值为512

parent 6ff61a5f
...@@ -32,7 +32,7 @@ async function compressBuffer(buffer) { ...@@ -32,7 +32,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 (resBuffer.length > 50) { if (resBuffer.length > 512) {
resolve(resBuffer); resolve(resBuffer);
} else { } else {
let str = resBuffer.toString(); let str = resBuffer.toString();
......
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport FormData from 'form-data';\nimport fs from 'fs-extra';\n\nconst tinifyUrl = 'http://tinify.duiba.com.cn/tinify';\n\nexport async function compressBuffer(buffer) {\n\treturn new Promise((resolve, reject) => {\n\t\tif (buffer && buffer.length > 0) {\n\t\t\tlet form = new FormData();\n\t\t\tform.append('file', buffer, {\n\t\t\t\tfilename: 'image.png',\n\t\t\t\tcontentType: 'image/png',\n\t\t\t});\n\n\t\t\tform.submit(tinifyUrl, function (err, res) {\n\t\t\t\tif (err) {\n\t\t\t\t\treject(err);\n\t\t\t\t} else {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tlet resBuffer = Buffer.alloc(0);\n\t\t\t\t\tres.on('data', (d) => {\n\t\t\t\t\t\tresBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);\n\t\t\t\t\t});\n\t\t\t\t\tres.on('end', () => {\n\t\t\t\t\t\tif (resBuffer.length > 50) {\n\t\t\t\t\t\t\tresolve(resBuffer);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet str = resBuffer.toString();\n\t\t\t\t\t\t\tlet json = JSON.parse(str);\n\t\t\t\t\t\t\treject(json.msg);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tres.on('abort', () => {\n\t\t\t\t\t\treject('abort')\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\treject('empty buffer')\n\t\t}\n\t})\n}\n\nexport async function compressFile(file) {\n\tlet buffer = await fs.readFile(file);\n\treturn compressBuffer(buffer);\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;;;AAGA,AAGA;AACA,MAAM,SAAS,GAAG,mCAAmC,CAAC;;AAEtD,AAAO,eAAe,cAAc,CAAC,MAAM,EAAE;CAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;EACvC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;GAChC,IAAI,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;GAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;IAC3B,QAAQ,EAAE,WAAW;IACrB,WAAW,EAAE,WAAW;IACxB,CAAC,CAAC;;GAEH,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;IAC1C,IAAI,GAAG,EAAE;KACR,MAAM,CAAC,GAAG,CAAC,CAAC;KACZ,MAAM;KACN,GAAG,CAAC,MAAM,EAAE,CAAC;KACb,IAAI,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK;MACrB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;MACvE,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM;MACnB,IAAI,SAAS,CAAC,MAAM,GAAG,EAAE,EAAE;OAC1B,OAAO,CAAC,SAAS,CAAC,CAAC;OACnB,MAAM;OACN,IAAI,GAAG,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;OAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;OAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;OACjB;MACD,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM;MACrB,MAAM,CAAC,OAAO,EAAC;MACf,CAAC,CAAC;KACH;IACD,CAAC,CAAC;GACH,MAAM;GACN,MAAM,CAAC,cAAc,EAAC;GACtB;EACD,CAAC;CACF;;AAED,AAAO,eAAe,YAAY,CAAC,IAAI,EAAE;CACxC,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;CACrC,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;CAC9B;;;;;"} {"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport FormData from 'form-data';\nimport fs from 'fs-extra';\n\nconst tinifyUrl = 'http://tinify.duiba.com.cn/tinify';\n\nexport async function compressBuffer(buffer) {\n\treturn new Promise((resolve, reject) => {\n\t\tif (buffer && buffer.length > 0) {\n\t\t\tlet form = new FormData();\n\t\t\tform.append('file', buffer, {\n\t\t\t\tfilename: 'image.png',\n\t\t\t\tcontentType: 'image/png',\n\t\t\t});\n\n\t\t\tform.submit(tinifyUrl, function (err, res) {\n\t\t\t\tif (err) {\n\t\t\t\t\treject(err);\n\t\t\t\t} else {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tlet resBuffer = Buffer.alloc(0);\n\t\t\t\t\tres.on('data', (d) => {\n\t\t\t\t\t\tresBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);\n\t\t\t\t\t});\n\t\t\t\t\tres.on('end', () => {\n\t\t\t\t\t\tif (resBuffer.length > 512) {\n\t\t\t\t\t\t\tresolve(resBuffer);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet str = resBuffer.toString();\n\t\t\t\t\t\t\tlet json = JSON.parse(str);\n\t\t\t\t\t\t\treject(json.msg);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tres.on('abort', () => {\n\t\t\t\t\t\treject('abort')\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\treject('empty buffer')\n\t\t}\n\t})\n}\n\nexport async function compressFile(file) {\n\tlet buffer = await fs.readFile(file);\n\treturn compressBuffer(buffer);\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;;;AAGA,AAGA;AACA,MAAM,SAAS,GAAG,mCAAmC,CAAC;;AAEtD,AAAO,eAAe,cAAc,CAAC,MAAM,EAAE;CAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;EACvC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;GAChC,IAAI,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;GAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;IAC3B,QAAQ,EAAE,WAAW;IACrB,WAAW,EAAE,WAAW;IACxB,CAAC,CAAC;;GAEH,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;IAC1C,IAAI,GAAG,EAAE;KACR,MAAM,CAAC,GAAG,CAAC,CAAC;KACZ,MAAM;KACN,GAAG,CAAC,MAAM,EAAE,CAAC;KACb,IAAI,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK;MACrB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;MACvE,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM;MACnB,IAAI,SAAS,CAAC,MAAM,GAAG,GAAG,EAAE;OAC3B,OAAO,CAAC,SAAS,CAAC,CAAC;OACnB,MAAM;OACN,IAAI,GAAG,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;OAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;OAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;OACjB;MACD,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM;MACrB,MAAM,CAAC,OAAO,EAAC;MACf,CAAC,CAAC;KACH;IACD,CAAC,CAAC;GACH,MAAM;GACN,MAAM,CAAC,cAAc,EAAC;GACtB;EACD,CAAC;CACF;;AAED,AAAO,eAAe,YAAY,CAAC,IAAI,EAAE;CACxC,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;CACrC,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;CAC9B;;;;;"}
\ No newline at end of file \ No newline at end of file
...@@ -26,7 +26,7 @@ export async function compressBuffer(buffer) { ...@@ -26,7 +26,7 @@ export 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 (resBuffer.length > 50) { if (resBuffer.length > 512) {
resolve(resBuffer); resolve(resBuffer);
} else { } else {
let str = resBuffer.toString(); let str = resBuffer.toString();
......
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