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