Commit 240063b6 authored by rockyl's avatar rockyl

init

parent 1fd247bf
......@@ -8,7 +8,9 @@ const compress_1 = require("./compress");
const ali_oss_duiba_1 = require("ali-oss-duiba");
const objectHash = require("object-hash");
const path = require("path");
const acceptTypes = ['image/png', 'image/jpeg'];
const fs = require("fs-extra");
const TYPE_DATA_URL = 'text/dataurl';
const acceptTypes = ['image/png', 'image/jpeg', TYPE_DATA_URL];
function applyApi(server) {
server.get('/info', function (req, res, next) {
res.sendRaw('Tiny image serve is running.');
......@@ -24,15 +26,8 @@ exports.default = applyApi;
//一个文件可以返回blob,多个文件必须传cdnRoot
async function tinify(req, res, next) {
const { type = 'builtin', cdnRoot } = req.query;
const fileMap = req.files;
const files = Object.entries(fileMap).filter(([key, file]) => {
const accept = acceptTypes.includes(file.type);
if (accept) {
file.key = key;
}
return accept;
}).map(([key, file]) => file);
const fileCount = files.length;
const inputs = await prepareInputs(req);
const fileCount = inputs.length;
if (fileCount === 0) {
res.send({
code: 1,
......@@ -40,7 +35,7 @@ async function tinify(req, res, next) {
});
}
else if (fileCount === 1) {
const [file] = files;
const [file] = inputs;
const result = await tinifyOnce(file, type, cdnRoot);
if (typeof result === 'string') {
res.send({
......@@ -62,7 +57,7 @@ async function tinify(req, res, next) {
}
}
else if (cdnRoot) {
const result = await Promise.all(files.map(file => tinifyOnce(file, type, cdnRoot)
const result = await Promise.all(inputs.map(file => tinifyOnce(file, type, cdnRoot)
.then(result => {
if (typeof result === 'string') {
return {
......@@ -88,11 +83,45 @@ async function tinify(req, res, next) {
}
return next();
}
async function prepareInputs(req) {
const inputs = [];
for (let key in req.files) {
const file = req.files[key];
if (!acceptTypes.includes(file.type)) {
continue;
}
let extname, buffer;
if (file.type === TYPE_DATA_URL) {
const dataUrl = await fs.readFile(file.path, 'utf-8');
const result = dataUrl.match(/data:(image\/\w+);/);
if (result) {
const type = result[1];
if (!acceptTypes.includes(type)) {
continue;
}
extname = '.' + type.substr(type.indexOf('/') + 1);
let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");
buffer = new Buffer(base64Data, 'base64');
}
}
else {
extname = path.extname(file.name);
buffer = await fs.readFile(file.path);
}
const input = {
key,
extname,
buffer,
};
inputs.push(input);
}
return inputs;
}
async function tinifyOnce(file, type, cdnRoot) {
try {
const buffer = await (0, compress_1.compressImage)(file.path, type);
const buffer = await (0, compress_1.compressImage)(file.buffer, type);
if (cdnRoot) {
const cdnPath = path.join(cdnRoot, objectHash(buffer) + path.extname(file.name));
const cdnPath = path.join(cdnRoot, objectHash(buffer) + file.extname);
return (0, ali_oss_duiba_1.uploadFile)(buffer, cdnPath);
}
else {
......
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,qCAA6B;AAC7B,yCAAwC;AACxC,iDAAwC;AACxC,0CAAyC;AACzC,6BAA4B;AAE5B,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,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,GAAG,CAAC,gBAAgB,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI;QACpD,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACjB,OAAO,IAAI,EAAE,CAAA;IACd,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;AAC/B,CAAC;AAZD,2BAYC;AAED,6BAA6B;AAC7B,KAAK,UAAU,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;IACnC,MAAM,EAAC,IAAI,GAAG,SAAS,EAAE,OAAO,EAAC,GAAG,GAAG,CAAC,KAA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,CAAC,CAAA;aACzC;SACD;aAAM;YACN,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACjC,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;SACrC;QAED,MAAM,KAAK,GAAQ;YAClB,GAAG;YACH,OAAO;YACP,MAAM;SACN,CAAA;QACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;KAClB;IACD,OAAO,MAAM,CAAA;AACd,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO;IAC5C,IAAI;QACH,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAa,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QACrD,IAAI,OAAO,EAAE;YACZ,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAA;YACrE,OAAO,IAAA,0BAAU,EAAC,MAAM,EAAE,OAAO,CAAC,CAAA;SAClC;aAAM;YACN,OAAO,MAAM,CAAA;SACb;KACD;IAAC,OAAO,CAAC,EAAE;QACX,OAAO,CAAC,CAAA;KACR;AACF,CAAC"}
\ No newline at end of file
......@@ -4,23 +4,23 @@
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressImage = void 0;
const imagemin = require("imagemin");
const { buffer: 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) {
function compressImage(buffer, type) {
switch (type) {
case 'builtin':
return imagemin([filePath], {
return imagemin(buffer, {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
}).then(buffer => buffer[0].data);
});
case 'tinypng':
return (0, node_tinify_1.compress)(filePath, { returnType: 'buffer' });
return (0, node_tinify_1.compress)(buffer, { returnType: 'buffer' });
}
}
exports.compressImage = compressImage;
......
{"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
{"version":3,"file":"compress.js","sourceRoot":"","sources":["../src/compress.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,MAAM,EAAC,MAAM,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;AAC9C,MAAM,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAA;AACxD,MAAM,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAA;AAExD,6CAAoC;AAEpC,SAAgB,aAAa,CAAC,MAAc,EAAE,IAAI;IACjD,QAAQ,IAAI,EAAE;QACb,KAAK,SAAS;YACb,OAAO,QAAQ,CAAC,MAAM,EAAE;gBACvB,OAAO,EAAE;oBACR,gBAAgB,EAAE;oBAClB,gBAAgB,CAAC;wBAChB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;qBACnB,CAAC;iBACF;aACD,CAAC,CAAA;QACH,KAAK,SAAS;YACb,OAAO,IAAA,sBAAQ,EAAC,MAAM,EAAE,EAAC,UAAU,EAAE,QAAQ,EAAC,CAAC,CAAA;KAChD;AACF,CAAC;AAdD,sCAcC"}
\ No newline at end of file
"use strict";
/**
* Created by rockyl on 2020-06-04.
*
* 跨域
*/
Object.defineProperty(exports, "__esModule", { value: true });
function default_1() {
return async (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
return next();
};
}
exports.default = default_1;
//# sourceMappingURL=cors.js.map
\ No newline at end of file
{"version":3,"file":"cors.js","sourceRoot":"","sources":["../src/cors.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAEH;IACC,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC/B,GAAG,CAAC,SAAS,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;QACjD,OAAO,IAAI,EAAE,CAAA;IACd,CAAC,CAAA;AACF,CAAC;AALD,4BAKC"}
\ No newline at end of file
......@@ -10,6 +10,7 @@ const logger_1 = require("./logger");
const config = require("config");
const api_1 = require("./api");
const ali_oss_duiba_1 = require("ali-oss-duiba");
const cors_1 = require("./cors");
let eureka;
const server = restify.createServer({
name: 'tiny-image',
......@@ -19,6 +20,7 @@ const ignoreUrlLogger = ['/monitor/check'];
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.use((0, cors_1.default)());
server.use(function (req, res, next) {
if (!ignoreUrlLogger.includes(req.url)) {
logger_1.default.info(req.url);
......@@ -37,12 +39,12 @@ process.on("SIGTERM", () => {
});
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' });
const ossConfigs = (0, remote_configs_1.getConfigs)({ name: `apollo.oss.duiba.accessId`, alias: 'accessId' }, { name: `apollo.oss.duiba.accessKey`, alias: 'accessKey' }, { name: `apollo.oss.duiba.bucketName`, alias: 'bucketName' });
logger_1.default.info('got oss configs:', ossConfigs);
(0, ali_oss_duiba_1.setup)({
accessKeyId: 'LTAI5tLEo6SbjfmNPmH7Mz3o',
accessKeySecret: '8yZ6nVMg56DEupEEMRl6rz1DcpG2R7',
bucket: 'duiba',
accessKeyId: ossConfigs.accessId,
accessKeySecret: ossConfigs.accessKey,
bucket: ossConfigs.bucketName,
region: 'oss-cn-hangzhou',
});
const port = config.get('PORT');
......
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,qDAA6E;AAE7E,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;AAClC,qCAAwC;AACxC,qCAA6B;AAC7B,iCAAgC;AAChC,+BAA4B;AAC5B,iDAA+C;AAE/C,IAAI,MAAM,CAAA;AAEV,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IACnC,IAAI,EAAE,YAAY;IAClB,OAAO,EAAE,OAAO;CAChB,CAAC,CAAA;AAEF,MAAM,eAAe,GAAG,CAAC,gBAAgB,CAAC,CAAA;AAE1C,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAA;AAC3D,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAA;AACzC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAA;AACxC,MAAM,CAAC,GAAG,CAAC,UAAU,GAAG,EAAE,GAAG,EAAE,I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AAM,EAAE,iBAAiB;KACzB,CAAC,CAAA;IAEF,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK;QACxB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE;YACzB,IAAI;gBACH,MAAM,GAAG,MAAM,IAAA,wBAAe,EAAC,IAAI,CAAC,CAAA;gBACpC,gBAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;aACtC;YAAC,OAAO,CAAC,EAAE;gBACX,gBAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,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
{
"local": {
"host": "http://localhost:8093"
},
"dev": {
"host": "http://tinify.duibadev.com.cn"
},
"prod": {
"host": "http://tinify.duiba.com.cn"
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -7,8 +7,10 @@ import {compressImage} from "./compress"
import {uploadFile} from 'ali-oss-duiba'
import * as objectHash from 'object-hash'
import * as path from 'path'
import * as fs from 'fs-extra'
const acceptTypes = ['image/png', 'image/jpeg']
const TYPE_DATA_URL = 'text/dataurl'
const acceptTypes = ['image/png', 'image/jpeg', TYPE_DATA_URL]
export default function applyApi(server) {
server.get('/info', function (req, res, next) {
......@@ -28,23 +30,15 @@ export default function applyApi(server) {
async function tinify(req, res, next) {
const {type = 'builtin', cdnRoot} = req.query
const fileMap: { [key: string]: any } = req.files
const files = Object.entries(fileMap).filter(([key, file]) => {
const accept = acceptTypes.includes(file.type)
if (accept) {
file.key = key
}
return accept
}).map(([key, file]) => file)
const fileCount = files.length
const inputs = await prepareInputs(req)
const fileCount = inputs.length
if (fileCount === 0) {
res.send({
code: 1,
msg: 'file not exists'
})
} else if (fileCount === 1) {
const [file] = files
const [file] = inputs
const result = await tinifyOnce(file, type, cdnRoot)
if (typeof result === 'string') {
res.send({
......@@ -63,7 +57,7 @@ async function tinify(req, res, next) {
})
}
} else if (cdnRoot) {
const result = await Promise.all(files.map(file => tinifyOnce(file, type, cdnRoot)
const result = await Promise.all<any>(inputs.map(file => tinifyOnce(file, type, cdnRoot)
.then(result => {
if (typeof result === 'string') {
return {
......@@ -90,11 +84,47 @@ async function tinify(req, res, next) {
return next()
}
async function prepareInputs(req) {
const inputs: any[] = []
for (let key in req.files) {
const file = req.files[key]
if (!acceptTypes.includes(file.type)) {
continue
}
let extname, buffer
if (file.type === TYPE_DATA_URL) {
const dataUrl = await fs.readFile(file.path, 'utf-8')
const result = dataUrl.match(/data:(image\/\w+);/)
if (result) {
const type = result[1]
if (!acceptTypes.includes(type)) {
continue
}
extname = '.' + type.substr(type.indexOf('/')+1)
let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "")
buffer = new Buffer(base64Data, 'base64')
}
} else {
extname = path.extname(file.name)
buffer = await fs.readFile(file.path)
}
const input: any = {
key,
extname,
buffer,
}
inputs.push(input)
}
return inputs
}
async function tinifyOnce(file, type, cdnRoot) {
try {
const buffer = await compressImage(file.path, type)
const buffer = await compressImage(file.buffer, type)
if (cdnRoot) {
const cdnPath = path.join(cdnRoot, objectHash(buffer) + path.extname(file.name))
const cdnPath = path.join(cdnRoot, objectHash(buffer) + file.extname)
return uploadFile(buffer, cdnPath)
} else {
return buffer
......
......@@ -2,24 +2,24 @@
* Created by rockyl on 2021/11/5.
*/
const imagemin = require("imagemin")
const {buffer: 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) {
export function compressImage(buffer: Buffer, type) {
switch (type) {
case 'builtin':
return imagemin([filePath], {
return imagemin(buffer, {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
}).then(buffer=>buffer[0].data)
})
case 'tinypng':
return compress(filePath, {returnType: 'buffer'})
return compress(buffer, {returnType: 'buffer'})
}
}
\ No newline at end of file
/**
* Created by rockyl on 2020-06-04.
*
* 跨域
*/
export default function () {
return async (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
return next()
}
}
......@@ -9,6 +9,7 @@ import logger from "./logger"
import * as config from "config"
import applyApi from "./api"
import {setup as setupOSS} from 'ali-oss-duiba'
import cors from "./cors";
let eureka
......@@ -22,6 +23,7 @@ const ignoreUrlLogger = ['/monitor/check']
server.use(restify.plugins.acceptParser(server.acceptable))
server.use(restify.plugins.queryParser())
server.use(restify.plugins.bodyParser())
server.use(cors())
server.use(function (req, res, next) {
if (!ignoreUrlLogger.includes(req.url as string)) {
logger.info(req.url)
......@@ -47,14 +49,15 @@ async function start() {
const ossConfigs = getConfigs(
{name: `apollo.oss.duiba.accessId`, alias: 'accessId'},
{name: `apollo.oss.duiba.accessKey`, alias: 'accessKey'}, //todo 解密
{name: `apollo.oss.duiba.accessKey`, alias: 'accessKey'},
{name: `apollo.oss.duiba.bucketName`, alias: 'bucketName'},
)
logger.info('got oss configs:', ossConfigs)
setupOSS({
accessKeyId: 'LTAI5tLEo6SbjfmNPmH7Mz3o',////ossConfigs.accessId,
accessKeySecret: '8yZ6nVMg56DEupEEMRl6rz1DcpG2R7',//ossConfigs.accessKey,
bucket: 'duiba',
accessKeyId: ossConfigs.accessId,
accessKeySecret: ossConfigs.accessKey,
bucket: ossConfigs.bucketName,
region: 'oss-cn-hangzhou',
})
......
......@@ -86,6 +86,13 @@
"@types/minimatch" "*"
"@types/node" "*"
"@types/imagemin@^8.0.0":
version "8.0.0"
resolved "http://npm.dui88.com:80/@types%2fimagemin/-/imagemin-8.0.0.tgz#bf5bbe1feff3b112c7e0de06d024712ad261e033"
integrity sha1-v1u+H+/zsRLH4N4G0CRxKtJh4DM=
dependencies:
"@types/node" "*"
"@types/minimatch@*":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
......@@ -1878,7 +1885,7 @@ isurl@^1.0.0-alpha5:
has-to-string-tag-x "^1.2.0"
is-object "^1.0.1"
jpegtran-bin-cn@^6.0.1:
jpegtran-bin-cn@^6.0.1, jpegtran-bin-cn@^6.0.6:
version "6.0.6"
resolved "https://registry.yarnpkg.com/jpegtran-bin-cn/-/jpegtran-bin-cn-6.0.6.tgz#4580feae2454536193bb4ce577b6ea81065c84fd"
integrity sha512-Pjlyb+cue/zQuFq5/fB0HTfjw+eXbwNJmvMD5UecRhqkrQRGkHaaHhd15QpOz9rhpuFEA6QekE6MfBOcYwPNvA==
......@@ -1886,14 +1893,6 @@ jpegtran-bin-cn@^6.0.1:
bin-build "^3.0.0"
bin-wrapper "^4.0.0"
jpegtran-bin-cn@^6.0.6:
version "6.0.6"
resolved "http://npm.dui88.com:80/jpegtran-bin-cn/-/jpegtran-bin-cn-6.0.6.tgz#4580feae2454536193bb4ce577b6ea81065c84fd"
integrity sha1-RYD+riRUU2GTu0zld7bqgQZchP0=
dependencies:
bin-build "^3.0.0"
bin-wrapper "^4.0.0"
js-base64@^2.5.2:
version "2.6.4"
resolved "http://npm.dui88.com:80/js-base64/-/js-base64-2.6.4.tgz#f4e686c5de1ea1f867dbcad3d46d969428df98c4"
......
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