mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-31 09:49:35 +08:00
🚧 WIP(custom): v3.0.0 migrate to vite and esm
This commit is contained in:
@@ -1,39 +1,37 @@
|
||||
import crypto from 'crypto'
|
||||
import path from 'path'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
import { availableIconList } from '@/manage/utils/icon'
|
||||
import { getConfig } from '@/manage/utils/dataSender'
|
||||
import { availableIconList } from '@/manage/utils/icon'
|
||||
import { IStringKeyMap } from '#/types/types'
|
||||
import { handleUrlEncode, isNeedToShorten, safeSliceF } from '#/utils/common'
|
||||
|
||||
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('')
|
||||
}
|
||||
|
||||
export function renameFileNameWithTimestamp(oldName: string): string {
|
||||
return `${Math.floor(Date.now() / 1000)}${randomStringGenerator(5)}${path.extname(oldName)}`
|
||||
export function renameFileNameWithTimestamp (oldName: string): string {
|
||||
return `${Math.floor(Date.now() / 1000)}${randomStringGenerator(5)}${window.node.path.extname(oldName)}`
|
||||
}
|
||||
|
||||
export function renameFileNameWithRandomString(oldName: string, length: number = 5): string {
|
||||
return `${randomStringGenerator(length)}${path.extname(oldName)}`
|
||||
export function renameFileNameWithRandomString (oldName: string, length: number = 5): string {
|
||||
return `${randomStringGenerator(length)}${window.node.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 {
|
||||
return crypto.createHash('md5').update(input).digest('hex')
|
||||
function getMd5 (input: any): string {
|
||||
return window.node.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 fileBaseName = window.node.path.basename(oldName, window.node.path.extname(oldName))
|
||||
const conversionMap: { [key: string]: () => string } = {
|
||||
'{Y}': () => year,
|
||||
'{y}': () => year.slice(2),
|
||||
@@ -47,8 +45,8 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st
|
||||
'{md5-16}': () => getMd5(fileBaseName).slice(0, 16),
|
||||
'{filename}': () =>
|
||||
affixFileName
|
||||
? path.basename(affixFileName, path.extname(affixFileName))
|
||||
: path.basename(oldName, path.extname(oldName)),
|
||||
? window.node.path.basename(affixFileName, window.node.path.extname(affixFileName))
|
||||
: window.node.path.basename(oldName, window.node.path.extname(oldName)),
|
||||
'{uuid}': () => uuidv4().replace(/-/g, ''),
|
||||
'{timestamp}': () => date.getTime().toString()
|
||||
}
|
||||
@@ -58,7 +56,7 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st
|
||||
) {
|
||||
return oldName
|
||||
}
|
||||
const ext = path.extname(oldName)
|
||||
const ext = window.node.path.extname(oldName)
|
||||
let newName =
|
||||
Object.keys(conversionMap).reduce((acc, cur) => {
|
||||
return acc.replace(new RegExp(cur, 'g'), conversionMap[cur]())
|
||||
@@ -71,7 +69,7 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st
|
||||
return newName
|
||||
}
|
||||
|
||||
export function renameFile(
|
||||
export function renameFile (
|
||||
{ timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap,
|
||||
oldName = ''
|
||||
): string {
|
||||
@@ -87,7 +85,7 @@ export function renameFile(
|
||||
}
|
||||
}
|
||||
|
||||
export async function formatLink(url: string, fileName: string, type: string, format?: string): Promise<string> {
|
||||
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':
|
||||
@@ -110,27 +108,27 @@ export async function formatLink(url: string, fileName: string, type: string, fo
|
||||
}
|
||||
}
|
||||
|
||||
export function getFileIconPath(fileName: string) {
|
||||
const ext = path.extname(fileName).slice(1).toLowerCase()
|
||||
export function getFileIconPath (fileName: string) {
|
||||
const ext = window.node.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) {
|
||||
let ext = path.extname(fileName)
|
||||
export function formatFileName (fileName: string, length: number = 20) {
|
||||
let ext = window.node.path.extname(fileName)
|
||||
ext = ext.length > 5 ? ext.slice(ext.length - 5) : ext
|
||||
const name = path.basename(fileName, ext)
|
||||
const name = window.node.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]))
|
||||
@@ -141,7 +139,7 @@ export function formObjToTableData(obj: any) {
|
||||
.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) {
|
||||
@@ -160,7 +158,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')
|
||||
@@ -171,7 +169,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
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { T as $T } from '@/i18n'
|
||||
import { IStringKeyMap } from '#/types/types'
|
||||
|
||||
const defaultBaseRule = (name: string) => {
|
||||
return [
|
||||
@@ -228,10 +229,10 @@ export const supportedPicBedList: IStringKeyMap = {
|
||||
const isBracketsValid = customUrlList.every((customUrl: string) => {
|
||||
const bracketPaired = (str: string) => {
|
||||
const stack = []
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (str[i] === '{') {
|
||||
stack.push(str[i])
|
||||
} else if (str[i] === '}') {
|
||||
for (const i of str) {
|
||||
if (i === '{') {
|
||||
stack.push(i)
|
||||
} else if (i === '}') {
|
||||
if (stack.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { sendRPC, triggerRPC } from '@/utils/common'
|
||||
|
||||
import { IRPCActionType } from '#/types/enum'
|
||||
import { IObj } from '#/types/types'
|
||||
|
||||
export function saveConfig(config: IObj | string, value?: any) {
|
||||
export function saveConfig (config: IObj | string, value?: any) {
|
||||
const configObj = typeof config === 'string' ? { [config]: value } : config
|
||||
sendRPC(IRPCActionType.MANAGE_SAVE_CONFIG, configObj)
|
||||
window.electron.sendRPC(IRPCActionType.MANAGE_SAVE_CONFIG, configObj)
|
||||
}
|
||||
|
||||
export async function getConfig<T>(key?: string): Promise<T | undefined> {
|
||||
return await triggerRPC<T>(IRPCActionType.MANAGE_GET_CONFIG, key)
|
||||
export async function getConfig<T> (key?: string): Promise<T | undefined> {
|
||||
return await window.electron.triggerRPC<T>(IRPCActionType.MANAGE_GET_CONFIG, key)
|
||||
}
|
||||
|
||||
export function removeConfig(key: string, propName: string) {
|
||||
sendRPC(IRPCActionType.MANAGE_REMOVE_CONFIG, key, propName)
|
||||
export function removeConfig (key: string, propName: string) {
|
||||
window.electron.sendRPC(IRPCActionType.MANAGE_REMOVE_CONFIG, key, propName)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import crypto from 'crypto'
|
||||
import axios from 'axios'
|
||||
import { IStringKeyMap } from '#/types/types'
|
||||
|
||||
const AUTH_KEY_VALUE_RE = /(\w+)=["']?([^'"]{1,10000})["']?/
|
||||
let NC = 0
|
||||
const NC_PAD = '00000000'
|
||||
|
||||
function md5(text: crypto.BinaryLike) {
|
||||
return crypto.createHash('md5').update(text).digest('hex')
|
||||
function md5 (text: any) {
|
||||
return window.node.crypto.createHash('md5').update(text).digest('hex')
|
||||
}
|
||||
|
||||
export function digestAuthHeader(
|
||||
export function digestAuthHeader (
|
||||
method: string,
|
||||
uri: string,
|
||||
wwwAuthenticate: string,
|
||||
@@ -18,8 +17,8 @@ export function digestAuthHeader(
|
||||
) {
|
||||
const parts = wwwAuthenticate.split(',')
|
||||
const opts = {} as IStringKeyMap
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const m = AUTH_KEY_VALUE_RE.exec(parts[i])
|
||||
for (const i of parts) {
|
||||
const m = AUTH_KEY_VALUE_RE.exec(i)
|
||||
if (m) {
|
||||
opts[m[1]] = m[2].replace(/["']/g, '')
|
||||
}
|
||||
@@ -35,7 +34,7 @@ export function digestAuthHeader(
|
||||
|
||||
let nc = String(++NC)
|
||||
nc = NC_PAD.substring(nc.length) + nc
|
||||
const cnonce = crypto.randomBytes(8).toString('hex')
|
||||
const cnonce = window.node.crypto.randomBytes(8).toString('hex')
|
||||
|
||||
const ha1 = md5(userpassArray[0] + ':' + opts.realm + ':' + userpassArray[1])
|
||||
const ha2 = md5(method.toUpperCase() + ':' + uri)
|
||||
@@ -67,9 +66,9 @@ export function digestAuthHeader(
|
||||
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 window.node.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,5 +1,6 @@
|
||||
import { AliyunAreaCodeName, QiniuAreaCodeName, TencentAreaCodeName } from '~/manage/utils/constants'
|
||||
import { T as $T } from '@/i18n'
|
||||
import { IStringKeyMap } from '#/types/types'
|
||||
import { AliyunAreaCodeName, QiniuAreaCodeName, TencentAreaCodeName } from '~/manage/utils/constants'
|
||||
|
||||
export const newBucketConfig: IStringKeyMap = {
|
||||
tcyun: {
|
||||
|
||||
Reference in New Issue
Block a user