mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-03-13 00:18:13 -04:00
🔨 Refactor(custom): refactor short url generation function
This commit is contained in:
parent
4a0a882de1
commit
f6936dae51
@ -116,14 +116,14 @@ export const handleUrlEncodeWithSetting = (url: string) => db.get(configPaths.se
|
|||||||
const c1nApi = 'https://c1n.cn/link/short'
|
const c1nApi = 'https://c1n.cn/link/short'
|
||||||
|
|
||||||
const generateC1NShortUrl = async (url: string) => {
|
const generateC1NShortUrl = async (url: string) => {
|
||||||
const form = new FormData()
|
|
||||||
form.append('url', url)
|
|
||||||
const c1nToken = db.get(configPaths.settings.c1nToken) || ''
|
const c1nToken = db.get(configPaths.settings.c1nToken) || ''
|
||||||
if (!c1nToken) {
|
if (!c1nToken) {
|
||||||
logger.warn('c1n token is not set')
|
logger.warn('c1n token is not set')
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
const form = new FormData()
|
||||||
|
form.append('url', url)
|
||||||
const res = await axios.post(c1nApi, form, {
|
const res = await axios.post(c1nApi, form, {
|
||||||
headers: {
|
headers: {
|
||||||
token: c1nToken
|
token: c1nToken
|
||||||
@ -141,44 +141,47 @@ const generateC1NShortUrl = async (url: string) => {
|
|||||||
const generateYOURLSShortUrl = async (url: string) => {
|
const generateYOURLSShortUrl = async (url: string) => {
|
||||||
let domain = db.get(configPaths.settings.yourlsDomain) || ''
|
let domain = db.get(configPaths.settings.yourlsDomain) || ''
|
||||||
const signature = db.get(configPaths.settings.yourlsSignature) || ''
|
const signature = db.get(configPaths.settings.yourlsSignature) || ''
|
||||||
if (domain && signature) {
|
|
||||||
if (!/^https?:\/\//.test(domain)) {
|
if (!domain || !signature) {
|
||||||
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')
|
logger.warn('Yourls server or signature is not set')
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
if (!/^https?:\/\//.test(domain)) {
|
||||||
|
domain = `http://${domain}`
|
||||||
|
}
|
||||||
|
const params = new URLSearchParams({ signature, action: 'shorturl', format: 'json', url })
|
||||||
|
try {
|
||||||
|
const res = await axios.get(`${domain}/yourls-api.php?${params.toString()}`)
|
||||||
|
if (res.data?.shorturl) {
|
||||||
|
return res.data.shorturl
|
||||||
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
if (e.response?.data?.message?.includes('already exists in database')) {
|
||||||
|
return e.response.data.shorturl
|
||||||
|
}
|
||||||
|
logger.error(e)
|
||||||
|
}
|
||||||
|
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
const generateCFWORKERShortUrl = async (url: string) => {
|
const generateCFWORKERShortUrl = async (url: string) => {
|
||||||
let cfWorkerHost = db.get(configPaths.settings.cfWorkerHost) || ''
|
let cfWorkerHost = db.get(configPaths.settings.cfWorkerHost) || ''
|
||||||
cfWorkerHost = cfWorkerHost.replace(/\/$/, '')
|
cfWorkerHost = cfWorkerHost.replace(/\/$/, '')
|
||||||
if (cfWorkerHost) {
|
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')
|
logger.warn('CF Worker host is not set')
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user