🐛 Fix: fix sftp delete error when the user don't have ssh permission

ISSUES CLOSED: #100
This commit is contained in:
Kuingsmile
2023-09-30 20:55:07 +08:00
parent ef1812a8eb
commit 819dfbb2f1
6 changed files with 41 additions and 18 deletions

View File

@@ -130,13 +130,13 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW
}
}
async function deleteWebdavFile (config: ISftpPlistConfig, fileName: string) {
async function deleteSFTPFile (config: ISftpPlistConfig, fileName: string) {
try {
const client = SSHClient.instance
await client.connect(config)
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
const remote = path.join(uploadPath, fileName)
const deleteResult = await client.deleteFile(remote)
const deleteResult = await client.deleteFileSFTP(config, remote)
client.close()
return deleteResult
} catch (err: any) {
@@ -161,10 +161,10 @@ export const deleteChoosedFiles = async (list: ImgInfo[]): Promise<boolean[]> =>
})
notification.show()
}
if (item.type === 'webdavplist') {
if (item.type === 'sftpplist') {
const { fileName, config } = item
setTimeout(() => {
deleteWebdavFile(getRawData(config), fileName || '').then(noteFunc)
deleteSFTPFile(getRawData(config), fileName || '').then(noteFunc)
}, 0)
} else {
setTimeout(() => {

View File

@@ -174,7 +174,7 @@ export default {
await client.connect(config)
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
const remote = path.join(uploadPath, fileName)
const deleteResult = await client.deleteFile(remote)
const deleteResult = await client.deleteFileSFTP(config, remote)
client.close()
return deleteResult
} catch (err: any) {

View File

@@ -1,6 +1,9 @@
// @ts-nocheck
import { NodeSSH, Config, SSHExecCommandResponse } from 'node-ssh-no-cpu-features'
import path from 'path'
import { ISftpPlistConfig } from 'piclist/dist/types'
import { Client } from 'ssh2-no-cpu-features'
import fs from 'fs-extra'
class SSHClient {
// eslint-disable-next-line no-use-before-define
@@ -38,16 +41,35 @@ class SSHClient {
}
}
public async deleteFile (remote: string): Promise<boolean> {
if (!this._isConnected) {
throw new Error('SSH 未连接')
}
public async deleteFileSFTP (config: ISftpPlistConfig, remote: string): Promise<boolean> {
try {
const client = new Client()
const { username, password, privateKey, passphrase } = config
const loginInfo: Config = privateKey
? { username, privateKey: fs.readFileSync(privateKey), passphrase: passphrase || undefined }
: { username, password }
remote = this.changeWinStylePathToUnix(remote)
if (remote === '/' || remote.includes('*')) return false
const script = `rm -f "${remote}"`
return await this.exec(script)
const promise = new Promise((resolve, reject) => {
client.on('ready', () => {
client.sftp((err, sftp) => {
if (err) reject(false)
sftp.unlink(remote, (err) => {
if (err) reject(false)
client.end()
resolve(true)
})
})
}).connect({
host: config.host,
port: Number(config.port) || 22,
...loginInfo
})
}
)
return await promise
} catch (err: any) {
console.log(err)
return false
}
}