@kne/fastify-aliyun 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -1
- package/libs/controllers.js +2 -2
- package/libs/{services.js → services/nls.js} +5 -2
- package/libs/services/oss.js +37 -0
- package/package.json +2 -1
package/index.js
CHANGED
|
@@ -18,6 +18,9 @@ module.exports = fp(
|
|
|
18
18
|
endpoint: 'http://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts'
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
|
+
oss: {
|
|
22
|
+
baseDir: 'default'
|
|
23
|
+
},
|
|
21
24
|
prefix: `/api/v${packageJson.version.split('.')[0]}/aliyun`,
|
|
22
25
|
createAuthenticate: () => {
|
|
23
26
|
return [];
|
|
@@ -31,7 +34,7 @@ module.exports = fp(
|
|
|
31
34
|
options,
|
|
32
35
|
modules: [
|
|
33
36
|
['controllers', path.resolve(__dirname, './libs/controllers.js')],
|
|
34
|
-
['services', path.resolve(__dirname, './libs/services
|
|
37
|
+
['services', path.resolve(__dirname, './libs/services')]
|
|
35
38
|
]
|
|
36
39
|
});
|
|
37
40
|
},
|
package/libs/controllers.js
CHANGED
|
@@ -7,7 +7,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
7
7
|
},
|
|
8
8
|
async () => {
|
|
9
9
|
const { services } = fastify.aliyun;
|
|
10
|
-
return services.getToken();
|
|
10
|
+
return services.nls.getToken();
|
|
11
11
|
}
|
|
12
12
|
);
|
|
13
13
|
|
|
@@ -28,7 +28,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
28
28
|
},
|
|
29
29
|
async request => {
|
|
30
30
|
const { services } = fastify.aliyun;
|
|
31
|
-
return await services.tts(request.body);
|
|
31
|
+
return await services.nls.tts(request.body);
|
|
32
32
|
}
|
|
33
33
|
);
|
|
34
34
|
});
|
|
@@ -3,6 +3,7 @@ const { RPCClient } = require('@alicloud/pop-core');
|
|
|
3
3
|
const fetch = require('node-fetch');
|
|
4
4
|
const fileType = require('file-type');
|
|
5
5
|
module.exports = fp(async (fastify, options) => {
|
|
6
|
+
const { services } = fastify.aliyun;
|
|
6
7
|
const getToken = async () => {
|
|
7
8
|
if (options.cache.has('nls:token')) {
|
|
8
9
|
const Token = options.cache.get('nls:token');
|
|
@@ -18,7 +19,6 @@ module.exports = fp(async (fastify, options) => {
|
|
|
18
19
|
options.cache.set('nls:token', Token, Token.expireTime * 1000 - Date.now());
|
|
19
20
|
return { token: Token.Id };
|
|
20
21
|
};
|
|
21
|
-
|
|
22
22
|
const tts = async ({ text, audioName }) => {
|
|
23
23
|
const { token } = await getToken();
|
|
24
24
|
const response = await fetch(options.nls.tts.endpoint, {
|
|
@@ -51,5 +51,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
51
51
|
file: { toBuffer: () => buffer, filename: `${audioName || 'audio'}.${type.ext}`, mimetype: type.mime }
|
|
52
52
|
});
|
|
53
53
|
};
|
|
54
|
-
|
|
54
|
+
services.nls = {
|
|
55
|
+
getToken,
|
|
56
|
+
tts
|
|
57
|
+
};
|
|
55
58
|
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const OSS = require('ali-oss');
|
|
2
|
+
const fp = require('fastify-plugin');
|
|
3
|
+
|
|
4
|
+
module.exports = fp(async (fastify, options) => {
|
|
5
|
+
const { services } = fastify.aliyun;
|
|
6
|
+
const client = new OSS({
|
|
7
|
+
region: options.oss.region,
|
|
8
|
+
accessKeyId: options.oss.accessKeyId,
|
|
9
|
+
accessKeySecret: options.oss.accessKeySecret,
|
|
10
|
+
bucket: options.oss.bucket
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const uploadFile = async ({ file, filename }) => {
|
|
14
|
+
await client.put(`${options.oss.baseDir}/${filename}`, file);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const uploadFileStream = async ({ stream, filename }) => {
|
|
18
|
+
await client.putStream(`${options.oss.baseDir}/${filename}`, stream);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const downloadFile = async ({ filename }) => {
|
|
22
|
+
const result = await client.get(`${options.oss.baseDir}/${filename}`);
|
|
23
|
+
return result.content;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getFileLink = ({ filename, expires }) => {
|
|
27
|
+
return client.signatureUrl(`${options.oss.baseDir}/${filename}`, { expires: expires || 3600 });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
services.oss = {
|
|
31
|
+
client,
|
|
32
|
+
uploadFile,
|
|
33
|
+
uploadFileStream,
|
|
34
|
+
getFileLink,
|
|
35
|
+
downloadFile
|
|
36
|
+
};
|
|
37
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-aliyun",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "对接阿里云服务",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@alicloud/pop-core": "^1.7.13",
|
|
42
42
|
"@kne/fastify-namespace": "^0.1.2",
|
|
43
|
+
"ali-oss": "^6.20.0",
|
|
43
44
|
"file-type": "^16.5.4",
|
|
44
45
|
"lodash": "^4.17.21",
|
|
45
46
|
"node-cache": "^5.1.2",
|