picgo 1.4.26 → 1.5.0-alpha.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.
Files changed (101) hide show
  1. package/.eslintignore +2 -0
  2. package/.github/workflows/alpha.yml +22 -0
  3. package/CHANGELOG.md +43 -0
  4. package/README.md +14 -1
  5. package/bin/picgo +1 -1
  6. package/dist/{src/core → core}/Lifecycle.d.ts +15 -15
  7. package/dist/{src/core → core}/PicGo.d.ts +44 -44
  8. package/dist/i18n/en.d.ts +2 -0
  9. package/dist/i18n/index.d.ts +16 -0
  10. package/dist/i18n/zh-CN.d.ts +65 -0
  11. package/dist/index.cjs.js +2 -0
  12. package/dist/index.d.ts +9 -0
  13. package/dist/index.esm.js +2 -0
  14. package/dist/{src/lib → lib}/Commander.d.ts +16 -16
  15. package/dist/{src/lib → lib}/LifecyclePlugins.d.ts +16 -16
  16. package/dist/{src/lib → lib}/Logger.d.ts +16 -16
  17. package/dist/{src/lib → lib}/PluginHandler.d.ts +10 -10
  18. package/dist/{src/lib → lib}/PluginLoader.d.ts +27 -27
  19. package/dist/{src/lib → lib}/Request.d.ts +11 -11
  20. package/dist/{src/plugins → plugins}/commander/config.d.ts +3 -3
  21. package/dist/plugins/commander/i18n.d.ts +3 -0
  22. package/dist/{src/plugins → plugins}/commander/index.d.ts +3 -3
  23. package/dist/{src/plugins → plugins}/commander/init.d.ts +3 -3
  24. package/dist/{src/plugins → plugins}/commander/pluginHandler.d.ts +3 -3
  25. package/dist/{src/plugins → plugins}/commander/proxy.d.ts +3 -3
  26. package/dist/{src/plugins → plugins}/commander/setting.d.ts +5 -5
  27. package/dist/{src/plugins → plugins}/commander/upload.d.ts +3 -3
  28. package/dist/{src/plugins → plugins}/commander/use.d.ts +3 -3
  29. package/dist/{src/plugins → plugins}/transformer/base64.d.ts +5 -5
  30. package/dist/{src/plugins → plugins}/transformer/index.d.ts +3 -3
  31. package/dist/{src/plugins → plugins}/transformer/path.d.ts +5 -5
  32. package/dist/plugins/uploader/aliyun.d.ts +2 -0
  33. package/dist/plugins/uploader/github.d.ts +2 -0
  34. package/dist/plugins/uploader/imgur.d.ts +2 -0
  35. package/dist/{src/plugins → plugins}/uploader/index.d.ts +3 -3
  36. package/dist/plugins/uploader/qiniu.d.ts +2 -0
  37. package/dist/plugins/uploader/smms.d.ts +2 -0
  38. package/dist/plugins/uploader/tcyun.d.ts +8 -0
  39. package/dist/plugins/uploader/upyun.d.ts +2 -0
  40. package/dist/types/index.d.ts +468 -0
  41. package/dist/{src/utils → utils}/common.d.ts +102 -101
  42. package/dist/{src/utils → utils}/createContext.d.ts +6 -6
  43. package/dist/{src/utils → utils}/db.d.ts +15 -15
  44. package/dist/{src/utils → utils}/enum.d.ts +27 -27
  45. package/dist/{src/utils → utils}/eventBus.d.ts +4 -4
  46. package/dist/{src/utils → utils}/getClipboardImage.d.ts +4 -3
  47. package/dist/{src/utils → utils}/initUtils.d.ts +26 -26
  48. package/dist/{src/utils → utils}/interfaces.d.ts +203 -203
  49. package/package.json +27 -22
  50. package/rollup.config.js +82 -0
  51. package/dist/package.json +0 -127
  52. package/dist/src/core/Lifecycle.js +0 -123
  53. package/dist/src/core/PicGo.js +0 -210
  54. package/dist/src/index.d.ts +0 -2
  55. package/dist/src/index.js +0 -6
  56. package/dist/src/lib/Commander.js +0 -56
  57. package/dist/src/lib/LifecyclePlugins.js +0 -52
  58. package/dist/src/lib/Logger.js +0 -82
  59. package/dist/src/lib/PluginHandler.js +0 -260
  60. package/dist/src/lib/PluginLoader.js +0 -145
  61. package/dist/src/lib/Request.js +0 -42
  62. package/dist/src/plugins/commander/config.js +0 -11
  63. package/dist/src/plugins/commander/index.js +0 -21
  64. package/dist/src/plugins/commander/init.js +0 -122
  65. package/dist/src/plugins/commander/pluginHandler.js +0 -43
  66. package/dist/src/plugins/commander/proxy.js +0 -14
  67. package/dist/src/plugins/commander/setting.js +0 -102
  68. package/dist/src/plugins/commander/upload.js +0 -35
  69. package/dist/src/plugins/commander/use.js +0 -87
  70. package/dist/src/plugins/transformer/base64.js +0 -9
  71. package/dist/src/plugins/transformer/index.js +0 -16
  72. package/dist/src/plugins/transformer/path.js +0 -42
  73. package/dist/src/plugins/uploader/aliyun.d.ts +0 -7
  74. package/dist/src/plugins/uploader/aliyun.js +0 -129
  75. package/dist/src/plugins/uploader/github.d.ts +0 -7
  76. package/dist/src/plugins/uploader/github.js +0 -101
  77. package/dist/src/plugins/uploader/imgur.d.ts +0 -7
  78. package/dist/src/plugins/uploader/imgur.js +0 -81
  79. package/dist/src/plugins/uploader/index.js +0 -26
  80. package/dist/src/plugins/uploader/qiniu.d.ts +0 -7
  81. package/dist/src/plugins/uploader/qiniu.js +0 -132
  82. package/dist/src/plugins/uploader/smms.d.ts +0 -7
  83. package/dist/src/plugins/uploader/smms.js +0 -74
  84. package/dist/src/plugins/uploader/tcyun.d.ts +0 -13
  85. package/dist/src/plugins/uploader/tcyun.js +0 -218
  86. package/dist/src/plugins/uploader/upyun.d.ts +0 -7
  87. package/dist/src/plugins/uploader/upyun.js +0 -128
  88. package/dist/src/types/index.d.ts +0 -471
  89. package/dist/src/utils/clipboard/linux.sh +0 -18
  90. package/dist/src/utils/clipboard/mac.applescript +0 -0
  91. package/dist/src/utils/clipboard/windows.ps1 +0 -26
  92. package/dist/src/utils/clipboard/windows10.ps1 +0 -47
  93. package/dist/src/utils/clipboard/wsl.sh +0 -18
  94. package/dist/src/utils/common.js +0 -353
  95. package/dist/src/utils/createContext.js +0 -45
  96. package/dist/src/utils/db.js +0 -85
  97. package/dist/src/utils/enum.js +0 -33
  98. package/dist/src/utils/eventBus.js +0 -6
  99. package/dist/src/utils/getClipboardImage.js +0 -101
  100. package/dist/src/utils/initUtils.js +0 -120
  101. package/dist/src/utils/interfaces.js +0 -3
