Skip to content

Commit 42494ac

Browse files
authored
✨ Feature: support ENDPOINT for Tencent COS (#155)
1 parent 35cc8b7 commit 42494ac

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

src/i18n/en.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ export const EN: ILocales = {
3737
PICBED_TENCENTCLOUD_APPID: 'Set AppId',
3838
PICBED_TENCENTCLOUD_BUCKET: 'Set Bucket',
3939
PICBED_TENCENTCLOUD_AREA: 'Set Area',
40+
PICBED_TENCENTCLOUD_ENDPOINT: 'Set Endpoint',
4041
PICBED_TENCENTCLOUD_PATH: 'Set Path',
4142
PICBED_TENCENTCLOUD_OPTIONS: 'Set URL Suffix',
4243
PICBED_TENCENTCLOUD_CUSTOMURL: 'Set Custom URL',
4344
PICBED_TENCENTCLOUD_MESSAGE_APPID: 'Ex. 1234567890',
4445
PICBED_TENCENTCLOUD_MESSAGE_AREA: 'Ex. ap-beijing',
46+
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: 'Ex. cos-internal.accelerate.tencentcos.cn',
4547
PICBED_TENCENTCLOUD_MESSAGE_PATH: 'Ex. test/',
4648
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: 'Ex. http://test.com',
4749
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: 'Ex. ?imageMogr2',

src/i18n/zh-CN.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ export const ZH_CN = {
3535
PICBED_TENCENTCLOUD_APPID: '设定AppId',
3636
PICBED_TENCENTCLOUD_BUCKET: '设定Bucket',
3737
PICBED_TENCENTCLOUD_AREA: '设定存储区域',
38+
PICBED_TENCENTCLOUD_ENDPOINT: '设定Endpoint',
3839
PICBED_TENCENTCLOUD_PATH: '设定存储路径',
3940
PICBED_TENCENTCLOUD_OPTIONS: '设定网址后缀',
4041
PICBED_TENCENTCLOUD_CUSTOMURL: '设定自定义域名',
4142
PICBED_TENCENTCLOUD_MESSAGE_APPID: '例如:1234567890',
4243
PICBED_TENCENTCLOUD_MESSAGE_AREA: '例如:ap-beijing',
44+
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: '例如:cos-internal.accelerate.tencentcos.cn',
4345
PICBED_TENCENTCLOUD_MESSAGE_PATH: '例如:test/',
4446
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',
4547
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: '例如:?imageMogr2',

src/i18n/zh-TW.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ export const ZH_TW: ILocales = {
3737
PICBED_TENCENTCLOUD_APPID: '設定AppId',
3838
PICBED_TENCENTCLOUD_BUCKET: '設定Bucket',
3939
PICBED_TENCENTCLOUD_AREA: '設定儲存區域',
40+
PICBED_TENCENTCLOUD_ENDPOINT: '設定Endpoint',
4041
PICBED_TENCENTCLOUD_PATH: '設定儲存路徑',
4142
PICBED_TENCENTCLOUD_OPTIONS: '設定網址後綴',
4243
PICBED_TENCENTCLOUD_CUSTOMURL: '設定自訂網址',
4344
PICBED_TENCENTCLOUD_MESSAGE_APPID: '例如:1234567890',
4445
PICBED_TENCENTCLOUD_MESSAGE_AREA: '例如:ap-beijing',
46+
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: '例如:cos-internal.accelerate.tencentcos.cn',
4547
PICBED_TENCENTCLOUD_MESSAGE_PATH: '例如:test/',
4648
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',
4749
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: '例如:?imageMogr2',

src/plugins/uploader/tcyun.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const generateSignature = (options: ITcyunConfig, fileName: string): ISignature
3636
const tomorrow = today + 86400
3737
signTime = `${today};${tomorrow}`
3838
const signKey = crypto.createHmac('sha1', secretKey).update(signTime).digest('hex')
39-
const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.cos.${options.area}.myqcloud.com\n`
39+
const endpoint = options.endpoint ? options.endpoint : `cos.${options.area}.myqcloud.com`
40+
const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.${endpoint}\n`
4041
const sha1edHttpString = crypto.createHash('sha1').update(httpString).digest('hex')
4142
const stringToSign = `sha1\n${signTime}\n${sha1edHttpString}\n`
4243
signature = crypto.createHmac('sha1', signKey).update(stringToSign).digest('hex')
@@ -69,11 +70,14 @@ const postOptions = (options: ITcyunConfig, fileName: string, signature: ISignat
6970
resolveWithFullResponse: true
7071
}
7172
} else {
73+
// https://cloud.tencent.com/document/product/436/10976
74+
const endpoint = options.endpoint ? options.endpoint : `cos.${options.area}.myqcloud.com`
75+
7276
return {
7377
method: 'PUT',
74-
url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${encodeURIComponent(path)}${encodeURIComponent(fileName)}`,
78+
url: `http://${options.bucket}.${endpoint}/${encodeURIComponent(path)}${encodeURIComponent(fileName)}`,
7579
headers: {
76-
Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
80+
Host: `${options.bucket}.${endpoint}`,
7781
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}`,
7882
contentType: mime.lookup(fileName),
7983
'User-Agent': `PicGo;${version};null;null`
@@ -144,7 +148,8 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
144148
if (customUrl) {
145149
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
146150
} else {
147-
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
151+
const endpoint = tcYunOptions.endpoint ? tcYunOptions.endpoint : `cos.${tcYunOptions.area}.myqcloud.com`
152+
img.imgUrl = `https://${tcYunOptions.bucket}.${endpoint}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
148153
}
149154
} else {
150155
throw new Error(res.body.msg)
@@ -219,6 +224,15 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
219224
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_AREA') },
220225
required: true
221226
},
227+
{
228+
name: 'endpoint',
229+
type: 'input',
230+
get prefix () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_ENDPOINT') },
231+
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_ENDPOINT') },
232+
default: userConfig.endpoint || '',
233+
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT') },
234+
required: false
235+
},
222236
{
223237
name: 'path',
224238
type: 'input',

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ export interface ITcyunConfig {
321321
appId: string
322322
/** 存储区域,例如 ap-beijing-1 */
323323
area: string
324+
/** 请求的 ENDPOINT,设置后 `area` 字段会失效 */
325+
endpoint: string
324326
/** 自定义存储路径,比如 img/ */
325327
path: string
326328
/** 自定义域名,注意要加 `http://` 或者 `https://` */

0 commit comments

Comments
 (0)