Skip to content

Commit

Permalink
🐛 Fix: qiniu && upyun errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Apr 3, 2022
1 parent 28905f2 commit 587dd3f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/plugins/uploader/qiniu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function postOptions (options: IQiniuConfig, fileName: string, token: string, im
const base64FileName = Buffer.from(path + fileName, 'utf-8').toString('base64').replace(/\+/g, '-').replace(/\//g, '_')
return {
method: 'POST',
url: `http://upload${area}.qiniu.com/putb64/-1/key/${base64FileName}`,
url: `http://upload${area}.qiniup.com/putb64/-1/key/${base64FileName}`,
headers: {
Authorization: `UpToken ${token}`,
contentType: 'application/octet-stream'
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/uploader/upyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MD5 from 'md5'
import { Options } from 'request-promise-native'
import { IBuildInEvent } from '../../utils/enum'
import { ILocalesKey } from '../../i18n/zh-CN'
import { safeParse } from '../../utils/common'

// generate COS signature string
const generateSignature = (options: IUpyunConfig, fileName: string): string => {
Expand Down Expand Up @@ -67,11 +68,11 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
body: ctx.i18n.translate<ILocalesKey>('CHECK_SETTINGS')
})
} else {
const body = JSON.parse(err.error)
const body = safeParse<{ code: string }>(err.error)
ctx.emit(IBuildInEvent.NOTIFICATION, {
title: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED'),
body: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED_REASON', {
code: body.code as string
code: typeof body === 'object' ? body.code : body
}),
text: 'http://docs.upyun.com/api/errno/'
})
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export interface IQiniuConfig {
/** 自定义域名 */
url: string
/** 存储区域编号 */
area: 'z0' | 'z1' | 'z2' | 'na0' | 'as0'
area: 'z0' | 'z1' | 'z2' | 'na0' | 'as0' | string
/** 网址后缀,比如使用 `?imageslim` 可进行[图片瘦身](https://developer.qiniu.com/dora/api/1271/image-thin-body-imageslim) */
options: string
/** 自定义存储路径,比如 `img/` */
Expand Down
8 changes: 8 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ export const isInputConfigValid = (config: any): boolean => {
return false
}

export function safeParse<T> (str: string): T | string {
try {
return JSON.parse(str)
} catch (error) {
return str
}
}

// hold...
// export const configWhiteList: RegExp[] = [
// /^picBed/,
Expand Down

0 comments on commit 587dd3f

Please sign in to comment.