2023-02-15 10:36:47 -05:00
|
|
|
import OSS from 'ali-oss'
|
|
|
|
|
2023-04-26 05:01:55 -04:00
|
|
|
interface IConfigMap {
|
|
|
|
fileName: string
|
2024-04-09 03:03:32 -04:00
|
|
|
config: PartialKeys<IAliYunConfig, 'path'>
|
2023-04-26 05:01:55 -04:00
|
|
|
}
|
|
|
|
|
2023-02-15 10:36:47 -05:00
|
|
|
export default class AliyunApi {
|
2024-04-15 11:12:02 -04:00
|
|
|
static #getKey (fileName: string, path?: string): string {
|
2023-06-29 08:00:46 -04:00
|
|
|
return path && path !== '/'
|
2023-08-07 04:36:27 -04:00
|
|
|
? `${path.replace(/^\/+|\/+$/, '')}/${fileName}`
|
2023-06-29 08:00:46 -04:00
|
|
|
: fileName
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:01:55 -04:00
|
|
|
static async delete (configMap: IConfigMap): Promise<boolean> {
|
2023-06-29 08:00:46 -04:00
|
|
|
const { fileName, config } = configMap
|
2023-02-15 10:36:47 -05:00
|
|
|
try {
|
2024-04-15 11:12:02 -04:00
|
|
|
const client = new OSS({ ...config, region: config.area })
|
|
|
|
const key = AliyunApi.#getKey(fileName, config.path)
|
2023-08-12 04:00:40 -04:00
|
|
|
const result = await client.delete(key)
|
2023-02-15 10:36:47 -05:00
|
|
|
return result.res.status === 204
|
|
|
|
} catch (error) {
|
2023-06-29 08:00:46 -04:00
|
|
|
console.error(error)
|
2023-02-15 10:36:47 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|