Commit 1fd247bf authored by rockyl's avatar rockyl

init

parent 77bd123b
{
"PORT": 8090,
"PORT": 8093,
"remote_config_url": "http://configserver.dui88.com/duiba-tiny-image-node/dev?accessKey=b13f806fda2248e2aeff3e101b015876"
}
......@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("./logger");
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'];
function applyApi(server) {
server.get('/info', function (req, res, next) {
......@@ -19,9 +21,9 @@ function applyApi(server) {
server.post('/tinify', tinify);
}
exports.default = applyApi;
//一个文件可以返回blob,多个文件必须传cdnPath
//一个文件可以返回blob,多个文件必须传cdnRoot
async function tinify(req, res, next) {
const { type = 'builtin', cdnPath } = req.query;
const { type = 'builtin', cdnRoot } = req.query;
const fileMap = req.files;
const files = Object.entries(fileMap).filter(([key, file]) => {
const accept = acceptTypes.includes(file.type);
......@@ -39,7 +41,7 @@ async function tinify(req, res, next) {
}
else if (fileCount === 1) {
const [file] = files;
const result = await tinifyOnce(file.path, type, cdnPath);
const result = await tinifyOnce(file, type, cdnRoot);
if (typeof result === 'string') {
res.send({
code: 0,
......@@ -59,8 +61,8 @@ async function tinify(req, res, next) {
});
}
}
else if (cdnPath) {
const urls = await Promise.all(files.map(file => tinifyOnce(file.path, type, cdnPath)
else if (cdnRoot) {
const result = await Promise.all(files.map(file => tinifyOnce(file, type, cdnRoot)
.then(result => {
if (typeof result === 'string') {
return {
......@@ -69,20 +71,28 @@ async function tinify(req, res, next) {
};
}
})));
console.log(urls);
const data = {};
for (let item of result) {
data[item.key] = item.url;
}
res.send({
code: 0,
data,
});
}
else {
res.send({
code: 4,
msg: 'specify cdnPath when multi files'
code: 2,
msg: 'specify cdnRoot when multi files'
});
}
return next();
}
async function tinifyOnce(filePath, type, cdnPath) {
async function tinifyOnce(file, type, cdnRoot) {
try {
const buffer = await (0, compress_1.compressImage)(filePath, type);
if (cdnPath) {
const buffer = await (0, compress_1.compressImage)(file.path, type);
if (cdnRoot) {
const cdnPath = path.join(cdnRoot, objectHash(buffer) + path.extname(file.name));
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;AAExC,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,KAAK,CAAA;IAE7C,MAAM,OAAO,GAA2B,GAAG,CAAC,KAAK,CAAA;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE;QAC5D,MAAM,MAAM,GAAG,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,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F;IACD,OAAO,IAAI,EAAE,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,IAAI,EAAE,IAAI,CAAC,CAAA;QACnD,IAAI,OAAO,EAAE;YACZ,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;YAChF,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
......@@ -40,8 +40,8 @@ async function start() {
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,
accessKeyId: 'LTAI5tLEo6SbjfmNPmH7Mz3o',
accessKeySecret: '8yZ6nVMg56DEupEEMRl6rz1DcpG2R7',
bucket: 'duiba',
region: 'oss-cn-hangzhou',
});
......
{"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,EAA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AAK,EAAE,UAAU,EAAC,EACtD,EAAC,IAAI,EAAE,4BAA4B,EAAE,KAAK,EAAE,WAAW,EAAC,CACxD,CAAA;IACD,gBAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAA;IAE3C,IAAA,qBAAQ,EAAC;QACR,WAAW,EAAE,0BAA0B;QACvC,eAAe,EAAE,gCAAgC;QACjD,MAAM,EAAE,OAAO;QACf,MAAM,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
### 查询项目
POST http://localhost:8090/tinify?cdnPath=spark/assets
POST http://localhost:8093/tinify?cdnRoot=spark/assets
Accept: */*
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=WebAppBoundary
......
......@@ -5,6 +5,8 @@
import logger from "./logger"
import {compressImage} from "./compress"
import {uploadFile} from 'ali-oss-duiba'
import * as objectHash from 'object-hash'
import * as path from 'path'
const acceptTypes = ['image/png', 'image/jpeg']
......@@ -22,9 +24,9 @@ export default function applyApi(server) {
server.post('/tinify', tinify)
}
//一个文件可以返回blob,多个文件必须传cdnPath
//一个文件可以返回blob,多个文件必须传cdnRoot
async function tinify(req, res, next) {
const {type = 'builtin', cdnPath} = req.query
const {type = 'builtin', cdnRoot} = req.query
const fileMap: { [key: string]: any } = req.files
......@@ -43,7 +45,7 @@ async function tinify(req, res, next) {
})
} else if (fileCount === 1) {
const [file] = files
const result = await tinifyOnce(file.path, type, cdnPath)
const result = await tinifyOnce(file, type, cdnRoot)
if (typeof result === 'string') {
res.send({
code: 0,
......@@ -60,8 +62,8 @@ async function tinify(req, res, next) {
msg: 'compress failed'
})
}
} else if (cdnPath) {
const urls = await Promise.all(files.map(file => tinifyOnce(file.path, type, cdnPath)
} else if (cdnRoot) {
const result = await Promise.all(files.map(file => tinifyOnce(file, type, cdnRoot)
.then(result => {
if (typeof result === 'string') {
return {
......@@ -71,20 +73,28 @@ async function tinify(req, res, next) {
}
})
))
console.log(urls)
const data = {}
for (let item of result) {
data[item.key] = item.url
}
res.send({
code: 0,
data,
})
} else {
res.send({
code: 4,
msg: 'specify cdnPath when multi files'
code: 2,
msg: 'specify cdnRoot when multi files'
})
}
return next()
}
async function tinifyOnce(filePath, type, cdnPath) {
async function tinifyOnce(file, type, cdnRoot) {
try {
const buffer = await compressImage(filePath, type)
if (cdnPath) {
const buffer = await compressImage(file.path, type)
if (cdnRoot) {
const cdnPath = path.join(cdnRoot, objectHash(buffer) + path.extname(file.name))
return uploadFile(buffer, cdnPath)
} else {
return buffer
......
......@@ -52,8 +52,8 @@ async function start() {
logger.info('got oss configs:', ossConfigs)
setupOSS({
accessKeyId: ossConfigs.accessId,
accessKeySecret: ossConfigs.accessKey,
accessKeyId: 'LTAI5tLEo6SbjfmNPmH7Mz3o',////ossConfigs.accessId,
accessKeySecret: '8yZ6nVMg56DEupEEMRl6rz1DcpG2R7',//ossConfigs.accessKey,
bucket: 'duiba',
region: 'oss-cn-hangzhou',
})
......
......@@ -42,14 +42,6 @@
resolved "http://npm.dui88.com:80/@tootallnate%2fonce/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
integrity sha1-zLkURTYBeaBOf+av94wA/8Hur4I=
"@tuia/apollo@^0.1.0":
version "0.1.0"
resolved "http://npm.dui88.com:80/@tuia%2fapollo/-/apollo-0.1.0.tgz#b1ea17e7f498c0b1325437471285e7156786d5fa"
integrity sha512-WVsGr2VQLGDxmO86tgGndKHDBodLFLFv1hXX+E95m2fFu/NNbqAqE+0XZL/QKn+d+TKb6Fmv9YsCJ/O1/HaYDg==
dependencies:
axios "^0.20.0"
lodash "^4.17.20"
"@types/ali-oss@^6.0.8":
version "6.16.1"
resolved "http://npm.dui88.com:80/@types%2fali-oss/-/ali-oss-6.16.1.tgz#0bf32c7c5da6360eb5b344717aa45d15699539a4"
......@@ -122,6 +114,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.36.tgz#5bd54d2383e714fc4d2c258107ee70c5bad86d0c"
integrity sha512-+5haRZ9uzI7rYqzDznXgkuacqb6LJhAti8mzZKWxIXn/WEtvB+GHVJ7AuMwcN1HMvXOSJcrvA6PPoYHYOYYebA==
"@types/object-hash@^2.2.1":
version "2.2.1"
resolved "http://npm.dui88.com:80/@types%2fobject-hash/-/object-hash-2.2.1.tgz#67c169f8f033e0b62abbf81df2d00f4598d540b9"
integrity sha1-Z8Fp+PAz4LYqu/gd8tAPRZjVQLk=
"@types/restify@^8.4.2":
version "8.5.2"
resolved "https://registry.yarnpkg.com/@types/restify/-/restify-8.5.2.tgz#e4679d5ee7852f2a123b3e0c92ee1f72515c8a25"
......@@ -311,13 +308,6 @@ axios@^0.19.0:
dependencies:
follow-redirects "1.5.10"
axios@^0.20.0:
version "0.20.0"
resolved "http://npm.dui88.com:80/axios/-/axios-0.20.0.tgz#057ba30f04884694993a8cd07fa394cff11c50bd"
integrity sha1-BXujDwSIRpSZOozQf6OUz/EcUL0=
dependencies:
follow-redirects "^1.10.0"
balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
......@@ -1257,11 +1247,6 @@ follow-redirects@1.5.10:
dependencies:
debug "=3.1.0"
follow-redirects@^1.10.0:
version "1.14.5"
resolved "http://npm.dui88.com:80/follow-redirects/-/follow-redirects-1.14.5.tgz#f09a5848981d3c772b5392309778523f8d85c381"
integrity sha1-8JpYSJgdPHcrU5Iwl3hSP42Fw4E=
forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
......@@ -1901,6 +1886,14 @@ 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"
......@@ -2006,7 +1999,7 @@ load-json-file@^1.0.0:
pinkie-promise "^2.0.0"
strip-bom "^2.0.0"
lodash@^4.13.1, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20:
lodash@^4.13.1, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
......
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