sftp file transfers

This commit is contained in:
Eugene
2024-07-20 14:28:59 +02:00
parent ec8ccb5d43
commit c2922c960b
7 changed files with 95 additions and 120 deletions

View File

@@ -63,22 +63,24 @@ export abstract class FileTransfer {
}
export abstract class FileDownload extends FileTransfer {
abstract write (buffer: Buffer): Promise<void>
abstract write (buffer: Uint8Array): Promise<void>
}
export abstract class FileUpload extends FileTransfer {
abstract read (): Promise<Buffer>
abstract read (): Promise<Uint8Array>
async readAll (): Promise<Buffer> {
const buffers: Buffer[] = []
async readAll (): Promise<Uint8Array> {
const result = new Uint8Array(this.getSize())
let pos = 0
while (true) {
const buf = await this.read()
if (!buf.length) {
break
}
buffers.push(Buffer.from(buf))
result.set(buf, pos)
pos += buf.length
}
return Buffer.concat(buffers)
return result
}
}
@@ -210,12 +212,12 @@ export class HTMLFileUpload extends FileUpload {
return this.file.size
}
async read (): Promise<Buffer> {
async read (): Promise<Uint8Array> {
const result: any = await this.reader.read()
if (result.done || !result.value) {
return Buffer.from('')
return new Uint8Array(0)
}
const chunk = Buffer.from(result.value)
const chunk = new Uint8Array(result.value)
this.increaseProgress(chunk.length)
return chunk
}

View File

@@ -306,7 +306,7 @@ export class VaultFileProvider extends FileProvider {
id,
description: `${description} (${transfer.getName()})`,
},
value: (await transfer.readAll()).toString('base64'),
value: Buffer.from(await transfer.readAll()).toString('base64'),
})
return `${this.prefix}${id}`
}