Commit 00d6acdd authored by rockyl's avatar rockyl

修改

parent d4d8b524
{
"PORT": 8090,
"remote_config_url": "http://configserver.dui88.com/duiba-tiny-image-node/dev?accessKey=b13f806fda2248e2aeff3e101b015876"
}
{
"PORT": 8090,
"remote_config_url": "http://configserver.dui88.com/duiba-tiny-image-node/dev?accessKey=b13f806fda2248e2aeff3e101b015876"
}
{
"PORT": 8090,
"remote_config_url": "http://configserver.duiba.com.cn/duiba-spark-node/prod?accessKey=264940a03c8c44cf8cd84c52a8e4f435"
}
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
*/ */
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 compress_1 = require("./compress");
const imageminJpegtran = require("imagemin-jpegtran"); const ali_oss_duiba_1 = require("ali-oss-duiba");
const imageminPngquant = require("imagemin-pngquant2");
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) {
...@@ -19,62 +17,30 @@ function applyApi(server) { ...@@ -19,62 +17,30 @@ function applyApi(server) {
return next(); return next();
}); });
server.post('/tinify', tinify); server.post('/tinify', tinify);
server.post('/tinify2', tinify2);
} }
exports.default = applyApi; exports.default = applyApi;
async function tinify(req, res, next) { async function tinify(req, res, next) {
let file = req.files.file; let file = req.files.file;
const { type = 'builtin', cdnPath } = req.query;
if (file) { if (file) {
if (acceptTypes.includes(file.type)) { if (acceptTypes.includes(file.type)) {
try { try {
const buffer = await imagemin([file.path], { const buffer = await (0, compress_1.compressImage)(file.path, type);
plugins: [ if (cdnPath) {
imageminJpegtran(), const name = await (0, ali_oss_duiba_1.uploadFile)(buffer, cdnPath);
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
});
}
catch (e) {
logger_1.default.error('/tinify', 'compress failed', e.message);
res.send({ res.send({
code: 3, code: 0,
msg: 'compress failed' data: name
}); });
} }
}
else { else {
logger_1.default.error('/tinify', 'type error');
res.send({
code: 2,
msg: 'type error'
});
}
}
else {
res.send({
code: 1,
msg: 'file not exists'
});
}
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, { res.sendRaw(buffer, {
'Content-Type': file.type, 'Content-Type': file.type,
}); });
} }
}
catch (e) { catch (e) {
logger_1.default.error('/tinify2', 'compress failed', e.message); logger_1.default.error('/tinify', 'compress failed', e.message);
res.send({ res.send({
code: 3, code: 3,
msg: 'compress failed' msg: 'compress failed'
...@@ -82,7 +48,7 @@ async function tinify2(req, res, next) { ...@@ -82,7 +48,7 @@ async function tinify2(req, res, next) {
} }
} }
else { else {
logger_1.default.error('/tinify2', 'type error'); logger_1.default.error('/tinify', 'type error');
res.send({ res.send({
code: 2, code: 2,
msg: 'type error' msg: 'type error'
......
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,qCAA6B;AAE7B,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;AACpC,MAAM,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAA;AACrD,MAAM,gBAAgB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAA;AACtD,6CAAoC;AAEpC,MAAM,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;AAE/C,SAAwB,QAAQ,CAAC,MAAM;IACtC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI;QAC3C,GAAG,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAA;QAC3C,OAAO,IAAI,EAAE,CAAA;IACd,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AAI;qBACV,CAAC,CAAA;iBACF;qBAAM;oBACN,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE;wBACnB,cAAc,EAAE,IAAI,CAAC,IAAI;qBACzB,CAAC,CAAA;iBACF;aACD;YAAC,OAAO,CAAC,EAAE;gBACX,gBAAM,CAAC,KAAK,CAAC,SAAS,EAAE,iBAAiB,EAAE,CAAC,CAAC,OAAO,CAAC,CAAA;gBACrD,GAAG,CAAC,IAAI,CAAC;oBACR,IAAI,EAAE,CAAC;oBACP,GAAG,EAAE,iBAAiB;iBACtB,CAAC,CAAA;aACF;SACD;aAAM;YACN,gBAAM,CAAC,KAAK,CAAC,SAAS,EAAE,YAAY,CAAC,CAAA;YACrC,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";
/**
* Created by rockyl on 2021/11/5.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressImage = void 0;
const imagemin = require("imagemin");
const imageminJpegtran = require("imagemin-jpegtran-cn");
const imageminPngquant = require("imagemin-pngquant-cn");
const node_tinify_1 = require("node-tinify");
function compressImage(filePath, type) {
switch (type) {
case 'builtin':
return imagemin([filePath], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
}).then(buffer => buffer[0].data);
case 'tinypng':
return (0, node_tinify_1.compress)(filePath, { returnType: 'buffer' });
}
}
exports.compressImage = compressImage;
//# sourceMappingURL=compress.js.map
\ No newline at end of file
{"version":3,"file":"compress.js","sourceRoot":"","sources":["../src/compress.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;AACpC,MAAM,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAA;AACxD,MAAM,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAA;AAExD,6CAAoC;AAEpC,SAAgB,aAAa,CAAC,QAAQ,EAAE,IAAI;IAC3C,QAAQ,IAAI,EAAE;QACb,KAAK,SAAS;YACb,OAAO,QAAQ,CAAC,CAAC,QAAQ,CAAC,EAAE;gBAC3B,OAAO,EAAE;oBACR,gBAAgB,EAAE;oBAClB,gBAAgB,CAAC;wBAChB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;qBACnB,CAAC;iBACF;aACD,CAAC,CAAC,IAAI,CAAC,MAAM,CAAA,EAAE,CAAA,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;QAChC,KAAK,SAAS;YACb,OAAO,IAAA,sBAAQ,EAAC,QAAQ,EAAE,EAAC,UAAU,EAAE,QAAQ,EAAC,CAAC,CAAA;KAClD;AACF,CAAC;AAdD,sCAcC"}
\ 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,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"} {"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
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** /**
* Created by rockyl on 2020-02-12. * Created by rockyl on 2020-02-12.
*/ */
Object.defineProperty(exports, "__esModule", { value: true }); const remote_configs_1 = require("./remote-configs");
const restify = require('restify'); const restify = require('restify');
const eureka_1 = require("./eureka"); const eureka_1 = require("./eureka");
const logger_1 = require("./logger"); const logger_1 = require("./logger");
const config = require("config");
const api_1 = require("./api"); const api_1 = require("./api");
let [_, __, portStr = '8090'] = process.argv; const ali_oss_duiba_1 = require("ali-oss-duiba");
let eureka; let eureka;
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'
...@@ -24,7 +25,7 @@ server.use(function (req, res, next) { ...@@ -24,7 +25,7 @@ server.use(function (req, res, next) {
} }
return next(); return next();
}); });
api_1.default(server); (0, api_1.default)(server);
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
logger_1.default.error(err); logger_1.default.error(err);
}); });
...@@ -34,14 +35,32 @@ process.on("SIGTERM", () => { ...@@ -34,14 +35,32 @@ process.on("SIGTERM", () => {
process.exit(1); process.exit(1);
}, 6000); }, 6000);
}); });
server.listen(port, async function () { async function start() {
await (0, remote_configs_1.prepare)();
const ossConfigs = (0, remote_configs_1.getConfigs)({ name: `apollo.oss.duiba.accessId`, alias: 'accessId' }, { name: `apollo.oss.duiba.accessKey`, alias: 'accessKey' });
logger_1.default.info('got oss configs:', ossConfigs);
(0, ali_oss_duiba_1.setup)({
accessKeyId: ossConfigs.accessId,
accessKeySecret: ossConfigs.accessKey,
bucket: 'duiba',
region: 'oss-cn-hangzhou',
});
const port = config.get('PORT');
server.listen(port, async function () {
if (process.env.NODE_ENV) {
try { try {
eureka = await eureka_1.getEurekaClient(port); eureka = await (0, eureka_1.getEurekaClient)(port);
logger_1.default.info('eureka register success'); logger_1.default.info('eureka register success');
} }
catch (e) { catch (e) {
logger_1.default.error(e.name); logger_1.default.error(e.name);
} }
}
logger_1.default.info('%s listening at %s', server.name, server.url); logger_1.default.info('%s listening at %s', server.name, server.url);
});
}
start().catch(e => {
console.log(e);
process.exit(2);
}); });
//# sourceMappingURL=index.js.map //# 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,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;AAClC,qCAAwC;AACxC,qCAA6B;AAC7B,+BAA4B;AAE5B,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAA;AAC5C,IAAI,MAAM,CAAA;AACV,IAAI,IAAI,G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C,CAAA;aACpB;SACD;QACD,gBAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAA;IAC3D,CAAC,CAAC,CAAA;AACH,CAAC;AAED,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IACjB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;IACd,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AAChB,CAAC,CAAC,CAAA"}
\ No newline at end of file \ No newline at end of file
...@@ -8,7 +8,7 @@ const path = require("path"); ...@@ -8,7 +8,7 @@ const path = require("path");
const fs = require("fs-extra"); const fs = require("fs-extra");
const logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image'); const logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');
fs.ensureDir(logPath); fs.ensureDir(logPath);
log4js_1.configure({ (0, log4js_1.configure)({
appenders: { appenders: {
out: { type: 'console' }, out: { type: 'console' },
allLog: { allLog: {
...@@ -20,6 +20,6 @@ log4js_1.configure({ ...@@ -20,6 +20,6 @@ log4js_1.configure({
default: { appenders: ['out', 'allLog'], level: 'debug' }, default: { appenders: ['out', 'allLog'], level: 'debug' },
} }
}); });
const logger = log4js_1.getLogger(); const logger = (0, log4js_1.getLogger)();
exports.default = logger; exports.default = logger;
//# sourceMappingURL=logger.js.map //# sourceMappingURL=logger.js.map
\ No newline at end of file
{"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,WAAq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AC,CAAA;AAEF,MAAM,MAAM,GAAG,IAAA,kBAAS,GAAE,CAAA;AAE1B,kBAAe,MAAM,CAAA"}
\ No newline at end of file \ No newline at end of file
"use strict";
/**
* Created by rockyl on 2020/8/18.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfigs = exports.prepare = exports.remoteConfigs = void 0;
const node_fetch_1 = require("node-fetch");
const config = require("config");
exports.remoteConfigs = {};
async function prepare() {
const remoteConfigUrl = config.get('remote_config_url');
const resp = await (0, node_fetch_1.default)(remoteConfigUrl);
const originConfigs = await resp.json();
//logger.info('originConfigs:', originConfigs)
exports.remoteConfigs = originConfigs.propertySources.reduce((prev, item) => {
return Object.assign(prev, item.source);
}, {});
}
exports.prepare = prepare;
function getConfigs(name, ...args) {
let configs = {};
args.unshift(name);
for (let key of args) {
let name = key;
let mapKey = key;
if (typeof key === 'object') {
name = key.alias;
mapKey = key.name;
}
configs[name] = exports.remoteConfigs[mapKey];
}
return configs;
}
exports.getConfigs = getConfigs;
//# sourceMappingURL=remote-configs.js.map
\ No newline at end of file
{"version":3,"file":"remote-configs.js","sourceRoot":"","sources":["../src/remote-configs.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,2CAA8B;AAC9B,iCAAgC;AAErB,QAAA,aAAa,GAAG,EAAE,CAAA;AAEtB,KAAK,UAAU,OAAO;IAC5B,MAAM,eAAe,GAAU,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;IAC9D,MAAM,IAAI,GAAG,MAAM,IAAA,oBAAK,EAAC,eAAe,CAAC,CAAA;IACzC,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;IACvC,8CAA8C;IAE9C,qBAAa,GAAG,aAAa,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE;QACnE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IACxC,CAAC,EAAE,EAAE,CAAC,CAAA;AACP,CAAC;AATD,0BASC;AAED,SAAgB,UAAU,CAAC,IAAI,EAAE,GAAG,IAAI;IACvC,IAAI,OAAO,GAAG,EAAE,CAAA;IAChB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAClB,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE;QACrB,IAAI,IAAI,GAAG,GAAG,CAAA;QACd,IAAI,MAAM,GAAG,GAAG,CAAA;QAChB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC5B,IAAI,GAAG,GAAG,CAAC,KAAK,CAAA;YAChB,MAAM,GAAG,GAAG,CAAC,IAAI,CAAA;SACjB;QACD,OAAO,CAAC,IAAI,CAAC,GAAG,qBAAa,CAAC,MAAM,CAAC,CAAA;KACrC;IACD,OAAO,OAAO,CAAA;AACf,CAAC;AAbD,gCAaC"}
\ No newline at end of file
### 查询项目
POST http://localhost:8090/tinify?type=tinypng&cdnPath=spark/assets
Accept: */*
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary--
Content-Disposition: form-data; name="file"; filename="Github.png"
Content-Type: image/png
< /Users/rockyl/Pictures/1643017.jpg
--WebAppBoundary--
...@@ -4,12 +4,18 @@ ...@@ -4,12 +4,18 @@
"main": "dist/index.js", "main": "dist/index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@tuia/apollo": "^0.1.0",
"@types/config": "^0.0.39",
"@types/node-fetch": "2",
"ali-oss-duiba": "^1.0.12",
"config": "^3.3.6",
"eureka-client-ts": "^0.2.10", "eureka-client-ts": "^0.2.10",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"imagemin": "^7.0.1", "imagemin": "^7.0.1",
"imagemin-jpegtran": "^6.0.0", "imagemin-jpegtran-cn": "^7.0.2",
"imagemin-pngquant2": "^9.0.1", "imagemin-pngquant-cn": "^9.0.1",
"log4js": "^6.1.2", "log4js": "^6.1.2",
"node-fetch": "2",
"node-tinify": "^1.0.4", "node-tinify": "^1.0.4",
"restify": "^8.5.1" "restify": "^8.5.1"
}, },
......
...@@ -3,11 +3,8 @@ ...@@ -3,11 +3,8 @@
*/ */
import logger from "./logger" import logger from "./logger"
import {compressImage} from "./compress"
const imagemin = require("imagemin") import {uploadFile} from 'ali-oss-duiba'
const imageminJpegtran = require("imagemin-jpegtran")
const imageminPngquant = require("imagemin-pngquant2")
import {compress} from 'node-tinify'
const acceptTypes = ['image/png', 'image/jpeg'] const acceptTypes = ['image/png', 'image/jpeg']
...@@ -23,69 +20,36 @@ export default function applyApi(server) { ...@@ -23,69 +20,36 @@ export default function applyApi(server) {
}) })
server.post('/tinify', tinify) server.post('/tinify', tinify)
server.post('/tinify2', tinify2)
} }
async function tinify(req, res, next) { async function tinify(req, res, next) {
let file = req.files.file let file = req.files.file
const {type = 'builtin', cdnPath} = req.query
if (file) { if (file) {
if (acceptTypes.includes(file.type)) { if (acceptTypes.includes(file.type)) {
try { try {
const buffer = await imagemin([file.path], { const buffer = await compressImage(file.path, type)
plugins: [ if (cdnPath) {
imageminJpegtran(), const name = await uploadFile(buffer, cdnPath)
imageminPngquant({
quality: [0.6, 0.8]
})
]
})
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
})
} catch (e) {
logger.error('/tinify', 'compress failed', e.message)
res.send({ res.send({
code: 3, code: 0,
msg: 'compress failed' data: name
})
}
} else {
logger.error('/tinify', 'type error')
res.send({
code: 2,
msg: 'type error'
}) })
}
} else { } else {
res.send({
code: 1,
msg: 'file not exists'
})
}
return next()
}
async function tinify2(req, res, next) {
let file = req.files.file
if (file) {
if (acceptTypes.includes(file.type)) {
try {
const buffer = await compress(file.path, {returnType: 'buffer'})
res.sendRaw(buffer, { res.sendRaw(buffer, {
'Content-Type': file.type, 'Content-Type': file.type,
}) })
}
} catch (e) { } catch (e) {
logger.error('/tinify2', '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('/tinify2', 'type error') logger.error('/tinify', 'type error')
res.send({ res.send({
code: 2, code: 2,
msg: 'type error' msg: 'type error'
......
/**
* Created by rockyl on 2021/11/5.
*/
const imagemin = require("imagemin")
const imageminJpegtran = require("imagemin-jpegtran-cn")
const imageminPngquant = require("imagemin-pngquant-cn")
import {compress} from 'node-tinify'
export function compressImage(filePath, type) {
switch (type) {
case 'builtin':
return imagemin([filePath], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
}).then(buffer=>buffer[0].data)
case 'tinypng':
return compress(filePath, {returnType: 'buffer'})
}
}
\ No newline at end of file
/** /**
* Created by rockyl on 2020-02-12. * Created by rockyl on 2020-02-12.
*/ */
import {getConfigs, prepare as prepareRemoteConfigs} from "./remote-configs";
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 * as config from "config"
import applyApi from "./api" import applyApi from "./api"
import {setup as setupOSS} from 'ali-oss-duiba'
import {} from '@tuia/apollo'
let [_, __, portStr = '8090'] = process.argv
let eureka let eureka
let port = parseInt(portStr)
const server = restify.createServer({ const server = restify.createServer({
name: 'tiny-image', name: 'tiny-image',
...@@ -41,14 +43,37 @@ process.on("SIGTERM", () => { ...@@ -41,14 +43,37 @@ process.on("SIGTERM", () => {
}, 6000) }, 6000)
}) })
server.listen(port, async function () { async function start() {
await prepareRemoteConfigs()
const ossConfigs = getConfigs(
{name: `apollo.oss.duiba.accessId`, alias: 'accessId'},
{name: `apollo.oss.duiba.accessKey`, alias: 'accessKey'}, //todo 解密
)
logger.info('got oss configs:', ossConfigs)
setupOSS({
accessKeyId: ossConfigs.accessId,
accessKeySecret: ossConfigs.accessKey,
bucket: 'duiba',
region: 'oss-cn-hangzhou',
})
const port = config.get('PORT')
server.listen(port, async function () {
if (process.env.NODE_ENV) {
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)
}) })
}
start().catch(e => {
console.log(e)
process.exit(2)
})
/**
* Created by rockyl on 2020/8/18.
*/
import fetch from 'node-fetch'
import * as config from "config"
export let remoteConfigs = {}
export async function prepare() {
const remoteConfigUrl:string = config.get('remote_config_url')
const resp = await fetch(remoteConfigUrl)
const originConfigs = await resp.json()
//logger.info('originConfigs:', originConfigs)
remoteConfigs = originConfigs.propertySources.reduce((prev, item) => {
return Object.assign(prev, item.source)
}, {})
}
export function getConfigs(name, ...args):any {
let configs = {}
args.unshift(name)
for (let key of args) {
let name = key
let mapKey = key
if (typeof key === 'object') {
name = key.alias
mapKey = key.name
}
configs[name] = remoteConfigs[mapKey]
}
return configs
}
This source diff could not be displayed because it is too large. You can view the blob instead.
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