@shun-js/cos 0.3.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 qiaowenbin<uikoo9@qq.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/app.js ADDED
@@ -0,0 +1,26 @@
1
+ // config
2
+ const { parseServerConfig } = require('@shun-js/shun-config');
3
+
4
+ // init
5
+ (async () => {
6
+ // config
7
+ const config = await parseServerConfig(process.argv);
8
+ if (!config) {
9
+ console.log('read server config fail');
10
+ return;
11
+ }
12
+
13
+ // options
14
+ const options = {};
15
+
16
+ // options config
17
+ options.config = config;
18
+
19
+ // options log
20
+ options.log = require('qiao-log');
21
+ options.logOptions = require('./server/log-options.js')();
22
+
23
+ // start
24
+ const app = await require('qiao-z')(options);
25
+ app.listen(config.port);
26
+ })();
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@shun-js/cos",
3
+ "version": "0.3.2",
4
+ "description": "cos",
5
+ "keywords": [
6
+ "shun.js",
7
+ "cos"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "uikoo9 <uikoo9@qq.com>",
11
+ "homepage": "https://github.com/uikoo9/shun-js",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/uikoo9/shun-js.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/uikoo9/shun-js/issues"
18
+ },
19
+ "files": [
20
+ "server",
21
+ "app.js"
22
+ ],
23
+ "dependencies": {
24
+ "@shun-js/shun-config": "^0.3.1",
25
+ "@shun-js/shun-service": "^0.3.1",
26
+ "qiao-cos": "^5.0.6",
27
+ "qiao-log": "^5.1.9",
28
+ "qiao-z": "^5.8.9"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "registry": "https://registry.npmjs.org/"
33
+ },
34
+ "gitHead": "4fa47fe5cf1b00acfe54f47af836101291093f06"
35
+ }
@@ -0,0 +1,17 @@
1
+ // service
2
+ const service = require('../service/CosService.js');
3
+
4
+ /**
5
+ * cos controller
6
+ */
7
+ module.exports = (app) => {
8
+ // /cos/token
9
+ app.post('/cos/token', (req, res) => {
10
+ service.cosToken(req, res);
11
+ });
12
+
13
+ // /cos/sign
14
+ app.post('/cos/sign', (req, res) => {
15
+ service.cosSign(req, res);
16
+ });
17
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * log options
3
+ * @returns
4
+ */
5
+ module.exports = () => {
6
+ // log options
7
+ const logLevel = 'debug';
8
+ const logPattern = 'yyyy-MM-dd-hh';
9
+ const logPath = require('path').resolve(__dirname, '../logs/qiao-z.log');
10
+
11
+ return {
12
+ appenders: {
13
+ stdout: {
14
+ type: 'stdout',
15
+ },
16
+ datefile: {
17
+ type: 'dateFile',
18
+ pattern: logPattern,
19
+ filename: logPath,
20
+ keepFileExt: true,
21
+ },
22
+ },
23
+ categories: {
24
+ default: {
25
+ level: logLevel,
26
+ appenders: ['stdout', 'datefile'],
27
+ },
28
+ },
29
+ };
30
+ };
@@ -0,0 +1,81 @@
1
+ // cos
2
+ const COS = require('qiao-cos');
3
+
4
+ /**
5
+ * cosToken
6
+ * @param {*} req
7
+ * @param {*} res
8
+ */
9
+ exports.cosToken = async (req, res) => {
10
+ const methodName = 'cosToken';
11
+
12
+ // qcos
13
+ const qcos = COS({
14
+ SecretId: req.body.cosSecretId,
15
+ SecretKey: req.body.cosSecretKey,
16
+ Bucket: req.body.cosBucket,
17
+ Region: req.body.cosRegion,
18
+ });
19
+
20
+ // const
21
+ const durationSeconds = req.body.durationSeconds;
22
+ const allowPrefix = req.body.allowPrefix;
23
+
24
+ // sts
25
+ try {
26
+ const sts = await qcos.getCredential(durationSeconds, allowPrefix);
27
+ req.logger.info(methodName, 'sts', sts);
28
+ res.jsonSuccess('cos token success', sts);
29
+ } catch (error) {
30
+ const msg = 'cos token error';
31
+ req.logger.error(methodName, msg, error);
32
+ res.jsonFail(msg);
33
+ }
34
+ };
35
+
36
+ /**
37
+ * cosSign
38
+ * @param {*} req
39
+ * @param {*} res
40
+ * @returns
41
+ */
42
+ exports.cosSign = (req, res) => {
43
+ const methodName = 'cosSign';
44
+
45
+ // qcos
46
+ const qcos = COS({
47
+ SecretId: req.body.cosSecretId,
48
+ SecretKey: req.body.cosSecretKey,
49
+ Bucket: req.body.cosBucket,
50
+ Region: req.body.cosRegion,
51
+ signKey: req.body.signKey,
52
+ });
53
+
54
+ // const
55
+ const signTimeout = req.body.signTimeout;
56
+ const cdnHost = req.body.cdnHost;
57
+ const filePath = req.body.filePath;
58
+ const formatWebp = req.body.formatWebp;
59
+ const formatWidth = req.body.formatWidth;
60
+
61
+ // sign
62
+ const signUrl = qcos.cdnSign(filePath, signTimeout);
63
+ const finalUrl = [cdnHost + signUrl];
64
+
65
+ // webp
66
+ if (formatWebp === 'yes') {
67
+ // mp4
68
+ if (filePath.endsWith('mp4')) {
69
+ finalUrl.push('&ci-process=snapshot&time=0&width=0&height=0&format=jpg&rotate=auto&mode=exactframe');
70
+ }
71
+
72
+ // format
73
+ finalUrl.push('&imageMogr2/format/webp');
74
+ if (formatWidth) finalUrl.push(`|imageMogr2/thumbnail/${formatWidth}x`);
75
+ }
76
+
77
+ // r
78
+ const url = finalUrl.join('');
79
+ req.logger.info(methodName, 'url', url);
80
+ res.jsonSuccess('cos sign success', url);
81
+ };
@@ -0,0 +1,130 @@
1
+ // model
2
+ const { cosToken, cosSign } = require('../model/CosModel.js');
3
+
4
+ /**
5
+ * cosToken
6
+ * @param {*} req
7
+ * @param {*} res
8
+ */
9
+ exports.cosToken = async (req, res) => {
10
+ const methodName = 'cosToken';
11
+
12
+ // const cos
13
+ const cosSecretId = req.body.cosSecretId;
14
+ const cosSecretKey = req.body.cosSecretKey;
15
+ const cosBucket = req.body.cosBucket;
16
+ const cosRegion = req.body.cosRegion;
17
+ if (!cosSecretId) {
18
+ const msg = 'need cosSecretId';
19
+ req.logger.warn(methodName, msg);
20
+ return res.jsonFail(msg);
21
+ }
22
+ if (!cosSecretKey) {
23
+ const msg = 'need cosSecretKey';
24
+ req.logger.warn(methodName, msg);
25
+ return res.jsonFail(msg);
26
+ }
27
+ if (!cosBucket) {
28
+ const msg = 'need cosBucket';
29
+ req.logger.warn(methodName, msg);
30
+ return res.jsonFail(msg);
31
+ }
32
+ if (!cosRegion) {
33
+ const msg = 'need cosRegion';
34
+ req.logger.warn(methodName, msg);
35
+ return res.jsonFail(msg);
36
+ }
37
+
38
+ // const others
39
+ const durationSeconds = req.body.durationSeconds;
40
+ const allowPrefix = req.body.allowPrefix;
41
+ if (!durationSeconds) {
42
+ const msg = 'need durationSeconds';
43
+ req.logger.warn(methodName, msg);
44
+ return res.jsonFail(msg);
45
+ }
46
+ if (!allowPrefix) {
47
+ const msg = 'need allowPrefix';
48
+ req.logger.warn(methodName, msg);
49
+ return res.jsonFail(msg);
50
+ }
51
+
52
+ // cos token
53
+ cosToken(req, res);
54
+ };
55
+
56
+ /**
57
+ * cosSign
58
+ * @param {*} req
59
+ * @param {*} res
60
+ */
61
+ exports.cosSign = async (req, res) => {
62
+ const methodName = 'cosSign';
63
+
64
+ // const cos
65
+ const cosSecretId = req.body.cosSecretId;
66
+ const cosSecretKey = req.body.cosSecretKey;
67
+ const cosBucket = req.body.cosBucket;
68
+ const cosRegion = req.body.cosRegion;
69
+ if (!cosSecretId) {
70
+ const msg = 'need cosSecretId';
71
+ req.logger.warn(methodName, msg);
72
+ return res.jsonFail(msg);
73
+ }
74
+ if (!cosSecretKey) {
75
+ const msg = 'need cosSecretKey';
76
+ req.logger.warn(methodName, msg);
77
+ return res.jsonFail(msg);
78
+ }
79
+ if (!cosBucket) {
80
+ const msg = 'need cosBucket';
81
+ req.logger.warn(methodName, msg);
82
+ return res.jsonFail(msg);
83
+ }
84
+ if (!cosRegion) {
85
+ const msg = 'need cosRegion';
86
+ req.logger.warn(methodName, msg);
87
+ return res.jsonFail(msg);
88
+ }
89
+
90
+ // const others
91
+ const signKey = req.body.signKey;
92
+ const signTimeout = req.body.signTimeout;
93
+ const cdnHost = req.body.cdnHost;
94
+ const filePath = req.body.filePath;
95
+ const formatWebp = req.body.formatWebp;
96
+ const formatWidth = req.body.formatWidth;
97
+ if (!signKey) {
98
+ const msg = 'need signKey';
99
+ req.logger.warn(methodName, msg);
100
+ return res.jsonFail(msg);
101
+ }
102
+ if (!signTimeout) {
103
+ const msg = 'need signTimeout';
104
+ req.logger.warn(methodName, msg);
105
+ return res.jsonFail(msg);
106
+ }
107
+ if (!cdnHost) {
108
+ const msg = 'need cdnHost';
109
+ req.logger.warn(methodName, msg);
110
+ return res.jsonFail(msg);
111
+ }
112
+ if (!filePath) {
113
+ const msg = 'need filePath';
114
+ req.logger.warn(methodName, msg);
115
+ return res.jsonFail(msg);
116
+ }
117
+ if (!formatWebp) {
118
+ const msg = 'need formatWebp';
119
+ req.logger.warn(methodName, msg);
120
+ return res.jsonFail(msg);
121
+ }
122
+ if (!formatWidth) {
123
+ const msg = 'need formatWidth';
124
+ req.logger.warn(methodName, msg);
125
+ return res.jsonFail(msg);
126
+ }
127
+
128
+ // cos sign
129
+ cosSign(req, res);
130
+ };