🚧 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

@@ -35,7 +35,8 @@ app.config.globalProperties.$builtInPicBed = [
'aliyun',
'github',
'webdavplist',
'local'
'local',
'sftpplist'
]
app.config.unwrapInjectedRef = true

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

View File

@@ -8,6 +8,7 @@ import UpyunApi from './upyun'
import AwsS3Api from './awss3'
import WebdavApi from './webdav'
import LocalApi from './local'
import SftpPlistApi from './sftpplist'
const apiMap: IStringKeyMap = {
smms: SmmsApi,
@@ -19,7 +20,8 @@ const apiMap: IStringKeyMap = {
upyun: UpyunApi,
'aws-s3': AwsS3Api,
webdavplist: WebdavApi,
local: LocalApi
local: LocalApi,
sftpplist: SftpPlistApi
}
export default class ALLApi {

View File

@@ -0,0 +1,18 @@
import { ipcRenderer } from 'electron'
import { getRawData } from '~/renderer/utils/common'
export default class SftpPlistApi {
static async delete (configMap: IStringKeyMap): Promise<boolean> {
const { fileName, config } = configMap
try {
const deleteResult = await ipcRenderer.invoke('delete-sftp-file',
getRawData(config),
fileName
)
return deleteResult
} catch (error) {
console.error(error)
return false
}
}
}

View File

@@ -664,7 +664,7 @@ function remove (item: ImgInfo) {
type: 'warning'
}).then(async () => {
const file = await $$db.getById(item.id!)
const picBedsCanbeDeleted = ['smms', 'github', 'imgur', 'tcyun', 'aliyun', 'qiniu', 'upyun', 'aws-s3', 'webdavplist', 'local']
const picBedsCanbeDeleted = ['smms', 'github', 'imgur', 'tcyun', 'aliyun', 'qiniu', 'upyun', 'aws-s3', 'webdavplist', 'local', 'sftpplist']
if (await getConfig('settings.deleteCloudFile')) {
if (item.type !== undefined && picBedsCanbeDeleted.includes(item.type)) {
const result = await ALLApi.delete(item)
@@ -774,7 +774,7 @@ function multiRemove () {
const files: IResult<ImgInfo>[] = []
const imageIDList = Object.keys(choosedList)
const isDeleteCloudFile = await getConfig('settings.deleteCloudFile')
const picBedsCanbeDeleted = ['smms', 'github', 'imgur', 'tcyun', 'aliyun', 'qiniu', 'upyun', 'aws-s3', 'webdavplist', 'local']
const picBedsCanbeDeleted = ['smms', 'github', 'imgur', 'tcyun', 'aliyun', 'qiniu', 'upyun', 'aws-s3', 'webdavplist', 'local', 'sftpplist']
if (isDeleteCloudFile) {
for (let i = 0; i < imageIDList.length; i++) {
const key = imageIDList[i]

View File

@@ -1448,6 +1448,7 @@
</div>
</template>
<script lang="ts" setup>
// @ts-ignore
import { ElForm, ElMessage as $message, ElMessage, ElMessageBox, FormRules } from 'element-plus'
import { Reading, Close, Edit, InfoFilled } from '@element-plus/icons-vue'
import pkg from 'root/package.json'