mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 14:48:13 -05:00
🔨 Refactor: refactor of some apis
This commit is contained in:
parent
6680434231
commit
8a70975cda
@ -12,25 +12,31 @@ interface IConfigMap {
|
||||
}
|
||||
|
||||
export default class AliyunApi {
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, config: { accessKeyId, accessKeySecret, bucket, area, path } } = configMap
|
||||
try {
|
||||
const client = new OSS({
|
||||
private static createClient (config: IConfigMap['config']): OSS {
|
||||
const { accessKeyId, accessKeySecret, bucket, area } = config
|
||||
return new OSS({
|
||||
accessKeyId,
|
||||
accessKeySecret,
|
||||
bucket,
|
||||
region: area
|
||||
})
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = fileName
|
||||
} else {
|
||||
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
}
|
||||
|
||||
private static getKey (fileName: string, path?: string): string {
|
||||
return path && path !== '/'
|
||||
? `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
: fileName
|
||||
}
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, config } = configMap
|
||||
try {
|
||||
const client = AliyunApi.createClient(config)
|
||||
const key = AliyunApi.getKey(fileName, config.path)
|
||||
const result = await client.delete(key) as any
|
||||
return result.res.status === 204
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -22,10 +22,10 @@ const apiMap: IStringKeyMap = {
|
||||
|
||||
export default class ALLApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
if (apiMap[configMap.type] !== undefined) {
|
||||
return await apiMap[configMap.type].delete(configMap)
|
||||
} else {
|
||||
const api = apiMap[configMap.type]
|
||||
if (api) {
|
||||
return await api.delete(configMap)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,36 @@
|
||||
import { Octokit } from '@octokit/rest'
|
||||
|
||||
interface IConfigMap {
|
||||
fileName: string
|
||||
hash: string
|
||||
config: {
|
||||
repo: string
|
||||
token: string
|
||||
branch: string
|
||||
path?: string
|
||||
}
|
||||
}
|
||||
|
||||
export default class GithubApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { fileName, hash, config: { repo, token, branch, path } } = configMap
|
||||
const owner = repo.split('/')[0]
|
||||
const repoName = repo.split('/')[1]
|
||||
const octokit = new Octokit({
|
||||
private static createOctokit (token: string) {
|
||||
return new Octokit({
|
||||
auth: token
|
||||
})
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = fileName
|
||||
} else {
|
||||
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
}
|
||||
|
||||
private static createKey (path: string | undefined, fileName: string): string {
|
||||
return path && path !== '/'
|
||||
? `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
: fileName
|
||||
}
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, hash, config: { repo, token, branch, path } } = configMap
|
||||
const [owner, repoName] = repo.split('/')
|
||||
const octokit = GithubApi.createOctokit(token)
|
||||
const key = GithubApi.createKey(path, fileName)
|
||||
try {
|
||||
const result = await octokit.rest.repos.deleteFile({
|
||||
const { status } = await octokit.rest.repos.deleteFile({
|
||||
owner,
|
||||
repo: repoName,
|
||||
path: key,
|
||||
@ -23,9 +38,9 @@ export default class GithubApi {
|
||||
sha: hash,
|
||||
branch
|
||||
})
|
||||
return result.status === 200
|
||||
return status === 200
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,45 @@
|
||||
import axios from 'axios'
|
||||
import axios, { AxiosResponse } from 'axios'
|
||||
|
||||
interface IConfigMap {
|
||||
config?: {
|
||||
clientId?: string
|
||||
username?: string
|
||||
accessToken?: string
|
||||
}
|
||||
hash?: string
|
||||
}
|
||||
|
||||
interface IConfig {
|
||||
headers: {
|
||||
Authorization: string
|
||||
}
|
||||
timeout: number
|
||||
}
|
||||
|
||||
export default class ImgurApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
private static async makeRequest (
|
||||
method: 'delete',
|
||||
url: string,
|
||||
config: IConfig,
|
||||
logError: boolean = true
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response: AxiosResponse = await axios[method](url, config)
|
||||
return response.status === 200
|
||||
} catch (error) {
|
||||
if (logError) {
|
||||
console.error(error)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { config = {}, hash = '' } = configMap || {}
|
||||
const { clientId = '', username = '', accessToken = '' } = config
|
||||
const baseUrl = 'https://api.imgur.com/3'
|
||||
let Authorization
|
||||
let apiUrl
|
||||
let Authorization: string
|
||||
let apiUrl: string
|
||||
if (username && accessToken) {
|
||||
Authorization = `Bearer ${accessToken}`
|
||||
apiUrl = `${baseUrl}/account/${username}/image/${hash}`
|
||||
@ -19,15 +52,10 @@ export default class ImgurApi {
|
||||
const headers = {
|
||||
Authorization
|
||||
}
|
||||
try {
|
||||
const res = await axios.delete(apiUrl, {
|
||||
const requestConfig: IConfig = {
|
||||
headers,
|
||||
timeout: 30000
|
||||
})
|
||||
return res.status === 200
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return ImgurApi.makeRequest('delete', apiUrl, requestConfig)
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,24 @@
|
||||
import Qiniu from 'qiniu'
|
||||
|
||||
interface IConfigMap {
|
||||
fileName: string;
|
||||
config: {
|
||||
accessKey: string;
|
||||
secretKey: string;
|
||||
bucket: string;
|
||||
path?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export default class QiniuApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, config: { accessKey, secretKey, bucket, path } } = configMap
|
||||
const mac = new Qiniu.auth.digest.Mac(accessKey, secretKey)
|
||||
const qiniuConfig = new Qiniu.conf.Config()
|
||||
try {
|
||||
const bucketManager = new Qiniu.rs.BucketManager(mac, qiniuConfig)
|
||||
let key = ''
|
||||
if (path === '/' || !path) {
|
||||
key = fileName
|
||||
} else {
|
||||
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
}
|
||||
const formattedPath = path?.replace(/^\//, '').replace(/\/$/, '') || ''
|
||||
const key = path === '/' || !path ? fileName : `${formattedPath}/${fileName}`
|
||||
const res = await new Promise((resolve, reject) => {
|
||||
bucketManager.delete(bucket, key, (err, respBody, respInfo) => {
|
||||
if (err) {
|
||||
@ -27,7 +33,7 @@ export default class QiniuApi {
|
||||
}) as any
|
||||
return res && res.respInfo.statusCode === 200
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,26 @@
|
||||
import axios from 'axios'
|
||||
import axios, { AxiosResponse } from 'axios'
|
||||
|
||||
interface IConfigMap {
|
||||
hash?: string
|
||||
config?: {
|
||||
token?: string
|
||||
}
|
||||
}
|
||||
|
||||
export default class SmmsApi {
|
||||
private static readonly baseUrl = 'https://smms.app/api/v2'
|
||||
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { hash, config: { token } } = configMap
|
||||
if (!hash || !token) {
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { hash, config } = configMap
|
||||
if (!hash || !config || !config.token) {
|
||||
console.error('SmmsApi.delete: invalid params')
|
||||
return false
|
||||
}
|
||||
|
||||
const { token } = config
|
||||
|
||||
try {
|
||||
const res = await axios.get(
|
||||
const response: AxiosResponse = await axios.get(
|
||||
`${SmmsApi.baseUrl}/delete/${hash}`, {
|
||||
headers: {
|
||||
Authorization: token
|
||||
@ -20,9 +31,9 @@ export default class SmmsApi {
|
||||
},
|
||||
timeout: 30000
|
||||
})
|
||||
return res.status === 200
|
||||
return response.status === 200
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,28 @@
|
||||
import COS from 'cos-nodejs-sdk-v5'
|
||||
|
||||
interface IConfigMap {
|
||||
fileName: string;
|
||||
config: {
|
||||
secretId: string;
|
||||
secretKey: string;
|
||||
bucket: string;
|
||||
area: string;
|
||||
path?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default class TcyunApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
private static createCOS (SecretId: string, SecretKey: string): COS {
|
||||
return new COS({
|
||||
SecretId,
|
||||
SecretKey
|
||||
})
|
||||
}
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, config: { secretId, secretKey, bucket, area, path } } = configMap
|
||||
try {
|
||||
const cos = new COS({
|
||||
SecretId: secretId,
|
||||
SecretKey: secretKey
|
||||
})
|
||||
const cos = TcyunApi.createCOS(secretId, secretKey)
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = `/${fileName}`
|
||||
@ -21,7 +36,7 @@ export default class TcyunApi {
|
||||
})
|
||||
return result.statusCode === 204
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user