@@ -1,101 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const enum_1 = require("../../utils/enum");
4
- const postOptions = (fileName, options, data) => {
5
- const path = options.path || '';
6
- const { token, repo } = options;
7
- return {
8
- method: 'PUT',
9
- url: `https://api.github.com/repos/${repo}/contents/${encodeURI(path)}${encodeURI(fileName)}`,
10
- headers: {
11
- Authorization: `token ${token}`,
12
- 'User-Agent': 'PicGo'
13
- },
14
- body: data,
15
- json: true
16
- };
17
- };
18
- const handle = async (ctx) => {
19
- const githubOptions = ctx.getConfig('picBed.github');
20
- if (!githubOptions) {
21
- throw new Error('Can\'t find github config');
22
- }
23
- try {
24
- const imgList = ctx.output;
25
- for (const img of imgList) {
26
- if (img.fileName && img.buffer) {
27
- const base64Image = img.base64Image || Buffer.from(img.buffer).toString('base64');
28
- const data = {
29
- message: 'Upload by PicGo',
30
- branch: githubOptions.branch,
31
- content: base64Image,
32
- path: githubOptions.path + encodeURI(img.fileName)
33
- };
34
- const postConfig = postOptions(img.fileName, githubOptions, data);
35
- const body = await ctx.Request.request(postConfig);
36
- if (body) {
37
- delete img.base64Image;
38
- delete img.buffer;
39
- if (githubOptions.customUrl) {
40
- img.imgUrl = `${githubOptions.customUrl}/${githubOptions.path}${img.fileName}`;
41
- }
42
- else {
43
- img.imgUrl = body.content.download_url;
44
- }
45
- }
46
- else {
47
- throw new Error('Server error, please try again');
48
- }
49
- }
50
- }
51
- return ctx;
52
- }
53
- catch (err) {
54
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
55
- title: '上传失败',
56
- body: '服务端出错,请重试'
57
- });
58
- throw err;
59
- }
60
- };
61
- const config = (ctx) => {
62
- const userConfig = ctx.getConfig('picBed.github') || {};
63
- const config = [
64
- {
65
- name: 'repo',
66
- type: 'input',
67
- default: userConfig.repo || '',
68
- required: true
69
- },
70
- {
71
- name: 'branch',
72
- type: 'input',
73
- default: userConfig.branch || 'master',
74
- required: true
75
- },
76
- {
77
- name: 'token',
78
- type: 'input',
79
- default: userConfig.token || '',
80
- required: true
81
- },
82
- {
83
- name: 'path',
84
- type: 'input',
85
- default: userConfig.path || '',
86
- required: false
87
- },
88
- {
89
- name: 'customUrl',
90
- type: 'input',
91
- default: userConfig.customUrl || '',
92
- required: false
93
- }
94
- ];
95
- return config;
96
- };
97
- exports.default = {
98
- name: 'GitHub图床',
99
- handle,
100
- config
101
- };
@@ -1,7 +0,0 @@
1
- import { IPicGo, IPluginConfig } from '../../types';
2
- declare const _default: {
3
- name: string;
4
- handle: (ctx: IPicGo) => Promise<IPicGo>;
5
- config: (ctx: IPicGo) => IPluginConfig[];
6
- };
7
- export default _default;
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const enum_1 = require("../../utils/enum");
4
- const postOptions = (options, fileName, imgBase64) => {
5
- const clientId = options.clientId;
6
- const obj = {
7
- method: 'POST',
8
- url: 'https://api.imgur.com/3/image',
9
- headers: {
10
- Authorization: `Client-ID ${clientId}`,
11
- 'content-type': 'multipart/form-data',
12
- 'User-Agent': 'PicGo'
13
- },
14
- formData: {
15
- image: imgBase64,
16
- type: 'base64',
17
- name: fileName
18
- }
19
- };
20
- if (options.proxy) {
21
- obj.proxy = options.proxy;
22
- }
23
- return obj;
24
- };
25
- const handle = async (ctx) => {
26
- const imgurOptions = ctx.getConfig('picBed.imgur');
27
- if (!imgurOptions) {
28
- throw new Error('Can\'t find imgur config');
29
- }
30
- try {
31
- const imgList = ctx.output;
32
- for (const img of imgList) {
33
- if (img.fileName && img.buffer) {
34
- const base64Image = img.base64Image || Buffer.from(img.buffer).toString('base64');
35
- const options = postOptions(imgurOptions, img.fileName, base64Image);
36
- let body = await ctx.Request.request(options);
37
- body = JSON.parse(body);
38
- if (body.success) {
39
- delete img.base64Image;
40
- delete img.buffer;
41
- img.imgUrl = body.data.link;
42
- }
43
- else {
44
- throw new Error('Server error, please try again');
45
- }
46
- }
47
- }
48
- return ctx;
49
- }
50
- catch (err) {
51
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
52
- title: '上传失败',
53
- body: '请检查你的配置以及网络',
54
- text: 'http://docs.imgur.com/api/errno/'
55
- });
56
- throw err;
57
- }
58
- };
59
- const config = (ctx) => {
60
- const userConfig = ctx.getConfig('picBed.imgur') || {};
61
- const config = [
62
- {
63
- name: 'clientId',
64
- type: 'input',
65
- default: userConfig.clientId || '',
66
- required: true
67
- },
68
- {
69
- name: 'proxy',
70
- type: 'input',
71
- default: userConfig.proxy || '',
72
- required: false
73
- }
74
- ];
75
- return config;
76
- };
77
- exports.default = {
78
- name: 'Imgur图床',
79
- handle,
80
- config
81
- };
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const smms_1 = __importDefault(require("./smms"));
7
- const tcyun_1 = __importDefault(require("./tcyun"));
8
- const github_1 = __importDefault(require("./github"));
9
- const qiniu_1 = __importDefault(require("./qiniu"));
10
- const imgur_1 = __importDefault(require("./imgur"));
11
- const aliyun_1 = __importDefault(require("./aliyun"));
12
- const upyun_1 = __importDefault(require("./upyun"));
13
- const buildInUploaders = () => {
14
- return {
15
- register(ctx) {
16
- ctx.helper.uploader.register('smms', smms_1.default);
17
- ctx.helper.uploader.register('tcyun', tcyun_1.default);
18
- ctx.helper.uploader.register('github', github_1.default);
19
- ctx.helper.uploader.register('qiniu', qiniu_1.default);
20
- ctx.helper.uploader.register('imgur', imgur_1.default);
21
- ctx.helper.uploader.register('aliyun', aliyun_1.default);
22
- ctx.helper.uploader.register('upyun', upyun_1.default);
23
- }
24
- };
25
- };
26
- exports.default = buildInUploaders;
@@ -1,7 +0,0 @@
1
- import { IPluginConfig, IPicGo } from '../../types';
2
- declare const _default: {
3
- name: string;
4
- handle: (ctx: IPicGo) => Promise<IPicGo>;
5
- config: (ctx: IPicGo) => IPluginConfig[];
6
- };
7
- export default _default;
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const qiniu_1 = __importDefault(require("qiniu"));
7
- const enum_1 = require("../../utils/enum");
8
- function postOptions(options, fileName, token, imgBase64) {
9
- const area = selectArea(options.area || 'z0');
10
- const path = options.path || '';
11
- const base64FileName = Buffer.from(path + fileName, 'utf-8').toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
12
- return {
13
- method: 'POST',
14
- url: `http://upload${area}.qiniu.com/putb64/-1/key/${base64FileName}`,
15
- headers: {
16
- Authorization: `UpToken ${token}`,
17
- contentType: 'application/octet-stream'
18
- },
19
- body: imgBase64
20
- };
21
- }
22
- function selectArea(area) {
23
- return area === 'z0' ? '' : '-' + area;
24
- }
25
- function getToken(qiniuOptions) {
26
- const accessKey = qiniuOptions.accessKey;
27
- const secretKey = qiniuOptions.secretKey;
28
- const mac = new qiniu_1.default.auth.digest.Mac(accessKey, secretKey);
29
- const options = {
30
- scope: qiniuOptions.bucket
31
- };
32
- const putPolicy = new qiniu_1.default.rs.PutPolicy(options);
33
- return putPolicy.uploadToken(mac);
34
- }
35
- const handle = async (ctx) => {
36
- const qiniuOptions = ctx.getConfig('picBed.qiniu');
37
- if (!qiniuOptions) {
38
- throw new Error('Can\'t find qiniu config');
39
- }
40
- try {
41
- const imgList = ctx.output;
42
- for (const img of imgList) {
43
- if (img.fileName && img.buffer) {
44
- const base64Image = img.base64Image || Buffer.from(img.buffer).toString('base64');
45
- const options = postOptions(qiniuOptions, img.fileName, getToken(qiniuOptions), base64Image);
46
- const res = await ctx.Request.request(options);
47
- const body = JSON.parse(res);
48
- if (body === null || body === void 0 ? void 0 : body.key) {
49
- delete img.base64Image;
50
- delete img.buffer;
51
- const baseUrl = qiniuOptions.url;
52
- const options = qiniuOptions.options;
53
- img.imgUrl = `${baseUrl}/${body.key}${options}`;
54
- }
55
- else {
56
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
57
- title: '上传失败',
58
- body: res.body.msg
59
- });
60
- throw new Error('Upload failed');
61
- }
62
- }
63
- }
64
- return ctx;
65
- }
66
- catch (err) {
67
- if (err.message !== 'Upload failed') {
68
- // err.response maybe undefined
69
- if (err.response) {
70
- const error = JSON.parse(err.response.body || '{}');
71
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
72
- title: '上传失败',
73
- body: error.error
74
- });
75
- }
76
- }
77
- throw err;
78
- }
79
- };
80
- const config = (ctx) => {
81
- const userConfig = ctx.getConfig('picBed.qiniu') || {};
82
- const config = [
83
- {
84
- name: 'accessKey',
85
- type: 'input',
86
- default: userConfig.accessKey || '',
87
- required: true
88
- },
89
- {
90
- name: 'secretKey',
91
- type: 'input',
92
- default: userConfig.secretKey || '',
93
- required: true
94
- },
95
- {
96
- name: 'bucket',
97
- type: 'input',
98
- default: userConfig.bucket || '',
99
- required: true
100
- },
101
- {
102
- name: 'url',
103
- type: 'input',
104
- default: userConfig.url || '',
105
- required: true
106
- },
107
- {
108
- name: 'area',
109
- type: 'input',
110
- default: userConfig.area || '',
111
- required: true
112
- },
113
- {
114
- name: 'options',
115
- type: 'input',
116
- default: userConfig.options || '',
117
- required: false
118
- },
119
- {
120
- name: 'path',
121
- type: 'input',
122
- default: userConfig.path || '',
123
- required: false
124
- }
125
- ];
126
- return config;
127
- };
128
- exports.default = {
129
- name: '七牛图床',
130
- handle,
131
- config
132
- };
@@ -1,7 +0,0 @@
1
- import { IPicGo, IPluginConfig } from '../../types';
2
- declare const _default: {
3
- name: string;
4
- handle: (ctx: IPicGo) => Promise<IPicGo>;
5
- config: (ctx: IPicGo) => IPluginConfig[];
6
- };
7
- export default _default;
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const enum_1 = require("../../utils/enum");
4
- const postOptions = (fileName, image, apiToken) => {
5
- return {
6
- method: 'POST',
7
- url: 'https://sm.ms/api/v2/upload',
8
- headers: {
9
- contentType: 'multipart/form-data',
10
- 'User-Agent': 'PicGo',
11
- Authorization: apiToken
12
- },
13
- formData: {
14
- smfile: {
15
- value: image,
16
- options: {
17
- filename: fileName
18
- }
19
- },
20
- ssl: 'true'
21
- }
22
- };
23
- };
24
- const handle = async (ctx) => {
25
- const smmsConfig = ctx.getConfig('picBed.smms');
26
- const imgList = ctx.output;
27
- for (const img of imgList) {
28
- if (img.fileName && img.buffer) {
29
- let image = img.buffer;
30
- if (!image && img.base64Image) {
31
- image = Buffer.from(img.base64Image, 'base64');
32
- }
33
- const postConfig = postOptions(img.fileName, image, smmsConfig === null || smmsConfig === void 0 ? void 0 : smmsConfig.token);
34
- let body = await ctx.Request.request(postConfig);
35
- body = JSON.parse(body);
36
- if (body.code === 'success') {
37
- delete img.base64Image;
38
- delete img.buffer;
39
- img.imgUrl = body.data.url;
40
- }
41
- else if (body.code === 'image_repeated' && typeof body.images === 'string') { // do extra check since this error return is not documented at https://doc.sm.ms/#api-Image-Upload
42
- delete img.base64Image;
43
- delete img.buffer;
44
- img.imgUrl = body.images;
45
- }
46
- else {
47
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
48
- title: '上传失败',
49
- body: body.message
50
- });
51
- throw new Error(body.message);
52
- }
53
- }
54
- }
55
- return ctx;
56
- };
57
- const config = (ctx) => {
58
- const userConfig = ctx.getConfig('picBed.smms') || {};
59
- const config = [
60
- {
61
- name: 'token',
62
- message: 'api token',
63
- type: 'input',
64
- default: userConfig.token || '',
65
- required: true
66
- }
67
- ];
68
- return config;
69
- };
70
- exports.default = {
71
- name: 'SM.MS图床',
72
- handle,
73
- config
74
- };
@@ -1,13 +0,0 @@
1
- import { IPicGo, IPluginConfig } from '../../types';
2
- export interface ISignature {
3
- signature: string;
4
- appId: string;
5
- bucket: string;
6
- signTime: string;
7
- }
8
- declare const _default: {
9
- name: string;
10
- handle: (ctx: IPicGo) => Promise<boolean | IPicGo>;
11
- config: (ctx: IPicGo) => IPluginConfig[];
12
- };
13
- export default _default;
@@ -1,218 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const crypto_1 = __importDefault(require("crypto"));
7
- const mime_types_1 = __importDefault(require("mime-types"));
8
- const enum_1 = require("../../utils/enum");
9
- const generateSignature = (options, fileName) => {
10
- const secretId = options.secretId;
11
- const secretKey = options.secretKey;
12
- const appId = options.appId;
13
- const bucket = options.bucket;
14
- let signature;
15
- let signTime = '';
16
- if (!options.version || options.version === 'v4') {
17
- const random = Math.floor(Math.random() * 10000000000);
18
- const current = Math.floor(new Date().getTime() / 1000) - 1;
19
- const expired = current + 3600;
20
- const multiSignature = `a=${appId}&b=${bucket}&k=${secretId}&e=${expired}&t=${current}&r=${random}&f=`;
21
- const signHexKey = crypto_1.default.createHmac('sha1', secretKey).update(multiSignature).digest();
22
- const tempString = Buffer.concat([signHexKey, Buffer.from(multiSignature)]);
23
- signature = Buffer.from(tempString).toString('base64');
24
- }
25
- else {
26
- // https://cloud.tencent.com/document/product/436/7778#signature
27
- const today = Math.floor(new Date().getTime() / 1000);
28
- const tomorrow = today + 86400;
29
- signTime = `${today};${tomorrow}`;
30
- const signKey = crypto_1.default.createHmac('sha1', secretKey).update(signTime).digest('hex');
31
- const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.cos.${options.area}.myqcloud.com\n`;
32
- const sha1edHttpString = crypto_1.default.createHash('sha1').update(httpString).digest('hex');
33
- const stringToSign = `sha1\n${signTime}\n${sha1edHttpString}\n`;
34
- signature = crypto_1.default.createHmac('sha1', signKey).update(stringToSign).digest('hex');
35
- }
36
- return {
37
- signature,
38
- appId,
39
- bucket,
40
- signTime
41
- };
42
- };
43
- const postOptions = (options, fileName, signature, image) => {
44
- const area = options.area;
45
- const path = options.path;
46
- if (!options.version || options.version === 'v4') {
47
- return {
48
- method: 'POST',
49
- url: `http://${area}.file.myqcloud.com/files/v2/${signature.appId}/${signature.bucket}/${encodeURI(path)}${fileName}`,
50
- headers: {
51
- Host: `${area}.file.myqcloud.com`,
52
- Authorization: signature.signature,
53
- contentType: 'multipart/form-data'
54
- },
55
- formData: {
56
- op: 'upload',
57
- filecontent: image
58
- }
59
- };
60
- }
61
- else {
62
- return {
63
- method: 'PUT',
64
- url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${encodeURI(path)}${encodeURI(fileName)}`,
65
- headers: {
66
- Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
67
- Authorization: `q-sign-algorithm=sha1&q-ak=${options.secretId}&q-sign-time=${signature.signTime}&q-key-time=${signature.signTime}&q-header-list=host&q-url-param-list=&q-signature=${signature.signature}`,
68
- contentType: mime_types_1.default.lookup(fileName)
69
- },
70
- body: image,
71
- resolveWithFullResponse: true
72
- };
73
- }
74
- };
75
- const handle = async (ctx) => {
76
- const tcYunOptions = ctx.getConfig('picBed.tcyun');
77
- if (!tcYunOptions) {
78
- throw new Error('Can\'t find tencent COS config');
79
- }
80
- try {
81
- const imgList = ctx.output;
82
- const customUrl = tcYunOptions.customUrl;
83
- const path = tcYunOptions.path;
84
- const useV4 = !tcYunOptions.version || tcYunOptions.version === 'v4';
85
- for (const img of imgList) {
86
- if (img.fileName && img.buffer) {
87
- const signature = generateSignature(tcYunOptions, img.fileName);
88
- if (!signature) {
89
- return false;
90
- }
91
- let image = img.buffer;
92
- if (!image && img.base64Image) {
93
- image = Buffer.from(img.base64Image, 'base64');
94
- }
95
- const options = postOptions(tcYunOptions, img.fileName, signature, image);
96
- const res = await ctx.Request.request(options)
97
- .then((res) => res)
98
- .catch((err) => {
99
- ctx.log.error(err);
100
- return {
101
- statusCode: 400,
102
- body: {
103
- msg: '认证失败!'
104
- }
105
- };
106
- });
107
- let body;
108
- if (useV4 && typeof res === 'string') {
109
- body = JSON.parse(res);
110
- }
111
- else {
112
- body = res;
113
- }
114
- if (body.statusCode === 400) {
115
- throw new Error(body.msg || body.message);
116
- }
117
- if (useV4 && body.message === 'SUCCESS') {
118
- delete img.base64Image;
119
- delete img.buffer;
120
- if (customUrl) {
121
- img.imgUrl = `${customUrl}/${path}${img.fileName}`;
122
- }
123
- else {
124
- img.imgUrl = body.data.source_url;
125
- }
126
- }
127
- else if (!useV4 && body && body.statusCode === 200) {
128
- delete img.base64Image;
129
- delete img.buffer;
130
- if (customUrl) {
131
- img.imgUrl = `${customUrl}/${path}${img.fileName}`;
132
- }
133
- else {
134
- img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${path}${img.fileName}`;
135
- }
136
- }
137
- else {
138
- throw new Error(res.body.msg);
139
- }
140
- }
141
- }
142
- return ctx;
143
- }
144
- catch (err) {
145
- if (!tcYunOptions.version || tcYunOptions.version === 'v4') {
146
- try {
147
- const body = JSON.parse(err.error);
148
- ctx.emit(enum_1.IBuildInEvent.NOTIFICATION, {
149
- title: '上传失败',
150
- body: `错误码:${body.code},请打开浏览器粘贴地址查看相关原因`,
151
- text: 'https://cloud.tencent.com/document/product/436/8432'
152
- });
153
- }
154
- catch (e) { }
155
- }
156
- throw err;
157
- }
158
- };
159
- const config = (ctx) => {
160
- const userConfig = ctx.getConfig('picBed.tcyun') || {};
161
- const config = [
162
- {
163
- name: 'secretId',
164
- type: 'input',
165
- default: userConfig.secretId || '',
166
- required: true
167
- },
168
- {
169
- name: 'secretKey',
170
- type: 'input',
171
- default: userConfig.secretKey || '',
172
- required: true
173
- },
174
- {
175
- name: 'bucket',
176
- type: 'input',
177
- default: userConfig.bucket || '',
178
- required: true
179
- },
180
- {
181
- name: 'appId',
182
- type: 'input',
183
- default: userConfig.appId || '',
184
- required: true
185
- },
186
- {
187
- name: 'area',
188
- type: 'input',
189
- default: userConfig.area || '',
190
- required: true
191
- },
192
- {
193
- name: 'path',
194
- type: 'input',
195
- default: userConfig.path || '',
196
- required: false
197
- },
198
- {
199
- name: 'customUrl',
200
- type: 'input',
201
- default: userConfig.customUrl || '',
202
- required: false
203
- },
204
- {
205
- name: 'version',
206
- type: 'list',
207
- choices: ['v4', 'v5'],
208
- default: 'v5',
209
- required: false
210
- }
211
- ];
212
- return config;
213
- };
214
- exports.default = {
215
- name: '腾讯云COS',
216
- handle,
217
- config
218
- };
@@ -1,7 +0,0 @@
1
- import { IPicGo, IPluginConfig } from '../../types';
2
- declare const _default: {
3
- name: string;
4
- handle: (ctx: IPicGo) => Promise<IPicGo>;
5
- config: (ctx: IPicGo) => IPluginConfig[];
6
- };
7
- export default _default;