Skip to content

Commit

Permalink
✨ Feature: support ENDPOINT for Tencent COS (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
palmcivet authored Aug 22, 2023
1 parent 35cc8b7 commit 42494ac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export const EN: ILocales = {
PICBED_TENCENTCLOUD_APPID: 'Set AppId',
PICBED_TENCENTCLOUD_BUCKET: 'Set Bucket',
PICBED_TENCENTCLOUD_AREA: 'Set Area',
PICBED_TENCENTCLOUD_ENDPOINT: 'Set Endpoint',
PICBED_TENCENTCLOUD_PATH: 'Set Path',
PICBED_TENCENTCLOUD_OPTIONS: 'Set URL Suffix',
PICBED_TENCENTCLOUD_CUSTOMURL: 'Set Custom URL',
PICBED_TENCENTCLOUD_MESSAGE_APPID: 'Ex. 1234567890',
PICBED_TENCENTCLOUD_MESSAGE_AREA: 'Ex. ap-beijing',
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: 'Ex. cos-internal.accelerate.tencentcos.cn',
PICBED_TENCENTCLOUD_MESSAGE_PATH: 'Ex. test/',
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: 'Ex. http://test.com',
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: 'Ex. ?imageMogr2',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ export const ZH_CN = {
PICBED_TENCENTCLOUD_APPID: '设定AppId',
PICBED_TENCENTCLOUD_BUCKET: '设定Bucket',
PICBED_TENCENTCLOUD_AREA: '设定存储区域',
PICBED_TENCENTCLOUD_ENDPOINT: '设定Endpoint',
PICBED_TENCENTCLOUD_PATH: '设定存储路径',
PICBED_TENCENTCLOUD_OPTIONS: '设定网址后缀',
PICBED_TENCENTCLOUD_CUSTOMURL: '设定自定义域名',
PICBED_TENCENTCLOUD_MESSAGE_APPID: '例如:1234567890',
PICBED_TENCENTCLOUD_MESSAGE_AREA: '例如:ap-beijing',
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: '例如:cos-internal.accelerate.tencentcos.cn',
PICBED_TENCENTCLOUD_MESSAGE_PATH: '例如:test/',
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: '例如:?imageMogr2',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export const ZH_TW: ILocales = {
PICBED_TENCENTCLOUD_APPID: '設定AppId',
PICBED_TENCENTCLOUD_BUCKET: '設定Bucket',
PICBED_TENCENTCLOUD_AREA: '設定儲存區域',
PICBED_TENCENTCLOUD_ENDPOINT: '設定Endpoint',
PICBED_TENCENTCLOUD_PATH: '設定儲存路徑',
PICBED_TENCENTCLOUD_OPTIONS: '設定網址後綴',
PICBED_TENCENTCLOUD_CUSTOMURL: '設定自訂網址',
PICBED_TENCENTCLOUD_MESSAGE_APPID: '例如:1234567890',
PICBED_TENCENTCLOUD_MESSAGE_AREA: '例如:ap-beijing',
PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT: '例如:cos-internal.accelerate.tencentcos.cn',
PICBED_TENCENTCLOUD_MESSAGE_PATH: '例如:test/',
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: '例如:?imageMogr2',
Expand Down
22 changes: 18 additions & 4 deletions src/plugins/uploader/tcyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const generateSignature = (options: ITcyunConfig, fileName: string): ISignature
const tomorrow = today + 86400
signTime = `${today};${tomorrow}`
const signKey = crypto.createHmac('sha1', secretKey).update(signTime).digest('hex')
const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.cos.${options.area}.myqcloud.com\n`
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')
Expand Down Expand Up @@ -69,11 +70,14 @@ const postOptions = (options: ITcyunConfig, fileName: string, signature: ISignat
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}.cos.${options.area}.myqcloud.com/${encodeURIComponent(path)}${encodeURIComponent(fileName)}`,
url: `http://${options.bucket}.${endpoint}/${encodeURIComponent(path)}${encodeURIComponent(fileName)}`,
headers: {
Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
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`
Expand Down Expand Up @@ -144,7 +148,8 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
if (customUrl) {
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
} else {
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
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)
Expand Down Expand Up @@ -219,6 +224,15 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
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',
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ export interface ITcyunConfig {
appId: string
/** 存储区域,例如 ap-beijing-1 */
area: string
/** 请求的 ENDPOINT,设置后 `area` 字段会失效 */
endpoint: string
/** 自定义存储路径,比如 img/ */
path: string
/** 自定义域名,注意要加 `http://` 或者 `https://` */
Expand Down

0 comments on commit 42494ac

Please sign in to comment.