🔨 Refactor: refactor some code and add error log

This commit is contained in:
萌萌哒赫萝 2023-04-26 17:01:55 +08:00
parent 041018d4cc
commit 8cfb1006a9
11 changed files with 49 additions and 42 deletions

View File

@ -52,28 +52,21 @@ export const showMessageBox = (options: any) => {
})
}
const thresholds = [
{ limit: 1000, value: 500 },
{ limit: 1500, value: 1000 },
{ limit: 3000, value: 2000 },
{ limit: 5000, value: 3000 },
{ limit: 7000, value: 5000 },
{ limit: 10000, value: 8000 },
{ limit: 12000, value: 10000 },
{ limit: 20000, value: 15000 },
{ limit: 30000, value: 20000 }
]
export const calcDurationRange = (duration: number) => {
if (duration < 1000) {
return 500
} else if (duration < 1500) {
return 1000
} else if (duration < 3000) {
return 2000
} else if (duration < 5000) {
return 3000
} else if (duration < 7000) {
return 5000
} else if (duration < 10000) {
return 8000
} else if (duration < 12000) {
return 10000
} else if (duration < 20000) {
return 15000
} else if (duration < 30000) {
return 20000
}
// max range
return 100000
const foundThreshold = thresholds.find(({ limit }) => duration < limit)
return foundThreshold ? foundThreshold.value : 100000
}
/**

View File

@ -8,19 +8,11 @@ export const isMacOS = process.platform === 'darwin'
let version: string | undefined
const clean = (version: string) => {
const { length } = version.split('.')
if (length === 1) {
return `${version}.0.0`
}
if (length === 2) {
return `${version}.0`
}
return version
}
const clean = (version: string) => version.split('.').length === 1
? `${version}.0.0`
: version.split('.').length === 2
? `${version}.0`
: version
const parseVersion = (plist: string) => {
const matches = /<key>ProductVersion<\/key>\s*<string>([\d.]+)<\/string>/.exec(plist)
@ -32,9 +24,7 @@ const parseVersion = (plist: string) => {
}
export function macOSVersion (): string {
if (!isMacOS) {
return ''
}
if (!isMacOS) return ''
if (!version) {
const file = fs.readFileSync('/System/Library/CoreServices/SystemVersion.plist', 'utf8')

View File

@ -1,7 +1,18 @@
import OSS from 'ali-oss'
interface IConfigMap {
fileName: string
config: {
accessKeyId: string
accessKeySecret: string
bucket: string
area: string
path?: string
}
}
export default class AliyunApi {
static async delete (configMap: IStringKeyMap): Promise<boolean> {
static async delete (configMap: IConfigMap): Promise<boolean> {
const { fileName, config: { accessKeyId, accessKeySecret, bucket, area, path } } = configMap
try {
const client = new OSS({
@ -19,6 +30,7 @@ export default class AliyunApi {
const result = await client.delete(key) as any
return result.res.status === 204
} catch (error) {
console.log(error)
return false
}
}

View File

@ -40,6 +40,7 @@ export default class AwsS3Api {
}).promise()
return result.$response.httpResponse.statusCode === 204
} catch (error) {
console.log(error)
return false
}
}

View File

@ -25,6 +25,7 @@ export default class GithubApi {
})
return result.status === 200
} catch (error) {
console.log(error)
return false
}
}

View File

@ -26,6 +26,7 @@ export default class ImgurApi {
})
return res.status === 200
} catch (error) {
console.log(error)
return false
}
}

View File

@ -27,6 +27,7 @@ export default class QiniuApi {
}) as any
return res && res.respInfo.statusCode === 200
} catch (error) {
console.log(error)
return false
}
}

View File

@ -1,13 +1,16 @@
import axios from 'axios'
export default class SmmsApi {
private static readonly baseUrl = 'https://smms.app/api/v2'
static async delete (configMap: IStringKeyMap): Promise<boolean> {
const { hash, config: { token } } = configMap
if (!hash || !token) {
return false
} else {
}
try {
const res = await axios.get(
`https://smms.app/api/v2/delete/${hash}`, {
`${SmmsApi.baseUrl}/delete/${hash}`, {
headers: {
Authorization: token
},
@ -18,6 +21,9 @@ export default class SmmsApi {
timeout: 30000
})
return res.status === 200
} catch (error) {
console.log(error)
return false
}
}
}

View File

@ -21,6 +21,7 @@ export default class TcyunApi {
})
return result.statusCode === 204
} catch (error) {
console.log(error)
return false
}
}

View File

@ -13,9 +13,9 @@ export default class UpyunApi {
} else {
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
}
const result = await client.deleteFile(key)
return result
return await client.deleteFile(key)
} catch (error) {
console.log(error)
return false
}
}

View File

@ -22,6 +22,7 @@ export default class WebdavApi {
await ctx.deleteFile(key)
return true
} catch (error) {
console.log(error)
return false
}
}