@@ -3,6 +3,7 @@ import mime from 'mime-types'
33import { IPicGo , IPluginConfig , ITcyunConfig , IOldReqOptionsWithFullResponse } from '../../types'
44import { IBuildInEvent } from '../../utils/enum'
55import { ILocalesKey } from '../../i18n/zh-CN'
6+ import { handleUrlPathSafeEncode } from '../../utils/common'
67
78// generate COS signature string
89
@@ -14,6 +15,9 @@ export interface ISignature {
1415}
1516
1617const cosSafeUrlEncode = ( str : string ) : string => {
18+ // this must be encodeURIComponent instead of handleUrlEncode
19+ // cause this url encode is for content type
20+ // for example, application/json -> application%2Fjson
1721 return encodeURIComponent ( str )
1822 . replace ( / ! / g, '%21' )
1923 . replace ( / ' / g, '%27' )
@@ -69,7 +73,7 @@ const postOptions = (options: ITcyunConfig, fileName: string, signature: ISignat
6973 if ( ! options . version || options . version === 'v4' ) {
7074 return {
7175 method : 'POST' ,
72- url : `http://${ area } .file.myqcloud.com/files/v2/${ signature . appId } /${ signature . bucket } /${ encodeURI ( path ) } ${ fileName } ` ,
76+ url : `http://${ area } .file.myqcloud.com/files/v2/${ signature . appId } /${ signature . bucket } /${ handleUrlPathSafeEncode ( path , fileName ) } ` ,
7377 headers : {
7478 Host : `${ area } .file.myqcloud.com` ,
7579 Authorization : signature . signature ,
@@ -88,7 +92,7 @@ const postOptions = (options: ITcyunConfig, fileName: string, signature: ISignat
8892
8993 return {
9094 method : 'PUT' ,
91- url : `http://${ options . bucket } .${ endpoint } /${ encodeURI ( path ) } ${ encodeURIComponent ( fileName ) } ` ,
95+ url : `http://${ options . bucket } .${ endpoint } /${ handleUrlPathSafeEncode ( path , fileName ) } ` ,
9296 headers : {
9397 Host : `${ options . bucket } .${ endpoint } ` ,
9498 Authorization : `q-sign-algorithm=sha1&q-ak=${ options . secretId } &q-sign-time=${ signature . signTime } &q-key-time=${ signature . signTime } &q-header-list=content-length;content-type;host&q-url-param-list=&q-signature=${ signature . signature } ` ,
@@ -161,10 +165,10 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
161165 delete img . base64Image
162166 delete img . buffer
163167 if ( customUrl ) {
164- img . imgUrl = `${ customUrl } /${ encodeURI ( path ) } ${ encodeURIComponent ( img . fileName ) } ${ optionUrl } `
168+ img . imgUrl = `${ customUrl } /${ encodeURI ( path ) } ${ handleUrlPathSafeEncode ( img . fileName ) } ${ optionUrl } `
165169 } else {
166170 const endpoint = tcYunOptions . endpoint ? tcYunOptions . endpoint : `cos.${ tcYunOptions . area } .myqcloud.com`
167- img . imgUrl = `https://${ tcYunOptions . bucket } .${ endpoint } /${ encodeURI ( path ) } ${ encodeURIComponent ( img . fileName ) } ${ optionUrl } `
171+ img . imgUrl = `https://${ tcYunOptions . bucket } .${ endpoint } /${ encodeURI ( path ) } ${ handleUrlPathSafeEncode ( img . fileName ) } ${ optionUrl } `
168172 }
169173 } else {
170174 throw new Error ( res . body . msg )
0 commit comments