🐛 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

@ -67,13 +67,14 @@
"mitt": "^3.0.1",
"node-ssh-no-cpu-features": "^1.0.1",
"nodejs-file-downloader": "^4.12.1",
"piclist": "^1.1.2",
"piclist": "^1.1.4",
"pinia": "^2.1.6",
"pinia-plugin-persistedstate": "^3.2.0",
"qiniu": "^7.9.0",
"qrcode.vue": "^3.4.1",
"querystring": "^0.2.1",
"shell-path": "2.1.0",
"ssh2-no-cpu-features": "^1.0.0",
"upyun": "^3.4.6",
"uuid": "^9.0.0",
"video.js": "^8.5.2",

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
}
}

View File

@ -944,7 +944,7 @@ export const supportedPicBedList: IStringKeyMap = {
default: '0755'
},
baseDir: {
required: true,
required: false,
description: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_DESC'),
placeholder: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_PLACEHOLDER'),
type: 'string',

View File

@ -12639,10 +12639,10 @@ performance-now@^2.1.0:
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==
piclist@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.1.2.tgz#b008fd8814cd24e10a165cf056fe1b3f32aad140"
integrity sha512-QSdd9UERrDTkwrBTGu9uYHPIfBY+AZ4deEeKwe4VmTTVhpNi1J/zoAQPVwqpWv7JHktHPh5S2pDb4z9IdEVoNg==
piclist@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.1.4.tgz#11a1bf77afe83d245cf86980a9457a3e03341618"
integrity sha512-GnFUsLlBO3DvLAsZxudD7AWNtqNfaCTtMhQnTaBuqgMweLNE+7/Pjvxyv3rzI9wXpbs+Ylemff4uaKvWy2t2kw==
dependencies:
"@picgo/i18n" "^1.0.0"
"@picgo/store" "^2.1.0"
@ -14567,7 +14567,7 @@ sprintf-js@~1.0.2:
ssh2-no-cpu-features@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/ssh2-no-cpu-features/-/ssh2-no-cpu-features-1.0.0.tgz#641a8ea5331638ea9bfdee9e53f38ebfc46bf8cf"
resolved "https://registry.yarnpkg.com/ssh2-no-cpu-features/-/ssh2-no-cpu-features-1.0.0.tgz#641a8ea5331638ea9bfdee9e53f38ebfc46bf8cf"
integrity sha512-94HbS6PbjOvGYnWQ0OFlGLWp3yw6BfSAmpVlCRvsqqyfWa86gCorpnXFJLLJklnfCbp6UiZhEKzm3W+8vKCHqw==
dependencies:
asn1 "^0.2.6"