mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-25 10:10:21 +08:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { AuthType, WebDAVClientOptions, createClient } from 'webdav'
|
|
|
|
import { deleteFailedLog, deleteLog } from '#/utils/deleteLog'
|
|
import { formatEndpoint } from '#/utils/common'
|
|
|
|
interface IConfigMap {
|
|
fileName: string
|
|
config: PartialKeys<IWebdavPlistConfig, 'path'>
|
|
}
|
|
|
|
export default class WebdavApi {
|
|
static async delete (configMap: IConfigMap): Promise<boolean> {
|
|
const { fileName, config: { host, username, password, path, sslEnabled, authType } } = configMap
|
|
const endpoint = formatEndpoint(host, sslEnabled)
|
|
const options: WebDAVClientOptions = {
|
|
username,
|
|
password
|
|
}
|
|
if (authType === 'digest') {
|
|
options.authType = AuthType.Digest
|
|
}
|
|
const ctx = createClient(
|
|
endpoint,
|
|
options
|
|
)
|
|
let key
|
|
if (path === '/' || !path) {
|
|
key = fileName
|
|
} else {
|
|
key = `${path.replace(/^\/+|\/+$/, '')}/${fileName}`
|
|
}
|
|
try {
|
|
await ctx.deleteFile(key)
|
|
deleteLog(fileName, 'WebDAV')
|
|
return true
|
|
} catch (error: any) {
|
|
deleteFailedLog(fileName, 'WebDAV', error)
|
|
return false
|
|
}
|
|
}
|
|
}
|