🔨 Refactor: modify some code

This commit is contained in:
萌萌哒赫萝
2023-03-02 23:07:26 +08:00
parent 8911217780
commit c819872635
6 changed files with 91 additions and 77 deletions

View File

@@ -76,15 +76,17 @@ export function getFileIconPath (fileName: string) {
return availableIconList.includes(ext) ? `${ext}.webp` : 'unknown.webp'
}
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
export function formatFileSize (size: number) {
if (size === 0) return ''
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
const index = Math.floor(Math.log2(size) / 10)
return `${(size / Math.pow(2, index * 10)).toFixed(2)} ${units[index]}`
}
export function formatFileName (fileName: string, length: number = 20) {
const ext = path.extname(fileName)
let ext = path.extname(fileName)
ext = ext.length > 5 ? ext.slice(ext.length - 5) : ext
const name = path.basename(fileName, ext)
return name.length > length ? `${name.slice(0, length)}...${ext}` : fileName
}
@@ -92,7 +94,7 @@ export function formatFileName (fileName: string, length: number = 20) {
export const getExtension = (fileName: string) => path.extname(fileName).slice(1)
export const isImage = (fileName: string) =>
['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'ico'].includes(getExtension(fileName))
['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'ico', 'svg'].includes(getExtension(fileName))
export function formObjToTableData (obj: any) {
const exclude = [undefined, null, '', 'transformedConfig']
@@ -103,16 +105,11 @@ export function formObjToTableData (obj: any) {
}
export function isValidUrl (str: string) {
const pattern = new RegExp(
'^([a-zA-Z]+:\\/\\/)?' +
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
'((\\d{1,3}\\.){3}\\d{1,3}))' +
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
'(\\?[;&a-z\\d%_.~+=-]*)?' +
'(\\#[-a-z\\d_]*)?$',
'i'
)
return pattern.test(str)
try {
return !!new URL(str)
} catch (e) {
return false
}
}
export interface IHTTPProxy {

View File

@@ -756,63 +756,5 @@ export const supportedPicBedList: IStringKeyMap = {
options: ['alias', 'endpoint', 'username', 'password', 'bucketName', 'baseDir', 'customUrl', 'proxy', 'sslEnabled'],
refLink: 'https://pichoro.horosama.com/#/PicHoroDocs/configure?id=webdav',
referenceText: '配置教程请参考:'
},
localplist: {
name: '本地',
icon: 'localplist',
configOptions: {
alias: {
required: true,
description: '配置别名-必需',
placeholder: '该配置的唯一标识',
type: 'string',
rule: aliasRule,
default: 'localplist-A',
tooltip: aliasTooltip
},
baseDir: {
required: false,
description: '起始目录-可选',
placeholder: '例如:/test1',
type: 'string',
default: '/',
tooltip: '请填写完整的本地路径,例如:/test1'
},
customUrl: {
required: false,
description: '自定义域名-可选',
placeholder: '例如https://example.com',
type: 'string',
tooltip: '如果您的本地服务器支持自定义域名请填写完整的自定义域名例如https://example.com',
rule: [
{
validator: (rule: any, value: any, callback: any) => {
if (value) {
if (!/^https?:\/\/.+/.test(value)) {
callback(new Error('自定义域名请以http://或https://开头'))
} else {
callback()
}
} else {
callback()
}
},
trigger: 'change'
}
]
},
webDir: {
required: false,
description: 'Web目录-可选',
placeholder: '例如:/test1',
type: 'string',
default: '/',
tooltip: '请填写您的Web服务器的根目录例如/test1'
}
},
explain: '本地配置',
options: ['alias', 'baseDir', 'customUrl', 'webDir'],
refLink: '',
referenceText: ''
}
}