Skip to content

Commit 587dd3f

Browse files
committed
🐛 Fix: qiniu && upyun errors
1 parent 28905f2 commit 587dd3f

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/plugins/uploader/qiniu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function postOptions (options: IQiniuConfig, fileName: string, token: string, im
1010
const base64FileName = Buffer.from(path + fileName, 'utf-8').toString('base64').replace(/\+/g, '-').replace(/\//g, '_')
1111
return {
1212
method: 'POST',
13-
url: `http://upload${area}.qiniu.com/putb64/-1/key/${base64FileName}`,
13+
url: `http://upload${area}.qiniup.com/putb64/-1/key/${base64FileName}`,
1414
headers: {
1515
Authorization: `UpToken ${token}`,
1616
contentType: 'application/octet-stream'

src/plugins/uploader/upyun.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MD5 from 'md5'
44
import { Options } from 'request-promise-native'
55
import { IBuildInEvent } from '../../utils/enum'
66
import { ILocalesKey } from '../../i18n/zh-CN'
7+
import { safeParse } from '../../utils/common'
78

89
// generate COS signature string
910
const generateSignature = (options: IUpyunConfig, fileName: string): string => {
@@ -67,11 +68,11 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
6768
body: ctx.i18n.translate<ILocalesKey>('CHECK_SETTINGS')
6869
})
6970
} else {
70-
const body = JSON.parse(err.error)
71+
const body = safeParse<{ code: string }>(err.error)
7172
ctx.emit(IBuildInEvent.NOTIFICATION, {
7273
title: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED'),
7374
body: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED_REASON', {
74-
code: body.code as string
75+
code: typeof body === 'object' ? body.code : body
7576
}),
7677
text: 'http://docs.upyun.com/api/errno/'
7778
})

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export interface IQiniuConfig {
205205
/** 自定义域名 */
206206
url: string
207207
/** 存储区域编号 */
208-
area: 'z0' | 'z1' | 'z2' | 'na0' | 'as0'
208+
area: 'z0' | 'z1' | 'z2' | 'na0' | 'as0' | string
209209
/** 网址后缀,比如使用 `?imageslim` 可进行[图片瘦身](https://developer.qiniu.com/dora/api/1271/image-thin-body-imageslim) */
210210
options: string
211211
/** 自定义存储路径,比如 `img/` */

src/utils/common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ export const isInputConfigValid = (config: any): boolean => {
341341
return false
342342
}
343343

344+
export function safeParse<T> (str: string): T | string {
345+
try {
346+
return JSON.parse(str)
347+
} catch (error) {
348+
return str
349+
}
350+
}
351+
344352
// hold...
345353
// export const configWhiteList: RegExp[] = [
346354
// /^picBed/,

0 commit comments

Comments
 (0)