mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-07-26 16:29:22 +08:00
✨ Feature: add remote file delete , picBed management
First version of PicList. In album, you can delete remote file now. Add picBed management function.
This commit is contained in:
25
src/renderer/apis/aliyun.ts
Normal file
25
src/renderer/apis/aliyun.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import OSS from 'ali-oss'
|
||||
|
||||
export default class AliyunApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { fileName, config: { accessKeyId, accessKeySecret, bucket, area, path } } = configMap
|
||||
try {
|
||||
const client = new OSS({
|
||||
accessKeyId,
|
||||
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
|
||||
return result.res.status === 204
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/renderer/apis/allApi.ts
Normal file
27
src/renderer/apis/allApi.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import SmmsApi from './smms'
|
||||
import TcyunApi from './tcyun'
|
||||
import AliyunApi from './aliyun'
|
||||
import QiniuApi from './qiniu'
|
||||
import ImgurApi from './imgur'
|
||||
import GithubApi from './github'
|
||||
import UpyunApi from './upyun'
|
||||
|
||||
const apiMap: IStringKeyMap = {
|
||||
smms: SmmsApi,
|
||||
tcyun: TcyunApi,
|
||||
aliyun: AliyunApi,
|
||||
qiniu: QiniuApi,
|
||||
imgur: ImgurApi,
|
||||
github: GithubApi,
|
||||
upyun: UpyunApi
|
||||
}
|
||||
|
||||
export default class ALLApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
if (apiMap[configMap.type] !== undefined) {
|
||||
return await apiMap[configMap.type].delete(configMap)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/renderer/apis/github.ts
Normal file
31
src/renderer/apis/github.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Octokit } from '@octokit/rest'
|
||||
|
||||
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({
|
||||
auth: token
|
||||
})
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = fileName
|
||||
} else {
|
||||
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
}
|
||||
try {
|
||||
const result = await octokit.rest.repos.deleteFile({
|
||||
owner,
|
||||
repo: repoName,
|
||||
path: key,
|
||||
message: `delete ${fileName} by PicList`,
|
||||
sha: hash,
|
||||
branch
|
||||
})
|
||||
return result.status === 200
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/renderer/apis/imgur.ts
Normal file
21
src/renderer/apis/imgur.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export default class ImgurApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const clientId = configMap.config.clientId
|
||||
const { hash } = configMap
|
||||
const fullUrl = `https://api.imgur.com/3/image/${hash}`
|
||||
const headers = {
|
||||
Authorization: `Client-ID ${clientId}`
|
||||
}
|
||||
try {
|
||||
const res = await axios.delete(fullUrl, {
|
||||
headers,
|
||||
timeout: 10000
|
||||
})
|
||||
return res.status === 200
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/renderer/apis/qiniu.ts
Normal file
33
src/renderer/apis/qiniu.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import Qiniu from 'qiniu'
|
||||
|
||||
export default class QiniuApi {
|
||||
static async delete (configMap: IStringKeyMap): 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 res = await new Promise((resolve, reject) => {
|
||||
bucketManager.delete(bucket, key, (err, respBody, respInfo) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve({
|
||||
respBody,
|
||||
respInfo
|
||||
})
|
||||
}
|
||||
})
|
||||
}) as any
|
||||
return res && res.respInfo.statusCode === 200
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/renderer/apis/smms.ts
Normal file
23
src/renderer/apis/smms.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export default class SmmsApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { hash, config: { token } } = configMap
|
||||
if (!hash || !token) {
|
||||
return false
|
||||
} else {
|
||||
const res = await axios.get(
|
||||
`https://smms.app/api/v2/delete/${hash}`, {
|
||||
headers: {
|
||||
Authorization: token
|
||||
},
|
||||
params: {
|
||||
hash,
|
||||
format: 'json'
|
||||
},
|
||||
timeout: 10000
|
||||
})
|
||||
return res.status === 200
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/renderer/apis/tcyun.ts
Normal file
27
src/renderer/apis/tcyun.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import COS from 'cos-nodejs-sdk-v5'
|
||||
|
||||
export default class TcyunApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { fileName, config: { secretId, secretKey, bucket, area, path } } = configMap
|
||||
try {
|
||||
const cos = new COS({
|
||||
SecretId: secretId,
|
||||
SecretKey: secretKey
|
||||
})
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = `/${fileName}`
|
||||
} else {
|
||||
key = `/${path.replace(/^\//, '').replace(/\/$/, '')}${fileName}`
|
||||
}
|
||||
const result = await cos.deleteObject({
|
||||
Bucket: bucket,
|
||||
Region: area,
|
||||
Key: key
|
||||
})
|
||||
return result.statusCode === 204
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/renderer/apis/upyun.ts
Normal file
22
src/renderer/apis/upyun.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
// @ts-ignore
|
||||
import Upyun from 'upyun'
|
||||
|
||||
export default class UpyunApi {
|
||||
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { fileName, config: { bucket, operator, password, path } } = configMap
|
||||
try {
|
||||
const service = new Upyun.Service(bucket, operator, password)
|
||||
const client = new Upyun.Client(service)
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = fileName
|
||||
} else {
|
||||
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
||||
}
|
||||
const result = await client.deleteFile(key)
|
||||
return result
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user