-
Notifications
You must be signed in to change notification settings - Fork 84
/
tcyun.ts
292 lines (282 loc) · 10.8 KB
/
tcyun.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import crypto from 'crypto'
import mime from 'mime-types'
import { IPicGo, IPluginConfig, ITcyunConfig, IOldReqOptionsWithFullResponse } from '../../types'
import { IBuildInEvent } from '../../utils/enum'
import { ILocalesKey } from '../../i18n/zh-CN'
// generate COS signature string
export interface ISignature {
signature: string
appId: string
bucket: string
signTime: string
}
const generateSignature = (options: ITcyunConfig, fileName: string): ISignature => {
const secretId = options.secretId
const secretKey = options.secretKey
const appId = options.appId
const bucket = options.bucket
let signature
let signTime: string = ''
if (!options.version || options.version === 'v4') {
const random = Math.floor(Math.random() * 10000000000)
const current = Math.floor(new Date().getTime() / 1000) - 1
const expired = current + 3600
const multiSignature = `a=${appId}&b=${bucket}&k=${secretId}&e=${expired}&t=${current}&r=${random}&f=`
const signHexKey = crypto.createHmac('sha1', secretKey).update(multiSignature).digest()
const tempString = Buffer.concat([signHexKey, Buffer.from(multiSignature)])
signature = Buffer.from(tempString).toString('base64')
} else {
// https://cloud.tencent.com/document/product/436/7778#signature
const today = Math.floor(new Date().getTime() / 1000)
const tomorrow = today + 86400
signTime = `${today};${tomorrow}`
const signKey = crypto.createHmac('sha1', secretKey).update(signTime).digest('hex')
const endpoint = options.endpoint ? options.endpoint : `cos.${options.area}.myqcloud.com`
const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.${endpoint}\n`
const sha1edHttpString = crypto.createHash('sha1').update(httpString).digest('hex')
const stringToSign = `sha1\n${signTime}\n${sha1edHttpString}\n`
signature = crypto.createHmac('sha1', signKey).update(stringToSign).digest('hex')
}
return {
signature,
appId,
bucket,
signTime
}
}
const postOptions = (options: ITcyunConfig, fileName: string, signature: ISignature, image: Buffer, version: string): IOldReqOptionsWithFullResponse => {
const area = options.area
const path = options.path
if (!options.version || options.version === 'v4') {
return {
method: 'POST',
url: `http://${area}.file.myqcloud.com/files/v2/${signature.appId}/${signature.bucket}/${encodeURI(path)}${fileName}`,
headers: {
Host: `${area}.file.myqcloud.com`,
Authorization: signature.signature,
contentType: 'multipart/form-data',
'User-Agent': `PicGo;${version};null;null`
},
formData: {
op: 'upload',
filecontent: image
},
resolveWithFullResponse: true
}
} else {
// https://cloud.tencent.com/document/product/436/10976
const endpoint = options.endpoint ? options.endpoint : `cos.${options.area}.myqcloud.com`
return {
method: 'PUT',
url: `http://${options.bucket}.${endpoint}/${encodeURI(path)}${encodeURIComponent(fileName)}`,
headers: {
Host: `${options.bucket}.${endpoint}`,
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}`,
contentType: mime.lookup(fileName),
'User-Agent': `PicGo;${version};null;null`
},
body: image,
resolveWithFullResponse: true
}
}
}
const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
const tcYunOptions = ctx.getConfig<ITcyunConfig>('picBed.tcyun')
if (!tcYunOptions) {
throw new Error('Can\'t find tencent COS config')
}
try {
const imgList = ctx.output
const customUrl = tcYunOptions.customUrl
const path = tcYunOptions.path
const useV4 = !tcYunOptions.version || tcYunOptions.version === 'v4'
for (const img of imgList) {
if (img.fileName && img.buffer) {
const signature = generateSignature(tcYunOptions, img.fileName)
if (!signature) {
return false
}
let image = img.buffer
if (!image && img.base64Image) {
image = Buffer.from(img.base64Image, 'base64')
}
const options = postOptions(tcYunOptions, img.fileName, signature, image, ctx.GUI_VERSION || ctx.VERSION)
const res = await ctx.request(options)
.then((res: any) => res)
.catch((err: Error) => {
return {
statusCode: 400,
body: {
msg: ctx.i18n.translate<ILocalesKey>('AUTH_FAILED'),
err
}
}
})
let body
if (useV4 && typeof res === 'string') {
body = JSON.parse(res)
} else {
body = res
}
if (body.statusCode === 400) {
if (body?.body?.err) {
throw body.body.err
} else {
throw new Error(body?.body?.msg || body?.body?.message)
}
}
const optionUrl = tcYunOptions.options || ''
const slim = tcYunOptions.slim || ''
if (useV4 && body.message === 'SUCCESS') {
delete img.base64Image
delete img.buffer
if (customUrl) {
img.imgUrl = `${customUrl}/${path}${img.fileName}`
} else {
img.imgUrl = `${body.data.source_url as string}${optionUrl}`
}
} else if (!useV4 && body && body.statusCode === 200) {
delete img.base64Image
delete img.buffer
if (customUrl) {
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
} else {
const endpoint = tcYunOptions.endpoint ? tcYunOptions.endpoint : `cos.${tcYunOptions.area}.myqcloud.com`
img.imgUrl = `https://${tcYunOptions.bucket}.${endpoint}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
}
} else {
throw new Error(res.body.msg)
}
if (slim) {
if (optionUrl) {
img.imgUrl += '&imageSlim'
} else {
img.imgUrl += '?imageSlim'
}
}
}
}
return ctx
} catch (err: any) {
if (!tcYunOptions.version || tcYunOptions.version === 'v4') {
try {
const body = JSON.parse(err.error)
ctx.emit(IBuildInEvent.NOTIFICATION, {
title: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED'),
body: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED_REASON', {
code: body.code as string
}),
text: 'https://cloud.tencent.com/document/product/436/8432'
})
} catch (e) {}
}
throw err
}
}
const config = (ctx: IPicGo): IPluginConfig[] => {
const userConfig = ctx.getConfig<ITcyunConfig>('picBed.tcyun') || {}
const config: IPluginConfig[] = [
{
name: 'version',
type: 'list',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_VERSION'),
choices: ['v4', 'v5'],
default: 'v5',
required: false
},
{
name: 'secretId',
type: 'input',
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SECRETID') },
default: userConfig.secretId || '',
required: true
},
{
name: 'secretKey',
type: 'password',
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SECRETKEY') },
default: userConfig.secretKey || '',
required: true
},
{
name: 'bucket',
type: 'input',
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_BUCKET') },
default: userConfig.bucket || '',
required: true
},
{
name: 'appId',
type: 'input',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_APPID') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_APPID') },
default: userConfig.appId || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_APPID') },
required: true
},
{
name: 'area',
type: 'input',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_AREA') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_AREA') },
default: userConfig.area || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_AREA') },
required: true
},
{
name: 'endpoint',
type: 'input',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_ENDPOINT') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_ENDPOINT') },
default: userConfig.endpoint || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT') },
required: false
},
{
name: 'path',
type: 'input',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_PATH') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_PATH') },
default: userConfig.path || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_PATH') },
required: false
},
{
name: 'customUrl',
type: 'input',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_CUSTOMURL') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_CUSTOMURL') },
default: userConfig.customUrl || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL') },
required: false
},
{
name: 'options',
type: 'input',
default: userConfig.options || '',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_OPTIONS') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_OPTIONS') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_OPTIONS') },
required: false
},
{
name: 'slim',
type: 'confirm',
default: userConfig.options || '',
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM') },
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM') },
required: false,
get confirmText () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_CONFIRM') },
get cancelText () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_CANCEL') },
get tips () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_TIP') }
}
]
return config
}
export default function register (ctx: IPicGo): void {
ctx.helper.uploader.register('tcyun', {
get name () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD') },
handle,
config
})
}