mirror of
https://github.com/Eugeny/tabby.git
synced 2025-04-24 16:30:55 +08:00
Basic folder upload for sftp electron.
This commit is contained in:
parent
3f0b78edd0
commit
deaa529c07
tabby-core/src/api
tabby-electron/src
tabby-ssh/src/components
tabby-terminal/src/features
tabby-web/src
@ -22,6 +22,7 @@ export interface MessageBoxResult {
|
||||
|
||||
export abstract class FileTransfer {
|
||||
abstract getName (): string
|
||||
abstract getRelativePath (): string
|
||||
abstract getMode (): number
|
||||
abstract getSize (): number
|
||||
abstract close (): void
|
||||
@ -84,6 +85,7 @@ export abstract class FileUpload extends FileTransfer {
|
||||
|
||||
export interface FileUploadOptions {
|
||||
multiple: boolean
|
||||
directory: boolean
|
||||
}
|
||||
|
||||
export type PlatformTheme = 'light'|'dark'
|
||||
@ -202,6 +204,10 @@ export class HTMLFileUpload extends FileUpload {
|
||||
return this.file.name
|
||||
}
|
||||
|
||||
getRelativePath (): string {
|
||||
return ""
|
||||
}
|
||||
|
||||
getMode (): number {
|
||||
return 0o644
|
||||
}
|
||||
|
@ -48,6 +48,20 @@ export class ElectronPlatformService extends PlatformService {
|
||||
})
|
||||
}
|
||||
|
||||
async getAllFiles(dir: string) {
|
||||
let files: string[] = []
|
||||
const items = await fs.readdir(dir, { withFileTypes: true })
|
||||
for (const item of items) {
|
||||
const fullPath = path.posix.join(dir, item.name)
|
||||
if (item.isDirectory()) {
|
||||
files = files.concat(await this.getAllFiles(fullPath))
|
||||
} else {
|
||||
files.push(fullPath)
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
readClipboard (): string {
|
||||
return this.electron.clipboard.readText()
|
||||
}
|
||||
@ -187,12 +201,15 @@ export class ElectronPlatformService extends PlatformService {
|
||||
}
|
||||
|
||||
async startUpload (options?: FileUploadOptions, paths?: string[]): Promise<FileUpload[]> {
|
||||
options ??= { multiple: false }
|
||||
options ??= { multiple: false, directory: false }
|
||||
|
||||
const properties: any[] = ['openFile', 'treatPackageAsDirectory']
|
||||
if (options.multiple) {
|
||||
properties.push('multiSelections')
|
||||
}
|
||||
if (options.directory) {
|
||||
properties.push('openDirectory')
|
||||
}
|
||||
|
||||
if (!paths) {
|
||||
const result = await this.electron.dialog.showOpenDialog(
|
||||
@ -208,12 +225,29 @@ export class ElectronPlatformService extends PlatformService {
|
||||
paths = result.filePaths
|
||||
}
|
||||
|
||||
return Promise.all(paths.map(async p => {
|
||||
const transfer = new ElectronFileUpload(p, this.electron)
|
||||
await wrapPromise(this.zone, transfer.open())
|
||||
this.fileTransferStarted.next(transfer)
|
||||
return transfer
|
||||
}))
|
||||
if(options.directory) {
|
||||
let allFiles: string[] = []
|
||||
let relativePaths: string[] = []
|
||||
for (const folderPath of paths) {
|
||||
let files = await this.getAllFiles(folderPath)
|
||||
allFiles = allFiles.concat(files)
|
||||
relativePaths = relativePaths.concat(files.map(file => path.posix.join(path.basename(folderPath),path.posix.relative(folderPath, file))))
|
||||
}
|
||||
|
||||
return Promise.all(allFiles.map(async (p, index) => {
|
||||
const transfer = new ElectronFileUpload(p, this.electron, relativePaths[index])
|
||||
await wrapPromise(this.zone, transfer.open())
|
||||
this.fileTransferStarted.next(transfer)
|
||||
return transfer
|
||||
}))
|
||||
} else {
|
||||
return Promise.all(paths.map(async p => {
|
||||
const transfer = new ElectronFileUpload(p, this.electron)
|
||||
await wrapPromise(this.zone, transfer.open())
|
||||
this.fileTransferStarted.next(transfer)
|
||||
return transfer
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
async startDownload (name: string, mode: number, size: number, filePath?: string): Promise<FileDownload|null> {
|
||||
@ -266,7 +300,7 @@ class ElectronFileUpload extends FileUpload {
|
||||
private buffer: Buffer
|
||||
private powerSaveBlocker = 0
|
||||
|
||||
constructor (private filePath: string, private electron: ElectronService) {
|
||||
constructor (private filePath: string, private electron: ElectronService, private relativePath: string="") {
|
||||
super()
|
||||
this.buffer = Buffer.alloc(256 * 1024)
|
||||
this.powerSaveBlocker = electron.powerSaveBlocker.start('prevent-app-suspension')
|
||||
@ -282,6 +316,10 @@ class ElectronFileUpload extends FileUpload {
|
||||
getName (): string {
|
||||
return path.basename(this.filePath)
|
||||
}
|
||||
|
||||
getRelativePath (): string {
|
||||
return this.relativePath
|
||||
}
|
||||
|
||||
getMode (): number {
|
||||
return this.mode
|
||||
@ -325,6 +363,10 @@ class ElectronFileDownload extends FileDownload {
|
||||
return path.basename(this.filePath)
|
||||
}
|
||||
|
||||
getRelativePath (): string {
|
||||
return ""
|
||||
}
|
||||
|
||||
getMode (): number {
|
||||
return this.mode
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ export class EditSFTPContextMenu extends SFTPContextMenuItemProvider {
|
||||
if (event === 'rename') {
|
||||
watcher.close()
|
||||
}
|
||||
const upload = await this.platform.startUpload({ multiple: false }, [tempPath])
|
||||
const upload = await this.platform.startUpload({ multiple: false, directory: false }, [tempPath])
|
||||
if (!upload.length) {
|
||||
return
|
||||
}
|
||||
|
@ -25,6 +25,10 @@
|
||||
i.fas.fa-upload.me-1
|
||||
div(translate) Upload
|
||||
|
||||
button.btn.btn-link.btn-sm.flex-shrink-0.d-flex((click)='uploadFolder()')
|
||||
i.fas.fa-upload.me-1
|
||||
div(translate) Upload Folder
|
||||
|
||||
button.btn.btn-link.text-decoration-none((click)='close()') !{require('../../../tabby-core/src/icons/times.svg')}
|
||||
|
||||
.body(dropZone, (transfer)='uploadOne($event)')
|
||||
|
@ -176,10 +176,40 @@ export class SFTPPanelComponent {
|
||||
}
|
||||
|
||||
async upload (): Promise<void> {
|
||||
const transfers = await this.platform.startUpload({ multiple: true })
|
||||
const transfers = await this.platform.startUpload({ multiple: true, directory: false })
|
||||
await Promise.all(transfers.map(t => this.uploadOne(t)))
|
||||
}
|
||||
|
||||
async uploadFolder (): Promise<void> {
|
||||
const transfers = await this.platform.startUpload({ multiple: true, directory: true })
|
||||
await Promise.all(transfers.map(t => this.uploadOneWithFolder(t)))
|
||||
}
|
||||
|
||||
async uploadOneWithFolder (transfer: FileUpload): Promise<void> {
|
||||
const savedPath = this.path
|
||||
|
||||
try {
|
||||
await this.sftp.stat(path.join(this.path, transfer.getRelativePath()))
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message.includes('No such file')) {
|
||||
let accumPath = ""
|
||||
for (const pathParts of path.posix.dirname(transfer.getRelativePath()).split(path.posix.sep)) {
|
||||
accumPath = path.posix.join(accumPath,pathParts)
|
||||
this.sftp.mkdir(path.join(this.path, accumPath)).then(() => {
|
||||
this.notifications.notice('The directory was created successfully')
|
||||
}).catch(() => {})
|
||||
}
|
||||
} else {
|
||||
console.log("THROW HERE")
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
await this.sftp.upload(path.join(this.path, transfer.getRelativePath()), transfer)
|
||||
if (this.path === savedPath) {
|
||||
await this.navigate(this.path)
|
||||
}
|
||||
}
|
||||
|
||||
async uploadOne (transfer: FileUpload): Promise<void> {
|
||||
const savedPath = this.path
|
||||
await this.sftp.upload(path.join(this.path, transfer.getName()), transfer)
|
||||
|
@ -74,7 +74,7 @@ class ZModemMiddleware extends SessionMiddleware {
|
||||
this.logger.info('new session', zsession)
|
||||
|
||||
if (zsession.type === 'send') {
|
||||
const transfers = await this.platform.startUpload({ multiple: true })
|
||||
const transfers = await this.platform.startUpload({ multiple: true, directory: false })
|
||||
let filesRemaining = transfers.length
|
||||
let sizeRemaining = transfers.reduce((a, b) => a + b.getSize(), 0)
|
||||
for (const transfer of transfers) {
|
||||
|
@ -159,6 +159,10 @@ class HTMLFileDownload extends FileDownload {
|
||||
return this.name
|
||||
}
|
||||
|
||||
getRelativePath (): string {
|
||||
return ""
|
||||
}
|
||||
|
||||
getMode (): number {
|
||||
return this.mode
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user