Basic folder upload for sftp electron.

This commit is contained in:
marko1616
2024-08-21 13:50:25 +08:00
parent 3f0b78edd0
commit deaa529c07
7 changed files with 97 additions and 11 deletions

View File

@@ -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
}

View File

@@ -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
}