mirror of
https://github.com/Eugeny/tabby.git
synced 2025-07-20 10:28:05 +00:00
Basic folder upload for sftp electron.
This commit is contained in:
@@ -22,6 +22,7 @@ export interface MessageBoxResult {
|
|||||||
|
|
||||||
export abstract class FileTransfer {
|
export abstract class FileTransfer {
|
||||||
abstract getName (): string
|
abstract getName (): string
|
||||||
|
abstract getRelativePath (): string
|
||||||
abstract getMode (): number
|
abstract getMode (): number
|
||||||
abstract getSize (): number
|
abstract getSize (): number
|
||||||
abstract close (): void
|
abstract close (): void
|
||||||
@@ -84,6 +85,7 @@ export abstract class FileUpload extends FileTransfer {
|
|||||||
|
|
||||||
export interface FileUploadOptions {
|
export interface FileUploadOptions {
|
||||||
multiple: boolean
|
multiple: boolean
|
||||||
|
directory: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PlatformTheme = 'light'|'dark'
|
export type PlatformTheme = 'light'|'dark'
|
||||||
@@ -202,6 +204,10 @@ export class HTMLFileUpload extends FileUpload {
|
|||||||
return this.file.name
|
return this.file.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRelativePath (): string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
getMode (): number {
|
getMode (): number {
|
||||||
return 0o644
|
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 {
|
readClipboard (): string {
|
||||||
return this.electron.clipboard.readText()
|
return this.electron.clipboard.readText()
|
||||||
}
|
}
|
||||||
@@ -187,12 +201,15 @@ export class ElectronPlatformService extends PlatformService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async startUpload (options?: FileUploadOptions, paths?: string[]): Promise<FileUpload[]> {
|
async startUpload (options?: FileUploadOptions, paths?: string[]): Promise<FileUpload[]> {
|
||||||
options ??= { multiple: false }
|
options ??= { multiple: false, directory: false }
|
||||||
|
|
||||||
const properties: any[] = ['openFile', 'treatPackageAsDirectory']
|
const properties: any[] = ['openFile', 'treatPackageAsDirectory']
|
||||||
if (options.multiple) {
|
if (options.multiple) {
|
||||||
properties.push('multiSelections')
|
properties.push('multiSelections')
|
||||||
}
|
}
|
||||||
|
if (options.directory) {
|
||||||
|
properties.push('openDirectory')
|
||||||
|
}
|
||||||
|
|
||||||
if (!paths) {
|
if (!paths) {
|
||||||
const result = await this.electron.dialog.showOpenDialog(
|
const result = await this.electron.dialog.showOpenDialog(
|
||||||
@@ -208,6 +225,22 @@ export class ElectronPlatformService extends PlatformService {
|
|||||||
paths = result.filePaths
|
paths = result.filePaths
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 => {
|
return Promise.all(paths.map(async p => {
|
||||||
const transfer = new ElectronFileUpload(p, this.electron)
|
const transfer = new ElectronFileUpload(p, this.electron)
|
||||||
await wrapPromise(this.zone, transfer.open())
|
await wrapPromise(this.zone, transfer.open())
|
||||||
@@ -215,6 +248,7 @@ export class ElectronPlatformService extends PlatformService {
|
|||||||
return transfer
|
return transfer
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async startDownload (name: string, mode: number, size: number, filePath?: string): Promise<FileDownload|null> {
|
async startDownload (name: string, mode: number, size: number, filePath?: string): Promise<FileDownload|null> {
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
@@ -266,7 +300,7 @@ class ElectronFileUpload extends FileUpload {
|
|||||||
private buffer: Buffer
|
private buffer: Buffer
|
||||||
private powerSaveBlocker = 0
|
private powerSaveBlocker = 0
|
||||||
|
|
||||||
constructor (private filePath: string, private electron: ElectronService) {
|
constructor (private filePath: string, private electron: ElectronService, private relativePath: string="") {
|
||||||
super()
|
super()
|
||||||
this.buffer = Buffer.alloc(256 * 1024)
|
this.buffer = Buffer.alloc(256 * 1024)
|
||||||
this.powerSaveBlocker = electron.powerSaveBlocker.start('prevent-app-suspension')
|
this.powerSaveBlocker = electron.powerSaveBlocker.start('prevent-app-suspension')
|
||||||
@@ -283,6 +317,10 @@ class ElectronFileUpload extends FileUpload {
|
|||||||
return path.basename(this.filePath)
|
return path.basename(this.filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRelativePath (): string {
|
||||||
|
return this.relativePath
|
||||||
|
}
|
||||||
|
|
||||||
getMode (): number {
|
getMode (): number {
|
||||||
return this.mode
|
return this.mode
|
||||||
}
|
}
|
||||||
@@ -325,6 +363,10 @@ class ElectronFileDownload extends FileDownload {
|
|||||||
return path.basename(this.filePath)
|
return path.basename(this.filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRelativePath (): string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
getMode (): number {
|
getMode (): number {
|
||||||
return this.mode
|
return this.mode
|
||||||
}
|
}
|
||||||
|
@@ -54,7 +54,7 @@ export class EditSFTPContextMenu extends SFTPContextMenuItemProvider {
|
|||||||
if (event === 'rename') {
|
if (event === 'rename') {
|
||||||
watcher.close()
|
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) {
|
if (!upload.length) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -25,6 +25,10 @@
|
|||||||
i.fas.fa-upload.me-1
|
i.fas.fa-upload.me-1
|
||||||
div(translate) Upload
|
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')}
|
button.btn.btn-link.text-decoration-none((click)='close()') !{require('../../../tabby-core/src/icons/times.svg')}
|
||||||
|
|
||||||
.body(dropZone, (transfer)='uploadOne($event)')
|
.body(dropZone, (transfer)='uploadOne($event)')
|
||||||
|
@@ -176,10 +176,40 @@ export class SFTPPanelComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async upload (): Promise<void> {
|
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)))
|
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> {
|
async uploadOne (transfer: FileUpload): Promise<void> {
|
||||||
const savedPath = this.path
|
const savedPath = this.path
|
||||||
await this.sftp.upload(path.join(this.path, transfer.getName()), transfer)
|
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)
|
this.logger.info('new session', zsession)
|
||||||
|
|
||||||
if (zsession.type === 'send') {
|
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 filesRemaining = transfers.length
|
||||||
let sizeRemaining = transfers.reduce((a, b) => a + b.getSize(), 0)
|
let sizeRemaining = transfers.reduce((a, b) => a + b.getSize(), 0)
|
||||||
for (const transfer of transfers) {
|
for (const transfer of transfers) {
|
||||||
|
@@ -159,6 +159,10 @@ class HTMLFileDownload extends FileDownload {
|
|||||||
return this.name
|
return this.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRelativePath (): string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
getMode (): number {
|
getMode (): number {
|
||||||
return this.mode
|
return this.mode
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user