Skip to content

Commit

Permalink
🐛 Fix: url image download bug & tencent cos url encode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Sep 25, 2022
1 parent 907e6c9 commit 53d54f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/plugins/uploader/tcyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
delete img.base64Image
delete img.buffer
if (customUrl) {
img.imgUrl = `${customUrl}/${path}${img.fileName}${optionUrl}`
img.imgUrl = `${customUrl}/${encodeURIComponent(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
} else {
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${path}${img.fileName}${optionUrl}`
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURIComponent(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
}
} else {
throw new Error(res.body.msg)
Expand Down
27 changes: 20 additions & 7 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,28 @@ export type AxiosResponse<T = any, U = any> = import('axios').AxiosResponse<T, U

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

export interface IRequestOptionsWithFullResponse {
resolveWithFullResponse: true
}

export interface IRequestOptionsWithJson {
json: true
}

export interface IRequestOptionsWithResponseTypeArrayBuffer {
responseType: 'arraybuffer'
}

/**
* T is the config type
* U is the response data type
* T is the response data type
* U is the config type
*/
export type IResponse<T, U> = U extends {
resolveWithFullResponse: true
} ? IFullResponse<T, U> : U extends {
json: true
} ? T : string
export type IResponse<T, U> = U extends IRequestOptionsWithFullResponse
? IFullResponse<T, U>
: U extends IRequestOptionsWithJson
? T
: U extends IRequestOptionsWithResponseTypeArrayBuffer ?
Buffer : string

/**
* the old request lib will be removed in v1.5.0+
Expand Down
13 changes: 8 additions & 5 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export const isUrl = (url: string): boolean => (url.startsWith('http://') || url
export const isUrlEncode = (url: string): boolean => {
url = url || ''
try {
return url !== decodeURI(url)
return url !== decodeURIComponent(url)
} catch (e) {
// if some error caught, try to let it go
return true
}
}
export const handleUrlEncode = (url: string): string => {
if (!isUrlEncode(url)) {
url = encodeURI(url)
url = encodeURIComponent(url)
}
return url
}
Expand Down Expand Up @@ -70,7 +70,9 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
const requestFn = new Promise<IPathTransformedImgInfo>((resolve, reject) => {
(async () => {
try {
const res = await axios.get(url)
const res = await axios.get(url, {
responseType: 'arraybuffer'
})
.then((resp) => {
const contentType = resp.headers['content-type']
if (contentType?.includes('image')) {
Expand All @@ -94,11 +96,12 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
reason: `${url} is not image`
})
}
} catch {
} catch (error: any) {
clearTimeout(timeoutId)
resolve({
success: false,
reason: `request ${url} error`
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
reason: `request ${url} error, ${error?.message ?? ''}`
})
}
})().catch(reject)
Expand Down

0 comments on commit 53d54f8

Please sign in to comment.