mirror of
https://github.com/Eugeny/tabby.git
synced 2025-10-05 06:24:56 +00:00
finished transfers menu
This commit is contained in:
@@ -4,8 +4,8 @@ import * as fsSync from 'fs'
|
||||
import * as os from 'os'
|
||||
import promiseIpc from 'electron-promise-ipc'
|
||||
import { execFile } from 'mz/child_process'
|
||||
import { Injectable } from '@angular/core'
|
||||
import { PlatformService, ClipboardContent, HostAppService, Platform, ElectronService, MenuItemOptions, MessageBoxOptions, MessageBoxResult, FileUpload } from 'terminus-core'
|
||||
import { Injectable, NgZone } from '@angular/core'
|
||||
import { PlatformService, ClipboardContent, HostAppService, Platform, ElectronService, MenuItemOptions, MessageBoxOptions, MessageBoxResult, FileUpload, FileDownload, FileUploadOptions } from 'terminus-core'
|
||||
const fontManager = require('fontmanager-redux') // eslint-disable-line
|
||||
|
||||
/* eslint-disable block-scoped-var */
|
||||
@@ -26,6 +26,7 @@ export class ElectronPlatformService extends PlatformService {
|
||||
constructor (
|
||||
private hostApp: HostAppService,
|
||||
private electron: ElectronService,
|
||||
private zone: NgZone,
|
||||
) {
|
||||
super()
|
||||
this.configPath = path.join(electron.app.getPath('userData'), 'config.yaml')
|
||||
@@ -159,24 +160,58 @@ export class ElectronPlatformService extends PlatformService {
|
||||
this.electron.app.exit(0)
|
||||
}
|
||||
|
||||
async startUpload (): Promise<FileUpload[]> {
|
||||
async startUpload (options?: FileUploadOptions): Promise<FileUpload[]> {
|
||||
options ??= { multiple: false }
|
||||
|
||||
const properties: any[] = ['openFile', 'treatPackageAsDirectory']
|
||||
if (options.multiple) {
|
||||
properties.push('multiSelections')
|
||||
}
|
||||
|
||||
const result = await this.electron.dialog.showOpenDialog(
|
||||
this.hostApp.getWindow(),
|
||||
{
|
||||
buttonLabel: 'Select',
|
||||
properties: ['multiSelections', 'openFile', 'treatPackageAsDirectory'],
|
||||
properties,
|
||||
},
|
||||
)
|
||||
if (result.canceled) {
|
||||
return []
|
||||
}
|
||||
|
||||
return Promise.all(result.filePaths.map(async path => {
|
||||
const t = new ElectronFileUpload(path)
|
||||
await t.open()
|
||||
return t
|
||||
return Promise.all(result.filePaths.map(async p => {
|
||||
const transfer = new ElectronFileUpload(p)
|
||||
await this.wrapPromise(transfer.open())
|
||||
this.fileTransferStarted.next(transfer)
|
||||
return transfer
|
||||
}))
|
||||
}
|
||||
|
||||
async startDownload (name: string, size: number): Promise<FileDownload|null> {
|
||||
const result = await this.electron.dialog.showSaveDialog(
|
||||
this.hostApp.getWindow(),
|
||||
{
|
||||
defaultPath: name,
|
||||
},
|
||||
)
|
||||
if (!result.filePath) {
|
||||
return null
|
||||
}
|
||||
const transfer = new ElectronFileDownload(result.filePath, size)
|
||||
await this.wrapPromise(transfer.open())
|
||||
this.fileTransferStarted.next(transfer)
|
||||
return transfer
|
||||
}
|
||||
|
||||
private wrapPromise <T> (promise: Promise<T>): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
promise.then(result => {
|
||||
this.zone.run(() => resolve(result))
|
||||
}).catch(error => {
|
||||
this.zone.run(() => reject(error))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
class ElectronFileUpload extends FileUpload {
|
||||
@@ -205,7 +240,6 @@ class ElectronFileUpload extends FileUpload {
|
||||
async read (): Promise<Buffer> {
|
||||
const result = await this.file.read(this.buffer, 0, this.buffer.length, null)
|
||||
this.increaseProgress(result.bytesRead)
|
||||
console.log(result)
|
||||
return this.buffer.slice(0, result.bytesRead)
|
||||
}
|
||||
|
||||
@@ -215,18 +249,17 @@ class ElectronFileUpload extends FileUpload {
|
||||
}
|
||||
|
||||
class ElectronFileDownload extends FileDownload {
|
||||
private size: number
|
||||
private file: fs.FileHandle
|
||||
private buffer: Buffer
|
||||
|
||||
constructor (private filePath: string) {
|
||||
constructor (
|
||||
private filePath: string,
|
||||
private size: number,
|
||||
) {
|
||||
super()
|
||||
this.buffer = Buffer.alloc(256 * 1024)
|
||||
}
|
||||
|
||||
async open (): Promise<void> {
|
||||
this.size = (await fs.stat(this.filePath)).size
|
||||
this.file = await fs.open(this.filePath, 'r')
|
||||
this.file = await fs.open(this.filePath, 'w')
|
||||
}
|
||||
|
||||
getName (): string {
|
||||
@@ -237,11 +270,13 @@ class ElectronFileDownload extends FileDownload {
|
||||
return this.size
|
||||
}
|
||||
|
||||
async read (): Promise<Buffer> {
|
||||
const result = await this.file.read(this.buffer, 0, this.buffer.length, null)
|
||||
this.increaseProgress(result.bytesRead)
|
||||
console.log(result)
|
||||
return this.buffer.slice(0, result.bytesRead)
|
||||
async write (buffer: Buffer): Promise<void> {
|
||||
let pos = 0
|
||||
while (pos < buffer.length) {
|
||||
const result = await this.file.write(buffer, pos, buffer.length - pos, null)
|
||||
this.increaseProgress(result.bytesWritten)
|
||||
pos += result.bytesWritten
|
||||
}
|
||||
}
|
||||
|
||||
close (): void {
|
||||
|
Reference in New Issue
Block a user