Skip to content

Commit 53d54f8

Browse files
committed
🐛 Fix: url image download bug & tencent cos url encode bug
1 parent 907e6c9 commit 53d54f8

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/plugins/uploader/tcyun.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
142142
delete img.base64Image
143143
delete img.buffer
144144
if (customUrl) {
145-
img.imgUrl = `${customUrl}/${path}${img.fileName}${optionUrl}`
145+
img.imgUrl = `${customUrl}/${encodeURIComponent(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
146146
} else {
147-
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${path}${img.fileName}${optionUrl}`
147+
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURIComponent(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
148148
}
149149
} else {
150150
throw new Error(res.body.msg)

src/types/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,28 @@ export type AxiosResponse<T = any, U = any> = import('axios').AxiosResponse<T, U
189189

190190
export type AxiosRequestConfig<T = any> = import('axios').AxiosRequestConfig<T>
191191

192+
export interface IRequestOptionsWithFullResponse {
193+
resolveWithFullResponse: true
194+
}
195+
196+
export interface IRequestOptionsWithJson {
197+
json: true
198+
}
199+
200+
export interface IRequestOptionsWithResponseTypeArrayBuffer {
201+
responseType: 'arraybuffer'
202+
}
203+
192204
/**
193-
* T is the config type
194-
* U is the response data type
205+
* T is the response data type
206+
* U is the config type
195207
*/
196-
export type IResponse<T, U> = U extends {
197-
resolveWithFullResponse: true
198-
} ? IFullResponse<T, U> : U extends {
199-
json: true
200-
} ? T : string
208+
export type IResponse<T, U> = U extends IRequestOptionsWithFullResponse
209+
? IFullResponse<T, U>
210+
: U extends IRequestOptionsWithJson
211+
? T
212+
: U extends IRequestOptionsWithResponseTypeArrayBuffer ?
213+
Buffer : string
201214

202215
/**
203216
* the old request lib will be removed in v1.5.0+

src/utils/common.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ export const isUrl = (url: string): boolean => (url.startsWith('http://') || url
1515
export const isUrlEncode = (url: string): boolean => {
1616
url = url || ''
1717
try {
18-
return url !== decodeURI(url)
18+
return url !== decodeURIComponent(url)
1919
} catch (e) {
2020
// if some error caught, try to let it go
2121
return true
2222
}
2323
}
2424
export const handleUrlEncode = (url: string): string => {
2525
if (!isUrlEncode(url)) {
26-
url = encodeURI(url)
26+
url = encodeURIComponent(url)
2727
}
2828
return url
2929
}
@@ -70,7 +70,9 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
7070
const requestFn = new Promise<IPathTransformedImgInfo>((resolve, reject) => {
7171
(async () => {
7272
try {
73-
const res = await axios.get(url)
73+
const res = await axios.get(url, {
74+
responseType: 'arraybuffer'
75+
})
7476
.then((resp) => {
7577
const contentType = resp.headers['content-type']
7678
if (contentType?.includes('image')) {
@@ -94,11 +96,12 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
9496
reason: `${url} is not image`
9597
})
9698
}
97-
} catch {
99+
} catch (error: any) {
98100
clearTimeout(timeoutId)
99101
resolve({
100102
success: false,
101-
reason: `request ${url} error`
103+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
104+
reason: `request ${url} error, ${error?.message ?? ''}`
102105
})
103106
}
104107
})().catch(reject)

0 commit comments

Comments
 (0)