mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-31 17:59:45 +08:00
🎨 Style(custom): format with prettier
This commit is contained in:
@@ -7,32 +7,34 @@ import { getConfig } from '@/manage/utils/dataSender'
|
||||
|
||||
import { handleUrlEncode, safeSliceF, isNeedToShorten } from '#/utils/common'
|
||||
|
||||
export function randomStringGenerator (length: number): string {
|
||||
export function randomStringGenerator(length: number): string {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||||
return Array.from({ length }).map(() => chars.charAt(Math.floor(Math.random() * chars.length))).join('')
|
||||
return Array.from({ length })
|
||||
.map(() => chars.charAt(Math.floor(Math.random() * chars.length)))
|
||||
.join('')
|
||||
}
|
||||
|
||||
export function renameFileNameWithTimestamp (oldName: string): string {
|
||||
export function renameFileNameWithTimestamp(oldName: string): string {
|
||||
return `${Math.floor(Date.now() / 1000)}${randomStringGenerator(5)}${path.extname(oldName)}`
|
||||
}
|
||||
|
||||
export function renameFileNameWithRandomString (oldName: string, length: number = 5): string {
|
||||
export function renameFileNameWithRandomString(oldName: string, length: number = 5): string {
|
||||
return `${randomStringGenerator(length)}${path.extname(oldName)}`
|
||||
}
|
||||
|
||||
function renameFormatHelper (num: number): string {
|
||||
function renameFormatHelper(num: number): string {
|
||||
return num.toString().length === 1 ? `0${num}` : num.toString()
|
||||
}
|
||||
|
||||
function getMd5 (input: crypto.BinaryLike): string {
|
||||
function getMd5(input: crypto.BinaryLike): string {
|
||||
return crypto.createHash('md5').update(input).digest('hex')
|
||||
}
|
||||
|
||||
export function renameFileNameWithCustomString (oldName: string, customFormat: string, affixFileName?: string): string {
|
||||
export function renameFileNameWithCustomString(oldName: string, customFormat: string, affixFileName?: string): string {
|
||||
const date = new Date()
|
||||
const year = date.getFullYear().toString()
|
||||
const fileBaseName = path.basename(oldName, path.extname(oldName))
|
||||
const conversionMap : {[key: string]: () => string} = {
|
||||
const conversionMap: { [key: string]: () => string } = {
|
||||
'{Y}': () => year,
|
||||
'{y}': () => year.slice(2),
|
||||
'{m}': () => renameFormatHelper(date.getMonth() + 1),
|
||||
@@ -43,17 +45,24 @@ export function renameFileNameWithCustomString (oldName: string, customFormat: s
|
||||
'{ms}': () => date.getMilliseconds().toString().padStart(3, '0'),
|
||||
'{md5}': () => getMd5(fileBaseName),
|
||||
'{md5-16}': () => getMd5(fileBaseName).slice(0, 16),
|
||||
'{filename}': () => affixFileName ? path.basename(affixFileName, path.extname(affixFileName)) : path.basename(oldName, path.extname(oldName)),
|
||||
'{filename}': () =>
|
||||
affixFileName
|
||||
? path.basename(affixFileName, path.extname(affixFileName))
|
||||
: path.basename(oldName, path.extname(oldName)),
|
||||
'{uuid}': () => uuidv4().replace(/-/g, ''),
|
||||
'{timestamp}': () => date.getTime().toString()
|
||||
}
|
||||
if (customFormat === undefined || (!Object.keys(conversionMap).some(item => customFormat.includes(item)) && !customFormat.includes('{str-'))) {
|
||||
if (
|
||||
customFormat === undefined ||
|
||||
(!Object.keys(conversionMap).some(item => customFormat.includes(item)) && !customFormat.includes('{str-'))
|
||||
) {
|
||||
return oldName
|
||||
}
|
||||
const ext = path.extname(oldName)
|
||||
let newName = Object.keys(conversionMap).reduce((acc, cur) => {
|
||||
return acc.replace(new RegExp(cur, 'g'), conversionMap[cur]())
|
||||
}, customFormat) + ext
|
||||
let newName =
|
||||
Object.keys(conversionMap).reduce((acc, cur) => {
|
||||
return acc.replace(new RegExp(cur, 'g'), conversionMap[cur]())
|
||||
}, customFormat) + ext
|
||||
const strRegex = /{str-(\d+)}/gi
|
||||
newName = newName.replace(strRegex, (_, group1) => {
|
||||
const length = parseInt(group1, 10)
|
||||
@@ -62,7 +71,10 @@ export function renameFileNameWithCustomString (oldName: string, customFormat: s
|
||||
return newName
|
||||
}
|
||||
|
||||
export function renameFile ({ timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap, oldName = ''): string {
|
||||
export function renameFile(
|
||||
{ timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap,
|
||||
oldName = ''
|
||||
): string {
|
||||
switch (true) {
|
||||
case timestampRename:
|
||||
return renameFileNameWithTimestamp(oldName)
|
||||
@@ -75,8 +87,8 @@ export function renameFile ({ timestampRename, randomStringRename, customRename,
|
||||
}
|
||||
}
|
||||
|
||||
export async function formatLink (url: string, fileName: string, type: string, format?: string) : Promise<string> {
|
||||
const encodedUrl = await getConfig('settings.isEncodeUrl') ? handleUrlEncode(url) : url
|
||||
export async function formatLink(url: string, fileName: string, type: string, format?: string): Promise<string> {
|
||||
const encodedUrl = (await getConfig('settings.isEncodeUrl')) ? handleUrlEncode(url) : url
|
||||
switch (type) {
|
||||
case 'markdown':
|
||||
return ``
|
||||
@@ -98,35 +110,38 @@ export async function formatLink (url: string, fileName: string, type: string, f
|
||||
}
|
||||
}
|
||||
|
||||
export function getFileIconPath (fileName: string) {
|
||||
export function getFileIconPath(fileName: string) {
|
||||
const ext = path.extname(fileName).slice(1).toLowerCase()
|
||||
return availableIconList.includes(ext) ? `${ext}.webp` : 'unknown.webp'
|
||||
}
|
||||
|
||||
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
|
||||
|
||||
export function formatFileSize (size: number) {
|
||||
export function formatFileSize(size: number) {
|
||||
if (size === 0) return ''
|
||||
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) {
|
||||
export function formatFileName(fileName: string, length: number = 20) {
|
||||
let ext = path.extname(fileName)
|
||||
ext = ext.length > 5 ? ext.slice(ext.length - 5) : ext
|
||||
const name = path.basename(fileName, ext)
|
||||
return isNeedToShorten(fileName, length) ? `${safeSliceF(name, length - 3 - ext.length)}...${ext}` : fileName
|
||||
}
|
||||
|
||||
export function formObjToTableData (obj: any) {
|
||||
export function formObjToTableData(obj: any) {
|
||||
const exclude = [undefined, null, '', 'transformedConfig']
|
||||
return Object.keys(obj).filter(key => !exclude.includes(obj[key])).map(key => ({
|
||||
key,
|
||||
value: typeof obj[key] === 'object' ? JSON.stringify(obj[key]) : obj[key]
|
||||
})).sort((a, b) => a.key.localeCompare(b.key))
|
||||
return Object.keys(obj)
|
||||
.filter(key => !exclude.includes(obj[key]))
|
||||
.map(key => ({
|
||||
key,
|
||||
value: typeof obj[key] === 'object' ? JSON.stringify(obj[key]) : obj[key]
|
||||
}))
|
||||
.sort((a, b) => a.key.localeCompare(b.key))
|
||||
}
|
||||
|
||||
export function isValidUrl (str: string) {
|
||||
export function isValidUrl(str: string) {
|
||||
try {
|
||||
return !!new URL(str)
|
||||
} catch (e) {
|
||||
@@ -145,7 +160,7 @@ export const svg = `
|
||||
" style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
|
||||
`
|
||||
|
||||
export function customStrMatch (str: string, pattern: string) : boolean {
|
||||
export function customStrMatch(str: string, pattern: string): boolean {
|
||||
if (!str || !pattern) return false
|
||||
try {
|
||||
const reg = new RegExp(pattern, 'ug')
|
||||
@@ -156,7 +171,7 @@ export function customStrMatch (str: string, pattern: string) : boolean {
|
||||
}
|
||||
}
|
||||
|
||||
export function customStrReplace (str: string, pattern: string, replacement: string) : string {
|
||||
export function customStrReplace(str: string, pattern: string, replacement: string): string {
|
||||
if (!str || !pattern) return str
|
||||
replacement = replacement || ''
|
||||
let result = str
|
||||
|
||||
@@ -334,7 +334,16 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_ALIYUN_EXPLAIN'),
|
||||
options: ['alias', 'accessKeyId', 'accessKeySecret', 'bucketName', 'baseDir', 'isAutoCustomUrl', 'paging', 'itemsPerPage'],
|
||||
options: [
|
||||
'alias',
|
||||
'accessKeyId',
|
||||
'accessKeySecret',
|
||||
'bucketName',
|
||||
'baseDir',
|
||||
'isAutoCustomUrl',
|
||||
'paging',
|
||||
'itemsPerPage'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#%E9%98%BF%E9%87%8C%E4%BA%91oss',
|
||||
referenceText: $T('MANAGE_CONSTANT_ALIYUN_REFER_TEXT')
|
||||
},
|
||||
@@ -412,7 +421,17 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_TENCENT_EXPLAIN'),
|
||||
options: ['alias', 'secretId', 'secretKey', 'appId', 'bucketName', 'baseDir', 'isAutoCustomUrl', 'paging', 'itemsPerPage'],
|
||||
options: [
|
||||
'alias',
|
||||
'secretId',
|
||||
'secretKey',
|
||||
'appId',
|
||||
'bucketName',
|
||||
'baseDir',
|
||||
'isAutoCustomUrl',
|
||||
'paging',
|
||||
'itemsPerPage'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#%E8%85%BE%E8%AE%AF%E4%BA%91',
|
||||
referenceText: $T('MANAGE_CONSTANT_TENCENT_REFER_TEXT')
|
||||
},
|
||||
@@ -525,7 +544,18 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_UPYUN_EXPLAIN'),
|
||||
options: ['alias', 'bucketName', 'operator', 'password', 'baseDir', 'customUrl', 'paging', 'itemsPerPage', 'antiLeechToken', 'expireTime'],
|
||||
options: [
|
||||
'alias',
|
||||
'bucketName',
|
||||
'operator',
|
||||
'password',
|
||||
'baseDir',
|
||||
'customUrl',
|
||||
'paging',
|
||||
'itemsPerPage',
|
||||
'antiLeechToken',
|
||||
'expireTime'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#%E5%8F%88%E6%8B%8D%E4%BA%91',
|
||||
referenceText: $T('MANAGE_CONSTANT_UPYUN_REFER_TEXT')
|
||||
},
|
||||
@@ -680,7 +710,21 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_S3_EXPLAIN'),
|
||||
options: ['alias', 'accessKeyId', 'secretAccessKey', 'endpoint', 'sslEnabled', 's3ForcePathStyle', 'proxy', 'aclForUpload', 'bucketName', 'baseDir', 'dogeCloudSupport', 'paging', 'itemsPerPage'],
|
||||
options: [
|
||||
'alias',
|
||||
'accessKeyId',
|
||||
'secretAccessKey',
|
||||
'endpoint',
|
||||
'sslEnabled',
|
||||
's3ForcePathStyle',
|
||||
'proxy',
|
||||
'aclForUpload',
|
||||
'bucketName',
|
||||
'baseDir',
|
||||
'dogeCloudSupport',
|
||||
'paging',
|
||||
'itemsPerPage'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#s3',
|
||||
referenceText: $T('MANAGE_CONSTANT_S3_REFER_TEXT')
|
||||
},
|
||||
@@ -792,7 +836,19 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_WEBDAV_EXPLAIN'),
|
||||
options: ['alias', 'endpoint', 'username', 'password', 'bucketName', 'baseDir', 'customUrl', 'webPath', 'proxy', 'sslEnabled', 'authType'],
|
||||
options: [
|
||||
'alias',
|
||||
'endpoint',
|
||||
'username',
|
||||
'password',
|
||||
'bucketName',
|
||||
'baseDir',
|
||||
'customUrl',
|
||||
'webPath',
|
||||
'proxy',
|
||||
'sslEnabled',
|
||||
'authType'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#webdav',
|
||||
referenceText: $T('MANAGE_CONSTANT_WEBDAV_REFER_TEXT')
|
||||
},
|
||||
@@ -1003,7 +1059,21 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
}
|
||||
},
|
||||
explain: $T('MANAGE_CONSTANT_SFTP_EXPLAIN'),
|
||||
options: ['alias', 'host', 'port', 'username', 'password', 'privateKey', 'passphrase', 'fileMode', 'dirMode', 'baseDir', 'customUrl', 'bucketName', 'webPath'],
|
||||
options: [
|
||||
'alias',
|
||||
'host',
|
||||
'port',
|
||||
'username',
|
||||
'password',
|
||||
'privateKey',
|
||||
'passphrase',
|
||||
'fileMode',
|
||||
'dirMode',
|
||||
'baseDir',
|
||||
'customUrl',
|
||||
'bucketName',
|
||||
'webPath'
|
||||
],
|
||||
refLink: 'https://piclist.cn/manage.html#sftp',
|
||||
referenceText: $T('MANAGE_CONSTANT_SFTP_REFER_TEXT')
|
||||
}
|
||||
|
||||
@@ -2,17 +2,21 @@ import { ipcRenderer } from 'electron'
|
||||
|
||||
import { getRawData } from '@/utils/common'
|
||||
|
||||
import { PICLIST_MANAGE_GET_CONFIG, PICLIST_MANAGE_SAVE_CONFIG, PICLIST_MANAGE_REMOVE_CONFIG } from '~/manage/events/constants'
|
||||
import {
|
||||
PICLIST_MANAGE_GET_CONFIG,
|
||||
PICLIST_MANAGE_SAVE_CONFIG,
|
||||
PICLIST_MANAGE_REMOVE_CONFIG
|
||||
} from '~/manage/events/constants'
|
||||
|
||||
export function saveConfig (config: IObj | string, value?: any) {
|
||||
export function saveConfig(config: IObj | string, value?: any) {
|
||||
const configObj = typeof config === 'string' ? { [config]: value } : getRawData(config)
|
||||
ipcRenderer.send(PICLIST_MANAGE_SAVE_CONFIG, configObj)
|
||||
}
|
||||
|
||||
export async function getConfig<T> (key?: string): Promise<T | undefined> {
|
||||
export async function getConfig<T>(key?: string): Promise<T | undefined> {
|
||||
return await ipcRenderer.invoke(PICLIST_MANAGE_GET_CONFIG, key)
|
||||
}
|
||||
|
||||
export function removeConfig (key: string, propName: string) {
|
||||
export function removeConfig(key: string, propName: string) {
|
||||
ipcRenderer.send(PICLIST_MANAGE_REMOVE_CONFIG, key, propName)
|
||||
}
|
||||
|
||||
@@ -5,11 +5,17 @@ const AUTH_KEY_VALUE_RE = /(\w+)=["']?([^'"]{1,10000})["']?/
|
||||
let NC = 0
|
||||
const NC_PAD = '00000000'
|
||||
|
||||
function md5 (text: crypto.BinaryLike) {
|
||||
function md5(text: crypto.BinaryLike) {
|
||||
return crypto.createHash('md5').update(text).digest('hex')
|
||||
}
|
||||
|
||||
export function digestAuthHeader (method: string, uri: string, wwwAuthenticate: string, username: string, password: string) {
|
||||
export function digestAuthHeader(
|
||||
method: string,
|
||||
uri: string,
|
||||
wwwAuthenticate: string,
|
||||
username: string,
|
||||
password: string
|
||||
) {
|
||||
const parts = wwwAuthenticate.split(',')
|
||||
const opts = {} as IStringKeyMap
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
@@ -61,11 +67,9 @@ export function digestAuthHeader (method: string, uri: string, wwwAuthenticate:
|
||||
return authstring
|
||||
}
|
||||
|
||||
export async function getAuthHeader (method: string, host: string, uri: string, username: string, password: string) {
|
||||
export async function getAuthHeader(method: string, host: string, uri: string, username: string, password: string) {
|
||||
try {
|
||||
await axios.get(
|
||||
`${host}${uri}`
|
||||
)
|
||||
await axios.get(`${host}${uri}`)
|
||||
} catch (error: any) {
|
||||
if (error.response.status === 401 && error.response.headers['www-authenticate']) {
|
||||
return digestAuthHeader(method, uri, error.response.headers['www-authenticate'], username, password)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AliyunAreaCodeName, QiniuAreaCodeName, TencentAreaCodeName } from '~/manage/utils/constants'
|
||||
import { T as $T } from '@/i18n'
|
||||
|
||||
export const newBucketConfig:IStringKeyMap = {
|
||||
export const newBucketConfig: IStringKeyMap = {
|
||||
tcyun: {
|
||||
name: $T('MANAGE_NEW_BUCKET_TCYUN_NAME'),
|
||||
icon: 'tcyun',
|
||||
|
||||
Reference in New Issue
Block a user