mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-07-20 04:11:50 +08:00
🔨 Refactor(custom): refactor private to sharp in es6
This commit is contained in:
@@ -6,17 +6,7 @@ interface IConfigMap {
|
||||
}
|
||||
|
||||
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 {
|
||||
static #getKey (fileName: string, path?: string): string {
|
||||
return path && path !== '/'
|
||||
? `${path.replace(/^\/+|\/+$/, '')}/${fileName}`
|
||||
: fileName
|
||||
@@ -25,8 +15,8 @@ export default class AliyunApi {
|
||||
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 client = new OSS({ ...config, region: config.area })
|
||||
const key = AliyunApi.#getKey(fileName, config.path)
|
||||
const result = await client.delete(key)
|
||||
return result.res.status === 204
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,13 +7,13 @@ interface IConfigMap {
|
||||
}
|
||||
|
||||
export default class GithubApi {
|
||||
private static createOctokit (token: string) {
|
||||
static #createOctokit (token: string) {
|
||||
return new Octokit({
|
||||
auth: token
|
||||
})
|
||||
}
|
||||
|
||||
private static createKey (path: string | undefined, fileName: string): string {
|
||||
static #createKey (path: string | undefined, fileName: string): string {
|
||||
const formatedFileName = fileName.replace(/%2F/g, '/')
|
||||
return path && path !== '/'
|
||||
? `${path.replace(/^\/+|\/+$/, '')}/${formatedFileName}`
|
||||
@@ -23,8 +23,8 @@ export default class GithubApi {
|
||||
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)
|
||||
const octokit = GithubApi.#createOctokit(token)
|
||||
const key = GithubApi.#createKey(path, fileName)
|
||||
try {
|
||||
const { status } = await octokit.rest.repos.deleteFile({
|
||||
owner,
|
||||
|
||||
@@ -5,28 +5,8 @@ interface IConfigMap {
|
||||
hash?: string
|
||||
}
|
||||
|
||||
interface IConfig {
|
||||
headers: {
|
||||
Authorization: string
|
||||
}
|
||||
timeout: number
|
||||
}
|
||||
|
||||
export default class ImgurApi {
|
||||
static baseUrl = 'https://api.imgur.com/3'
|
||||
private static async makeRequest (
|
||||
method: 'delete',
|
||||
url: string,
|
||||
config: IConfig
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response: AxiosResponse = await axios[method](url, config)
|
||||
return response.status === 200
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
static #baseUrl = 'https://api.imgur.com/3'
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const {
|
||||
@@ -37,17 +17,22 @@ export default class ImgurApi {
|
||||
|
||||
if (username && accessToken) {
|
||||
Authorization = `Bearer ${accessToken}`
|
||||
apiUrl = `${ImgurApi.baseUrl}/account/${username}/image/${hash}`
|
||||
apiUrl = `${ImgurApi.#baseUrl}/account/${username}/image/${hash}`
|
||||
} else if (clientId) {
|
||||
Authorization = `Client-ID ${clientId}`
|
||||
apiUrl = `${ImgurApi.baseUrl}/image/${hash}`
|
||||
apiUrl = `${ImgurApi.#baseUrl}/image/${hash}`
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
const requestConfig: IConfig = {
|
||||
headers: { Authorization },
|
||||
timeout: 30000
|
||||
try {
|
||||
const response: AxiosResponse = await axios.delete(apiUrl, {
|
||||
headers: { Authorization },
|
||||
timeout: 30000
|
||||
})
|
||||
return response.status === 200
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return false
|
||||
}
|
||||
return ImgurApi.makeRequest('delete', apiUrl, requestConfig)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ interface IConfigMap {
|
||||
}
|
||||
|
||||
export default class SmmsApi {
|
||||
private static readonly baseUrl = 'https://smms.app/api/v2'
|
||||
static readonly #baseUrl = 'https://smms.app/api/v2'
|
||||
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { hash, config } = configMap
|
||||
@@ -19,7 +19,7 @@ export default class SmmsApi {
|
||||
|
||||
try {
|
||||
const response: AxiosResponse = await axios.get(
|
||||
`${SmmsApi.baseUrl}/delete/${hash}`, {
|
||||
`${SmmsApi.#baseUrl}/delete/${hash}`, {
|
||||
headers: {
|
||||
Authorization: token
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ interface IConfigMap {
|
||||
}
|
||||
|
||||
export default class TcyunApi {
|
||||
private static createCOS (SecretId: string, SecretKey: string): COS {
|
||||
static #createCOS (SecretId: string, SecretKey: string): COS {
|
||||
return new COS({
|
||||
SecretId,
|
||||
SecretKey
|
||||
@@ -16,7 +16,7 @@ export default class TcyunApi {
|
||||
static async delete (configMap: IConfigMap): Promise<boolean> {
|
||||
const { fileName, config: { secretId, secretKey, bucket, area, path } } = configMap
|
||||
try {
|
||||
const cos = TcyunApi.createCOS(secretId, secretKey)
|
||||
const cos = TcyunApi.#createCOS(secretId, secretKey)
|
||||
let key
|
||||
if (path === '/' || !path) {
|
||||
key = `/${fileName}`
|
||||
|
||||
@@ -5,45 +5,45 @@ import bus from '@/utils/bus'
|
||||
import { builtinI18nList } from '#/i18n'
|
||||
|
||||
export class I18nManager {
|
||||
private i18n: I18n | null = null
|
||||
private i18nFileList: II18nItem[] = builtinI18nList
|
||||
#i18n: I18n | null = null
|
||||
#i18nFileList: II18nItem[] = builtinI18nList
|
||||
|
||||
private getLanguageList () {
|
||||
#getLanguageList () {
|
||||
ipcRenderer.send(GET_LANGUAGE_LIST)
|
||||
ipcRenderer.once(GET_LANGUAGE_LIST, (event, list: II18nItem[]) => {
|
||||
this.i18nFileList = list
|
||||
this.#i18nFileList = list
|
||||
})
|
||||
}
|
||||
|
||||
private getCurrentLanguage () {
|
||||
#getCurrentLanguage () {
|
||||
ipcRenderer.send(GET_CURRENT_LANGUAGE)
|
||||
ipcRenderer.once(GET_CURRENT_LANGUAGE, (event, lang: string, locales: ILocales) => {
|
||||
this.setLocales(lang, locales)
|
||||
this.#setLocales(lang, locales)
|
||||
bus.emit(FORCE_UPDATE)
|
||||
})
|
||||
}
|
||||
|
||||
private setLocales (lang: string, locales: ILocales) {
|
||||
#setLocales (lang: string, locales: ILocales) {
|
||||
const objectAdapter = new ObjectAdapter({
|
||||
[lang]: locales
|
||||
})
|
||||
this.i18n = new I18n({
|
||||
this.#i18n = new I18n({
|
||||
adapter: objectAdapter,
|
||||
defaultLanguage: lang
|
||||
})
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this.getCurrentLanguage()
|
||||
this.getLanguageList()
|
||||
this.#getCurrentLanguage()
|
||||
this.#getLanguageList()
|
||||
ipcRenderer.on(SET_CURRENT_LANGUAGE, (event, lang: string, locales: ILocales) => {
|
||||
this.setLocales(lang, locales)
|
||||
this.#setLocales(lang, locales)
|
||||
bus.emit(FORCE_UPDATE)
|
||||
})
|
||||
}
|
||||
|
||||
T (key: ILocalesKey, args: IStringKeyMap = {}): string {
|
||||
return this.i18n?.translate(key, args) || key
|
||||
return this.#i18n?.translate(key, args) || key
|
||||
}
|
||||
|
||||
setCurrentLanguage (lang: string) {
|
||||
@@ -51,7 +51,7 @@ export class I18nManager {
|
||||
}
|
||||
|
||||
get languageList () {
|
||||
return this.i18nFileList
|
||||
return this.#i18nFileList
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,36 +25,36 @@ import { getRawData } from './common'
|
||||
|
||||
export class GalleryDB implements IGalleryDB {
|
||||
async get<T> (filter?: IFilter): Promise<IGetResult<T>> {
|
||||
const res = await this.msgHandler<IGetResult<T>>(PICGO_GET_DB, filter)
|
||||
const res = await this.#msgHandler<IGetResult<T>>(PICGO_GET_DB, filter)
|
||||
return res
|
||||
}
|
||||
|
||||
async insert<T> (value: T): Promise<IResult<T>> {
|
||||
const res = await this.msgHandler<IResult<T>>(PICGO_INSERT_DB, value)
|
||||
const res = await this.#msgHandler<IResult<T>>(PICGO_INSERT_DB, value)
|
||||
return res
|
||||
}
|
||||
|
||||
async insertMany<T> (value: T[]): Promise<IResult<T>[]> {
|
||||
const res = await this.msgHandler<IResult<T>[]>(PICGO_INSERT_MANY_DB, value)
|
||||
const res = await this.#msgHandler<IResult<T>[]>(PICGO_INSERT_MANY_DB, value)
|
||||
return res
|
||||
}
|
||||
|
||||
async updateById (id: string, value: IObject): Promise<boolean> {
|
||||
const res = await this.msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value)
|
||||
const res = await this.#msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value)
|
||||
return res
|
||||
}
|
||||
|
||||
async getById<T> (id: string): Promise<IResult<T> | undefined> {
|
||||
const res = await this.msgHandler<IResult<T> | undefined>(PICGO_GET_BY_ID_DB, id)
|
||||
const res = await this.#msgHandler<IResult<T> | undefined>(PICGO_GET_BY_ID_DB, id)
|
||||
return res
|
||||
}
|
||||
|
||||
async removeById (id: string): Promise<void> {
|
||||
const res = await this.msgHandler<void>(PICGO_REMOVE_BY_ID_DB, id)
|
||||
const res = await this.#msgHandler<void>(PICGO_REMOVE_BY_ID_DB, id)
|
||||
return res
|
||||
}
|
||||
|
||||
private msgHandler<T> (method: string, ...args: any[]): Promise<T> {
|
||||
#msgHandler<T> (method: string, ...args: any[]): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
const callbackId = uuid()
|
||||
const callback = (event: IpcRendererEvent, data: T, returnCallbackId: string) => {
|
||||
|
||||
Reference in New Issue
Block a user