diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 92dbad1..988186a 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -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', diff --git a/src/i18n/zh-CN.ts b/src/i18n/zh-CN.ts index 562ffb0..608e3a7 100644 --- a/src/i18n/zh-CN.ts +++ b/src/i18n/zh-CN.ts @@ -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', diff --git a/src/i18n/zh-TW.ts b/src/i18n/zh-TW.ts index e032c2c..9dca25b 100644 --- a/src/i18n/zh-TW.ts +++ b/src/i18n/zh-TW.ts @@ -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', diff --git a/src/plugins/uploader/tcyun.ts b/src/plugins/uploader/tcyun.ts index bbfb98a..c01001e 100644 --- a/src/plugins/uploader/tcyun.ts +++ b/src/plugins/uploader/tcyun.ts @@ -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') @@ -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` @@ -144,7 +148,8 @@ const handle = async (ctx: IPicGo): Promise => { 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) @@ -219,6 +224,15 @@ const config = (ctx: IPicGo): IPluginConfig[] => { get message () { return ctx.i18n.translate('PICBED_TENCENTCLOUD_MESSAGE_AREA') }, required: true }, + { + name: 'endpoint', + type: 'input', + get prefix () { return ctx.i18n.translate('PICBED_TENCENTCLOUD_ENDPOINT') }, + get alias () { return ctx.i18n.translate('PICBED_TENCENTCLOUD_ENDPOINT') }, + default: userConfig.endpoint || '', + get message () { return ctx.i18n.translate('PICBED_TENCENTCLOUD_MESSAGE_ENDPOINT') }, + required: false + }, { name: 'path', type: 'input', diff --git a/src/types/index.ts b/src/types/index.ts index 75b739e..aee888e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -321,6 +321,8 @@ export interface ITcyunConfig { appId: string /** 存储区域,例如 ap-beijing-1 */ area: string + /** 请求的 ENDPOINT,设置后 `area` 字段会失效 */ + endpoint: string /** 自定义存储路径,比如 img/ */ path: string /** 自定义域名,注意要加 `http://` 或者 `https://` */