🚧 WIP: working on build-in sftp picbed

This commit is contained in:
萌萌哒赫萝
2023-08-05 04:59:10 -07:00
parent b882d42e10
commit bdc11dad43
12 changed files with 225 additions and 87 deletions

View File

@@ -44,6 +44,8 @@ import { buildMainPageMenu, buildMiniPageMenu, buildPluginPageMenu, buildPicBedL
import path from 'path'
import { T } from '~/main/i18n'
import { uploadFile, downloadFile } from '../utils/syncSettings'
import SSHClient from '../utils/sshClient'
import { ISftpPlistConfig } from 'piclist'
const STORE_PATH = app.getPath('userData')
@@ -120,6 +122,21 @@ export default {
}
})
ipcMain.handle('delete-sftp-file', async (_evt: IpcMainInvokeEvent, config: ISftpPlistConfig, fileName: string) => {
try {
const client = SSHClient.instance
await client.connect(config)
const uploadPath = `/${(config.uploadPath || '').replace(/^\/+|\/+$/g, '')}/`.replace(/\/+/g, '/')
const remote = path.join(uploadPath, fileName)
const deleteResult = await client.deleteFile(remote)
client.close()
return deleteResult
} catch (err: any) {
console.error(err)
return false
}
})
ipcMain.handle('migrateFromPicGo', async () => {
const picGoConfigPath = STORE_PATH.replace('piclist', 'picgo')
const fileToMigration = [

View File

@@ -0,0 +1,64 @@
import { NodeSSH, Config } from 'node-ssh-no-cpu-features'
import { ISftpPlistConfig } from 'piclist/dist/types'
class SSHClient {
// eslint-disable-next-line no-use-before-define
private static _instance: SSHClient
private static _client: NodeSSH
private _isConnected = false
public static get instance (): SSHClient {
return this._instance || (this._instance = new this())
}
public static get client (): NodeSSH {
return this._client || (this._client = new NodeSSH())
}
private changeWinStylePathToUnix (path: string): string {
return path.replace(/\\/g, '/')
}
public async connect (config: ISftpPlistConfig): Promise<boolean> {
const { username, password, privateKey, passphrase } = config
const loginInfo: Config = privateKey
? { username, privateKeyPath: privateKey, passphrase: passphrase || undefined }
: { username, password }
try {
await SSHClient.client.connect({
host: config.host,
port: Number(config.port) || 22,
...loginInfo
})
this._isConnected = true
return true
} catch (err: any) {
throw new Error(err)
}
}
public async deleteFile (remote: string): Promise<boolean> {
if (!this._isConnected) {
throw new Error('SSH 未连接')
}
try {
remote = this.changeWinStylePathToUnix(remote)
const script = `rm -f ${remote}`
return await this.exec(script)
} catch (err: any) {
return false
}
}
private async exec (script: string): Promise<boolean> {
const execResult = await SSHClient.client.execCommand(script)
return execResult.code === 0
}
public close (): void {
SSHClient.client.dispose()
this._isConnected = false
}
}
export default SSHClient