2023-04-17 05:26:49 -04:00
|
|
|
import axios from 'axios'
|
|
|
|
import FormData from 'form-data'
|
|
|
|
import { C1 } from './static'
|
|
|
|
|
2023-02-28 21:37:58 -05:00
|
|
|
export const isUrl = (url: string): boolean => {
|
|
|
|
try {
|
2023-03-01 04:39:55 -05:00
|
|
|
return Boolean(new URL(url))
|
2023-02-28 21:37:58 -05:00
|
|
|
} catch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:30:58 -04:00
|
|
|
export const isUrlEncode = (url: string): boolean => {
|
|
|
|
url = url || ''
|
|
|
|
try {
|
2022-10-24 05:14:39 -04:00
|
|
|
return url !== decodeURI(url)
|
2020-06-28 03:30:58 -04:00
|
|
|
} catch (e) {
|
|
|
|
// if some error caught, try to let it go
|
2023-03-25 09:39:40 -04:00
|
|
|
return false
|
2020-06-28 03:30:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 00:52:35 -04:00
|
|
|
export const handleUrlEncode = (url: string): string => isUrlEncode(url) ? url : encodeURI(url)
|
2020-12-26 10:39:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* streamline the full plugin name to a simple one
|
|
|
|
* for example:
|
|
|
|
* 1. picgo-plugin-xxx -> xxx
|
|
|
|
* 2. @xxx/picgo-plugin-yyy -> yyy
|
|
|
|
* @param name pluginFullName
|
|
|
|
*/
|
|
|
|
export const handleStreamlinePluginName = (name: string) => {
|
|
|
|
if (/^@[^/]+\/picgo-plugin-/.test(name)) {
|
|
|
|
return name.replace(/^@[^/]+\/picgo-plugin-/, '')
|
|
|
|
} else {
|
|
|
|
return name.replace(/picgo-plugin-/, '')
|
|
|
|
}
|
|
|
|
}
|
2022-06-12 08:20:08 -04:00
|
|
|
|
|
|
|
export const simpleClone = (obj: any) => {
|
|
|
|
return JSON.parse(JSON.stringify(obj))
|
|
|
|
}
|
2022-07-31 05:47:15 -04:00
|
|
|
|
|
|
|
export const enforceNumber = (num: number | string) => {
|
2023-05-11 00:52:35 -04:00
|
|
|
return isNaN(+num) ? 0 : +num
|
2022-07-31 05:47:15 -04:00
|
|
|
}
|
2022-10-30 04:08:29 -04:00
|
|
|
|
|
|
|
export const isDev = process.env.NODE_ENV === 'development'
|
2022-12-31 07:44:19 -05:00
|
|
|
|
2023-05-11 00:52:35 -04:00
|
|
|
export const trimValues = <T extends IStringKeyMap>(obj: T): {[K in keyof T]: T[K] extends string ? string : T[K]} => {
|
|
|
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, typeof value === 'string' ? value.trim() : value])) as {[K in keyof T]: T[K] extends string ? string : T[K]}
|
2022-12-31 07:44:19 -05:00
|
|
|
}
|
2023-04-17 05:26:49 -04:00
|
|
|
|
|
|
|
const c1nApi = 'https://c1n.cn/link/short'
|
|
|
|
|
|
|
|
export const generateShortUrl = async (url: string) => {
|
|
|
|
const form = new FormData()
|
|
|
|
form.append('url', url)
|
|
|
|
const C = Buffer.from(C1, 'base64').toString()
|
|
|
|
try {
|
|
|
|
const res = await axios.post(c1nApi, form, {
|
|
|
|
headers: {
|
|
|
|
token: C
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (res.status >= 200 && res.status < 300 && res.data?.code === 0) {
|
|
|
|
return res.data.data
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|