mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-08 21:38:13 -05:00
🐛 Fix: fix sftp delete error when the user don't have ssh permission
ISSUES CLOSED: #100
This commit is contained in:
parent
ef1812a8eb
commit
819dfbb2f1
@ -67,13 +67,14 @@
|
|||||||
"mitt": "^3.0.1",
|
"mitt": "^3.0.1",
|
||||||
"node-ssh-no-cpu-features": "^1.0.1",
|
"node-ssh-no-cpu-features": "^1.0.1",
|
||||||
"nodejs-file-downloader": "^4.12.1",
|
"nodejs-file-downloader": "^4.12.1",
|
||||||
"piclist": "^1.1.2",
|
"piclist": "^1.1.4",
|
||||||
"pinia": "^2.1.6",
|
"pinia": "^2.1.6",
|
||||||
"pinia-plugin-persistedstate": "^3.2.0",
|
"pinia-plugin-persistedstate": "^3.2.0",
|
||||||
"qiniu": "^7.9.0",
|
"qiniu": "^7.9.0",
|
||||||
"qrcode.vue": "^3.4.1",
|
"qrcode.vue": "^3.4.1",
|
||||||
"querystring": "^0.2.1",
|
"querystring": "^0.2.1",
|
||||||
"shell-path": "2.1.0",
|
"shell-path": "2.1.0",
|
||||||
|
"ssh2-no-cpu-features": "^1.0.0",
|
||||||
"upyun": "^3.4.6",
|
"upyun": "^3.4.6",
|
||||||
"uuid": "^9.0.0",
|
"uuid": "^9.0.0",
|
||||||
"video.js": "^8.5.2",
|
"video.js": "^8.5.2",
|
||||||
|
@ -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 {
|
try {
|
||||||
const client = SSHClient.instance
|
const client = SSHClient.instance
|
||||||
await client.connect(config)
|
await client.connect(config)
|
||||||
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
|
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
|
||||||
const remote = path.join(uploadPath, fileName)
|
const remote = path.join(uploadPath, fileName)
|
||||||
const deleteResult = await client.deleteFile(remote)
|
const deleteResult = await client.deleteFileSFTP(config, remote)
|
||||||
client.close()
|
client.close()
|
||||||
return deleteResult
|
return deleteResult
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@ -161,10 +161,10 @@ export const deleteChoosedFiles = async (list: ImgInfo[]): Promise<boolean[]> =>
|
|||||||
})
|
})
|
||||||
notification.show()
|
notification.show()
|
||||||
}
|
}
|
||||||
if (item.type === 'webdavplist') {
|
if (item.type === 'sftpplist') {
|
||||||
const { fileName, config } = item
|
const { fileName, config } = item
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
deleteWebdavFile(getRawData(config), fileName || '').then(noteFunc)
|
deleteSFTPFile(getRawData(config), fileName || '').then(noteFunc)
|
||||||
}, 0)
|
}, 0)
|
||||||
} else {
|
} else {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -174,7 +174,7 @@ export default {
|
|||||||
await client.connect(config)
|
await client.connect(config)
|
||||||
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
|
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
|
||||||
const remote = path.join(uploadPath, fileName)
|
const remote = path.join(uploadPath, fileName)
|
||||||
const deleteResult = await client.deleteFile(remote)
|
const deleteResult = await client.deleteFileSFTP(config, remote)
|
||||||
client.close()
|
client.close()
|
||||||
return deleteResult
|
return deleteResult
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
// @ts-nocheck
|
||||||
import { NodeSSH, Config, SSHExecCommandResponse } from 'node-ssh-no-cpu-features'
|
import { NodeSSH, Config, SSHExecCommandResponse } from 'node-ssh-no-cpu-features'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { ISftpPlistConfig } from 'piclist/dist/types'
|
import { ISftpPlistConfig } from 'piclist/dist/types'
|
||||||
|
import { Client } from 'ssh2-no-cpu-features'
|
||||||
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
class SSHClient {
|
class SSHClient {
|
||||||
// eslint-disable-next-line no-use-before-define
|
// eslint-disable-next-line no-use-before-define
|
||||||
@ -38,16 +41,35 @@ class SSHClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteFile (remote: string): Promise<boolean> {
|
public async deleteFileSFTP (config: ISftpPlistConfig, remote: string): Promise<boolean> {
|
||||||
if (!this._isConnected) {
|
|
||||||
throw new Error('SSH 未连接')
|
|
||||||
}
|
|
||||||
try {
|
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)
|
remote = this.changeWinStylePathToUnix(remote)
|
||||||
if (remote === '/' || remote.includes('*')) return false
|
if (remote === '/' || remote.includes('*')) return false
|
||||||
const script = `rm -f "${remote}"`
|
const promise = new Promise((resolve, reject) => {
|
||||||
return await this.exec(script)
|
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) {
|
} catch (err: any) {
|
||||||
|
console.log(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -944,7 +944,7 @@ export const supportedPicBedList: IStringKeyMap = {
|
|||||||
default: '0755'
|
default: '0755'
|
||||||
},
|
},
|
||||||
baseDir: {
|
baseDir: {
|
||||||
required: true,
|
required: false,
|
||||||
description: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_DESC'),
|
description: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_DESC'),
|
||||||
placeholder: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_PLACEHOLDER'),
|
placeholder: $T('MANAGE_CONSTANT_SFTP_BASE_DIR_PLACEHOLDER'),
|
||||||
type: 'string',
|
type: 'string',
|
||||||
|
10
yarn.lock
10
yarn.lock
@ -12639,10 +12639,10 @@ performance-now@^2.1.0:
|
|||||||
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
|
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
|
||||||
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==
|
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==
|
||||||
|
|
||||||
piclist@^1.1.2:
|
piclist@^1.1.4:
|
||||||
version "1.1.2"
|
version "1.1.4"
|
||||||
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.1.2.tgz#b008fd8814cd24e10a165cf056fe1b3f32aad140"
|
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.1.4.tgz#11a1bf77afe83d245cf86980a9457a3e03341618"
|
||||||
integrity sha512-QSdd9UERrDTkwrBTGu9uYHPIfBY+AZ4deEeKwe4VmTTVhpNi1J/zoAQPVwqpWv7JHktHPh5S2pDb4z9IdEVoNg==
|
integrity sha512-GnFUsLlBO3DvLAsZxudD7AWNtqNfaCTtMhQnTaBuqgMweLNE+7/Pjvxyv3rzI9wXpbs+Ylemff4uaKvWy2t2kw==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@picgo/i18n" "^1.0.0"
|
"@picgo/i18n" "^1.0.0"
|
||||||
"@picgo/store" "^2.1.0"
|
"@picgo/store" "^2.1.0"
|
||||||
@ -14567,7 +14567,7 @@ sprintf-js@~1.0.2:
|
|||||||
|
|
||||||
ssh2-no-cpu-features@^1.0.0:
|
ssh2-no-cpu-features@^1.0.0:
|
||||||
version "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==
|
integrity sha512-94HbS6PbjOvGYnWQ0OFlGLWp3yw6BfSAmpVlCRvsqqyfWa86gCorpnXFJLLJklnfCbp6UiZhEKzm3W+8vKCHqw==
|
||||||
dependencies:
|
dependencies:
|
||||||
asn1 "^0.2.6"
|
asn1 "^0.2.6"
|
||||||
|
Loading…
Reference in New Issue
Block a user