🔨 Refactor(custom): refactor short url ggenerationg func

This commit is contained in:
Kuingsmile 2024-05-16 13:20:29 +08:00
parent d9e4e30da3
commit ace6f2e971

View File

@ -59,25 +59,15 @@ export const showMessageBox = (options: any) => {
} }
export const calcDurationRange = (duration: number) => { export const calcDurationRange = (duration: number) => {
if (duration < 1000) { if (duration < 1000) return 500
return 500 if (duration < 1500) return 1000
} else if (duration < 1500) { if (duration < 3000) return 2000
return 1000 if (duration < 5000) return 3000
} else if (duration < 3000) { if (duration < 7000) return 5000
return 2000 if (duration < 10000) return 8000
} else if (duration < 5000) { if (duration < 12000) return 10000
return 3000 if (duration < 20000) return 15000
} else if (duration < 7000) { if (duration < 30000) return 20000
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 // max range
return 100000 return 100000
} }
@ -105,22 +95,19 @@ export const ensureFilePath = (filePath: string, prefix = 'file://'): string =>
export const getClipboardFilePath = (): string => { export const getClipboardFilePath = (): string => {
// TODO: linux support // TODO: linux support
const img = clipboard.readImage() const img = clipboard.readImage()
if (img.isEmpty()) { const platform = process.platform
if (process.platform === 'win32') {
const imgPath = clipboard.readBuffer('FileNameW')?.toString('ucs2')?.replace(RegExp(String.fromCharCode(0), 'g'), '') if (!img.isEmpty() && platform === 'darwin') {
if (imgPath) { let imgPath = clipboard.read('public.file-url') // will get file://xxx/xxx
return imgPath imgPath = ensureFilePath(imgPath)
} return imgPath ? imgPath.replace('file://', '') : ''
}
} else {
if (process.platform === 'darwin') {
let imgPath = clipboard.read('public.file-url') // will get file://xxx/xxx
imgPath = ensureFilePath(imgPath)
if (imgPath) {
return imgPath.replace('file://', '')
}
}
} }
if (img.isEmpty() && platform === 'win32') {
const imgPath = clipboard.readBuffer('FileNameW')?.toString('ucs2')?.replace(RegExp(String.fromCharCode(0), 'g'), '')
return imgPath || ''
}
return '' return ''
} }
@ -128,66 +115,83 @@ export const handleUrlEncodeWithSetting = (url: string) => db.get(configPaths.se
const c1nApi = 'https://c1n.cn/link/short' const c1nApi = 'https://c1n.cn/link/short'
export const generateShortUrl = async (url: string) => { const generateC1NShortUrl = async (url: string) => {
const server = db.get(configPaths.settings.shortUrlServer) || IShortUrlServer.C1N const form = new FormData()
if (server === IShortUrlServer.C1N) { form.append('url', url)
const form = new FormData() const c1nToken = db.get(configPaths.settings.c1nToken) || ''
form.append('url', url) if (!c1nToken) {
const c1nToken = db.get(configPaths.settings.c1nToken) || '' logger.warn('c1n token is not set')
if (!c1nToken) { return url
logger.warn('c1n token is not set') }
return url try {
const res = await axios.post(c1nApi, form, {
headers: {
token: c1nToken
}
})
if (res.status >= 200 && res.status < 300 && res.data?.code === 0) {
return res.data.data
}
} catch (e: any) {
logger.error(e)
}
return url
}
const generateYOURLSShortUrl = async (url: string) => {
let domain = db.get(configPaths.settings.yourlsDomain) || ''
const signature = db.get(configPaths.settings.yourlsSignature) || ''
if (domain && signature) {
if (!/^https?:\/\//.test(domain)) {
domain = `http://${domain}`
} }
try { try {
const res = await axios.post(c1nApi, form, { const res = await axios.get(`${domain}/yourls-api.php?signature=${signature}&action=shorturl&format=json&url=${url}`)
headers: { if (res.data?.shorturl) {
token: c1nToken return res.data.shorturl
} }
} catch (e: any) {
if (e.response?.data?.message?.indexOf('already exists in database') !== -1) {
return e.response.data.shorturl
}
logger.error(e)
}
} else {
logger.warn('Yourls server or signature is not set')
}
return url
}
const generateCFWORKERShortUrl = async (url: string) => {
let cfWorkerHost = db.get(configPaths.settings.cfWorkerHost) || ''
cfWorkerHost = cfWorkerHost.replace(/\/$/, '')
if (cfWorkerHost) {
try {
const res = await axios.post(cfWorkerHost, {
url
}) })
if (res.status >= 200 && res.status < 300 && res.data?.code === 0) { if (res.data?.status === 200 && res.data?.key?.startsWith('/')) {
return res.data.data return `${cfWorkerHost}${res.data.key}`
} }
} catch (e: any) { } catch (e: any) {
logger.error(e) logger.error(e)
} }
} else if (server === IShortUrlServer.YOURLS) { } else {
let domain = db.get(configPaths.settings.yourlsDomain) || '' logger.warn('CF Worker host is not set')
const signature = db.get(configPaths.settings.yourlsSignature) || ''
if (domain && signature) {
if (!/^https?:\/\//.test(domain)) {
domain = `http://${domain}`
}
try {
const res = await axios.get(`${domain}/yourls-api.php?signature=${signature}&action=shorturl&format=json&url=${url}`)
if (res.data.shorturl) {
return res.data.shorturl
}
} catch (e: any) {
if (e.response.data.message.indexOf('already exists in database') !== -1) {
return e.response.data.shorturl
}
logger.error(e)
}
} else {
logger.warn('Yourls server or signature is not set')
}
} else if (server === IShortUrlServer.CFWORKER) {
let cfWorkerHost = db.get(configPaths.settings.cfWorkerHost) || ''
cfWorkerHost = cfWorkerHost.replace(/\/$/, '')
if (cfWorkerHost) {
try {
const res = await axios.post(cfWorkerHost, {
url
})
if (res.data.status === 200 && res.data.key.startsWith('/')) {
return `${cfWorkerHost}${res.data.key}`
}
} catch (e: any) {
logger.error(e)
}
} else {
logger.warn('CF Worker host is not set')
}
} }
return url return url
} }
export const generateShortUrl = async (url: string) => {
const server = db.get(configPaths.settings.shortUrlServer) || IShortUrlServer.C1N
switch (server) {
case IShortUrlServer.C1N:
return generateC1NShortUrl(url)
case IShortUrlServer.YOURLS:
return generateYOURLSShortUrl(url)
case IShortUrlServer.CFWORKER:
return generateCFWORKERShortUrl(url)
default:
return url
}
}