Commit d14f5027 authored by rockyl's avatar rockyl

使用tinypng开设tinify2接口

parent 65cecd20
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("./logger"); const logger_1 = require("./logger");
const imagemin = require("imagemin"); const imagemin = require("imagemin");
const imageminJpegtran = require("imagemin-jpegtran"); const imageminJpegtran = require("imagemin-mozjpeg");
const imageminPngquant = require("imagemin-pngquant2"); const imageminPngquant = require("imagemin-pngquant2");
const fs = require('fs-extra'); const node_tinify_1 = require("node-tinify");
const acceptTypes = ['image/png', 'image/jpeg']; const acceptTypes = ['image/png', 'image/jpeg'];
function applyApi(server) { function applyApi(server) {
server.get('/info', function (req, res, next) { server.get('/info', function (req, res, next) {
...@@ -18,12 +18,15 @@ function applyApi(server) { ...@@ -18,12 +18,15 @@ function applyApi(server) {
res.sendRaw('ok'); res.sendRaw('ok');
return next(); return next();
}); });
server.post('/tinify', async function (req, res, next) { server.post('/tinify', tinify);
server.post('/tinify2', tinify2);
}
exports.default = applyApi;
async function tinify(req, res, next) {
let file = req.files.file; let file = req.files.file;
if (file) { if (file) {
if (acceptTypes.includes(file.type)) { if (acceptTypes.includes(file.type)) {
try { try {
console.log(`start compress!`);
const buffer = await imagemin([file.path], { const buffer = await imagemin([file.path], {
plugins: [ plugins: [
imageminJpegtran(), imageminJpegtran(),
...@@ -32,11 +35,9 @@ function applyApi(server) { ...@@ -32,11 +35,9 @@ function applyApi(server) {
}) })
] ]
}); });
console.log(`compress success!`);
res.sendRaw(buffer[0].data, { res.sendRaw(buffer[0].data, {
'Content-Type': file.type, 'Content-Type': file.type,
}); });
console.log('send row success!');
} }
catch (e) { catch (e) {
logger_1.default.error('/tinify', 'compress failed', e.message); logger_1.default.error('/tinify', 'compress failed', e.message);
...@@ -61,7 +62,39 @@ function applyApi(server) { ...@@ -61,7 +62,39 @@ function applyApi(server) {
}); });
} }
return next(); return next();
}
async function tinify2(req, res, next) {
let file = req.files.file;
if (file) {
if (acceptTypes.includes(file.type)) {
try {
const buffer = await node_tinify_1.compress(file.path, { returnType: 'buffer' });
res.sendRaw(buffer, {
'Content-Type': file.type,
}); });
}
catch (e) {
logger_1.default.error('/tinify2', 'compress failed', e.message);
res.send({
code: 3,
msg: 'compress failed'
});
}
}
else {
logger_1.default.error('/tinify2', 'type error');
res.send({
code: 2,
msg: 'type error'
});
}
}
else {
res.send({
code: 1,
msg: 'file not exists'
});
}
return next();
} }
exports.default = applyApi;
//# sourceMappingURL=api.js.map //# sourceMappingURL=api.js.map
\ No newline at end of file
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,qCAA8B;AAC9B,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AACrC,MAAM,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AACtD,MAAM,gBAAgB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AACvD,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAE/B,MAAM,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;AAEhD,SAAwB,QAAQ,CAAC,MAAM;IACtC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI;QAC3C,GAAG,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC;QAC5C,OAAO,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,GAAG,CAAC,gBAAgB,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI;QACpD,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAClB,OAAO,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,WAAY,GAAG,EAAE,GAAG,EA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IAAI,CAAC,IAAI;iBACzB,CAAC,CAAA;aACF;YAAC,OAAO,CAAC,EAAE;gBACX,gBAAM,CAAC,KAAK,CAAC,UAAU,EAAE,iBAAiB,EAAE,CAAC,CAAC,OAAO,CAAC,CAAA;gBACtD,GAAG,CAAC,IAAI,CAAC;oBACR,IAAI,EAAE,CAAC;oBACP,GAAG,EAAE,iBAAiB;iBACtB,CAAC,CAAA;aACF;SACD;aAAM;YACN,gBAAM,CAAC,KAAK,CAAC,UAAU,EAAE,YAAY,CAAC,CAAA;YACtC,GAAG,CAAC,IAAI,CAAC;gBACR,IAAI,EAAE,CAAC;gBACP,GAAG,EAAE,YAAY;aACjB,CAAC,CAAA;SACF;KACD;SAAM;QACN,GAAG,CAAC,IAAI,CAAC;YACR,IAAI,EAAE,CAAC;YACP,GAAG,EAAE,iBAAiB;SACtB,CAAC,CAAA;KACF;IACD,OAAO,IAAI,EAAE,CAAA;AACd,CAAC"}
\ No newline at end of file \ No newline at end of file
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getEurekaClient = void 0;
const eureka_client_ts_1 = require("eureka-client-ts"); const eureka_client_ts_1 = require("eureka-client-ts");
const logger_1 = require("./logger"); const logger_1 = require("./logger");
const eurekaConfig = { const eurekaConfig = {
......
{"version":3,"file":"eureka.js","sourceRoot":"","sources":["../src/eureka.ts"],"names":[],"mappings":";;;AAAA,uDAA4C;AAC5C,qCAA8B;AAE9B,MAAM,YAAY,GAAO;IACvB,OAAO,EAAE,uBAAuB;IAChC,QAAQ,EAAE,CAAC,uBAAuB,CAAC;CACpC,CAAC;AAEK,KAAK,UAAU,eAAe,CAAC,IAAI;IACxC,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC;IACzB,IAAI,MAAM,CAAC;IACX,IAAI;QACF,MAAM,GAAG,IAAI,0BAAY,CAAC,YAAY,CAAC,CAAC;QACxC,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;KACtB;IAAC,OAAO,CAAC,EAAE;QACV,gBAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KACjB;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAXD,0CAWC"} {"version":3,"file":"eureka.js","sourceRoot":"","sources":["../src/eureka.ts"],"names":[],"mappings":";;AAAA,uDAA2C;AAC3C,qCAA6B;AAE7B,MAAM,YAAY,GAAO;IACvB,OAAO,EAAE,uBAAuB;IAChC,QAAQ,EAAE,CAAC,uBAAuB,CAAC;CACpC,CAAA;AAEM,KAAK,UAAU,eAAe,CAAC,IAAI;IACxC,YAAY,CAAC,IAAI,GAAG,IAAI,CAAA;IACxB,IAAI,MAAM,CAAA;IACV,IAAI;QACF,MAAM,GAAG,IAAI,0BAAY,CAAC,YAAY,CAAC,CAAA;QACvC,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;KACrB;IAAC,OAAO,CAAC,EAAE;QACV,gBAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;KAChB;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAXD,0CAWC"}
\ No newline at end of file \ No newline at end of file
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AACnC,qCAAyC;AACzC,qCAA8B;AAC9B,+BAA6B;AAE7B,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;AAC7C,IAAI,MAAM,CAAC;AACX,IAAI,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;AAE7B,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IACnC,IAAI,EAAE,YAAY;IAClB,OAAO,EAAE,OAAO;CAChB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAE3C,MAAM,CAAC,GAAG,CAA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AAC,CAAA;AAClB,CAAC,CAAC,CAAA;AAEF,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;IAC1B,MAAM,CAAC,IAAI,EAAE,CAAA;IACb,UAAU,CAAC,GAAG,EAAE;QACf,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAChB,CAAC,EAAE,IAAI,CAAC,CAAA;AACT,CAAC,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK;IACxB,IAAI;QACH,MAAM,GAAG,MAAM,wBAAe,CAAC,IAAI,CAAC,CAAA;QACpC,gBAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;KACtC;IAAC,OAAO,CAAC,EAAE;QACX,gBAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;KACpB;IACD,gBAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAA;AAC3D,CAAC,CAAC,CAAA"}
\ No newline at end of file \ No newline at end of file
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,mCAA4C;AAC5C,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAE/B,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAqB,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;AAEvG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAEtB,kBAAS,CAAC;IACT,SAAS,EAAE;QACV,GAAG,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;QACtB,MAAM,EAAE;YACP,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC;SACvC;KACD;IACD,UAAU,EAAE;QACX,OAAO,EAAE,EAAC,SAAS,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,KAAK,EAAE,OAAO,EAAC;KACvD;CAED,CAAC,CAAC;AAEH,MAAM,MAAM,GAAG,kBAAS,EAAE,CAAC;AAE3B,kBAAe,MAAM,CAAC"} {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,mCAA2C;AAC3C,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AAC5B,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;AAE9B,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAqB,EAAE,MAAM,EAAE,YAAY,CAAC,CAAA;AAEtG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;AAErB,kBAAS,CAAC;IACT,SAAS,EAAE;QACV,GAAG,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;QACtB,MAAM,EAAE;YACP,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC;SACvC;KACD;IACD,UAAU,EAAE;QACX,OAAO,EAAE,EAAC,SAAS,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,KAAK,EAAE,OAAO,EAAC;KACvD;CAED,CAAC,CAAA;AAEF,MAAM,MAAM,GAAG,kBAAS,EAAE,CAAA;AAE1B,kBAAe,MAAM,CAAA"}
\ No newline at end of file \ No newline at end of file
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"imagemin-jpegtran": "^6.0.0", "imagemin-jpegtran": "^6.0.0",
"imagemin-pngquant2": "^9.0.1", "imagemin-pngquant2": "^9.0.1",
"log4js": "^6.1.2", "log4js": "^6.1.2",
"node-tinify": "^1.0.4",
"restify": "^8.5.1" "restify": "^8.5.1"
}, },
"scripts": { "scripts": {
......
...@@ -2,32 +2,36 @@ ...@@ -2,32 +2,36 @@
* Created by rockyl on 2020-02-13. * Created by rockyl on 2020-02-13.
*/ */
import logger from "./logger"; import logger from "./logger"
const imagemin = require("imagemin");
const imageminJpegtran = require("imagemin-jpegtran");
const imageminPngquant = require("imagemin-pngquant2");
const fs = require('fs-extra');
const acceptTypes = ['image/png', 'image/jpeg']; const imagemin = require("imagemin")
const imageminJpegtran = require("imagemin-mozjpeg")
const imageminPngquant = require("imagemin-pngquant2")
import {compress} from 'node-tinify'
export default function applyApi(server){ const acceptTypes = ['image/png', 'image/jpeg']
export default function applyApi(server) {
server.get('/info', function (req, res, next) { server.get('/info', function (req, res, next) {
res.sendRaw('Tiny image serve is running.'); res.sendRaw('Tiny image serve is running.')
return next(); return next()
}); })
server.get('/monitor/check', function (req, res, next) { server.get('/monitor/check', function (req, res, next) {
res.sendRaw('ok'); res.sendRaw('ok')
return next(); return next()
}); })
server.post('/tinify', tinify)
server.post('/tinify2', tinify2)
}
server.post('/tinify', async function (req, res, next) { async function tinify(req, res, next) {
let file = req.files.file; let file = req.files.file
if(file){ if (file) {
if(acceptTypes.includes(file.type)){ if (acceptTypes.includes(file.type)) {
try { try {
console.log(`start compress!`);
const buffer = await imagemin([file.path], { const buffer = await imagemin([file.path], {
plugins: [ plugins: [
imageminJpegtran(), imageminJpegtran(),
...@@ -35,34 +39,63 @@ export default function applyApi(server){ ...@@ -35,34 +39,63 @@ export default function applyApi(server){
quality: [0.6, 0.8] quality: [0.6, 0.8]
}) })
] ]
}); })
console.log(`compress success!`);
res.sendRaw(buffer[0].data, { res.sendRaw(buffer[0].data, {
'Content-Type': file.type, 'Content-Type': file.type,
}); })
console.log('send row success!'); } catch (e) {
}catch (e) { logger.error('/tinify', 'compress failed', e.message)
logger.error('/tinify', 'compress failed', e.message);
res.send({ res.send({
code: 3, code: 3,
msg: 'compress failed' msg: 'compress failed'
}); })
} }
}else{ } else {
logger.error('/tinify', 'type error'); logger.error('/tinify', 'type error')
res.send({ res.send({
code: 2, code: 2,
msg: 'type error' msg: 'type error'
}); })
} }
}else{ } else {
res.send({ res.send({
code: 1, code: 1,
msg: 'file not exists' msg: 'file not exists'
}); })
} }
return next()
}
async function tinify2(req, res, next) {
let file = req.files.file
return next(); if (file) {
}); if (acceptTypes.includes(file.type)) {
try {
const buffer = await compress(file.path, {returnType: 'buffer'})
res.sendRaw(buffer, {
'Content-Type': file.type,
})
} catch (e) {
logger.error('/tinify2', 'compress failed', e.message)
res.send({
code: 3,
msg: 'compress failed'
})
}
} else {
logger.error('/tinify2', 'type error')
res.send({
code: 2,
msg: 'type error'
})
}
} else {
res.send({
code: 1,
msg: 'file not exists'
})
}
return next()
} }
import Eurekaclient from 'eureka-client-ts'; import Eurekaclient from 'eureka-client-ts'
import logger from './logger'; import logger from './logger'
const eurekaConfig:any = { const eurekaConfig:any = {
appName: 'duiba-tiny-image-node', appName: 'duiba-tiny-image-node',
services: ['duiba-tiny-image-node'], services: ['duiba-tiny-image-node'],
}; }
export async function getEurekaClient(port) { export async function getEurekaClient(port) {
eurekaConfig.port = port; eurekaConfig.port = port
let eureka; let eureka
try { try {
eureka = new Eurekaclient(eurekaConfig); eureka = new Eurekaclient(eurekaConfig)
await eureka.start(); await eureka.start()
} catch (e) { } catch (e) {
logger.error(e); logger.error(e)
} }
return eureka; return eureka
} }
...@@ -2,53 +2,53 @@ ...@@ -2,53 +2,53 @@
* Created by rockyl on 2020-02-12. * Created by rockyl on 2020-02-12.
*/ */
const restify = require('restify'); const restify = require('restify')
import {getEurekaClient} from "./eureka"; import {getEurekaClient} from "./eureka"
import logger from "./logger"; import logger from "./logger"
import applyApi from "./api"; import applyApi from "./api"
let [_, __, portStr = '8090'] = process.argv; let [_, __, portStr = '8090'] = process.argv
let eureka; let eureka
let port = parseInt(portStr); let port = parseInt(portStr)
const server = restify.createServer({ const server = restify.createServer({
name: 'tiny-image', name: 'tiny-image',
version: '1.0.0' version: '1.0.0'
}); })
const ignoreUrlLogger = ['/monitor/check']; const ignoreUrlLogger = ['/monitor/check']
server.use(restify.plugins.acceptParser(server.acceptable)); server.use(restify.plugins.acceptParser(server.acceptable))
server.use(restify.plugins.queryParser()); server.use(restify.plugins.queryParser())
server.use(restify.plugins.bodyParser()); server.use(restify.plugins.bodyParser())
server.use(function (req, res, next) { server.use(function (req, res, next) {
if (!ignoreUrlLogger.includes(req.url as string)) { if (!ignoreUrlLogger.includes(req.url as string)) {
logger.info(req.url); logger.info(req.url)
} }
return next(); return next()
}); })
applyApi(server); applyApi(server)
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
logger.error(err); logger.error(err)
}); })
process.on("SIGTERM", () => { process.on("SIGTERM", () => {
eureka.stop(); eureka.stop()
setTimeout(() => { setTimeout(() => {
process.exit(1) process.exit(1)
}, 6000); }, 6000)
}); })
server.listen(port, async function () { server.listen(port, async function () {
try { try {
eureka = await getEurekaClient(port); eureka = await getEurekaClient(port)
logger.info('eureka register success'); logger.info('eureka register success')
} catch (e) { } catch (e) {
logger.error(e.name); logger.error(e.name)
} }
logger.info('%s listening at %s', server.name, server.url); logger.info('%s listening at %s', server.name, server.url)
}); })
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
* Created by rockyl on 2020-02-13. * Created by rockyl on 2020-02-13.
*/ */
import {configure, getLogger} from 'log4js'; import {configure, getLogger} from 'log4js'
const path = require("path"); const path = require("path")
const fs = require("fs-extra"); const fs = require("fs-extra")
const logPath = path.join(process.env.HOME || process.env.USERPROFILE as string, 'logs', 'tiny-image'); const logPath = path.join(process.env.HOME || process.env.USERPROFILE as string, 'logs', 'tiny-image')
fs.ensureDir(logPath); fs.ensureDir(logPath)
configure({ configure({
appenders: { appenders: {
...@@ -22,8 +22,8 @@ configure({ ...@@ -22,8 +22,8 @@ configure({
default: {appenders: ['out', 'allLog'], level: 'debug'}, default: {appenders: ['out', 'allLog'], level: 'debug'},
} }
}); })
const logger = getLogger(); const logger = getLogger()
export default logger; export default logger
...@@ -1849,6 +1849,11 @@ nice-try@^1.0.4: ...@@ -1849,6 +1849,11 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
node-tinify@^1.0.4:
version "1.0.4"
resolved "http://npm.dui88.com:80/node-tinify/-/node-tinify-1.0.4.tgz#6f225449872a72ec49e3e1f24ec4abf212870135"
integrity sha1-byJUSYcqcuxJ4+HyTsSr8hKHATU=
normalize-package-data@^2.3.2, normalize-package-data@^2.3.4: normalize-package-data@^2.3.2, normalize-package-data@^2.3.4:
version "2.5.0" version "2.5.0"
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"
......
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