-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
platform.ts
228 lines (184 loc) · 6.03 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { MenuItemOptions } from './menu'
import { Subject, Observable } from 'rxjs'
/* eslint-disable @typescript-eslint/no-unused-vars */
export interface ClipboardContent {
text: string
html?: string
}
export interface MessageBoxOptions {
type: 'warning'|'error'
message: string
detail?: string
buttons: string[]
defaultId?: number
cancelId?: number
}
export interface MessageBoxResult {
response: number
}
export abstract class FileTransfer {
abstract getName (): string
abstract getMode (): number
abstract getSize (): number
abstract close (): void
getSpeed (): number {
return this.lastChunkSpeed
}
getCompletedBytes (): number {
return this.completedBytes
}
isComplete (): boolean {
return this.completedBytes >= this.getSize()
}
isCancelled (): boolean {
return this.cancelled
}
cancel (): void {
this.cancelled = true
this.close()
}
protected increaseProgress (bytes: number): void {
if (!bytes) {
return
}
this.completedBytes += bytes
this.lastChunkSpeed = bytes * 1000 / (Date.now() - this.lastChunkStartTime)
this.lastChunkStartTime = Date.now()
}
private completedBytes = 0
private lastChunkStartTime = Date.now()
private lastChunkSpeed = 0
private cancelled = false
}
export abstract class FileDownload extends FileTransfer {
abstract write (buffer: Buffer): Promise<void>
}
export abstract class FileUpload extends FileTransfer {
abstract read (): Promise<Buffer>
async readAll (): Promise<Buffer> {
const buffers: Buffer[] = []
while (true) {
const buf = await this.read()
if (!buf.length) {
break
}
buffers.push(Buffer.from(buf))
}
return Buffer.concat(buffers)
}
}
export interface FileUploadOptions {
multiple: boolean
}
export type PlatformTheme = 'light'|'dark'
export abstract class PlatformService {
supportsWindowControls = false
get fileTransferStarted$ (): Observable<FileTransfer> { return this.fileTransferStarted }
get displayMetricsChanged$ (): Observable<void> { return this.displayMetricsChanged }
get themeChanged$ (): Observable<PlatformTheme> { return this.themeChanged }
protected fileTransferStarted = new Subject<FileTransfer>()
protected displayMetricsChanged = new Subject<void>()
protected themeChanged = new Subject<PlatformTheme>()
abstract readClipboard (): string
abstract setClipboard (content: ClipboardContent): void
abstract loadConfig (): Promise<string>
abstract saveConfig (content: string): Promise<void>
abstract startDownload (name: string, mode: number, size: number): Promise<FileDownload|null>
abstract startUpload (options?: FileUploadOptions): Promise<FileUpload[]>
startUploadFromDragEvent (event: DragEvent, multiple = false): FileUpload[] {
const result: FileUpload[] = []
if (!event.dataTransfer) {
return []
}
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < event.dataTransfer.files.length; i++) {
const file = event.dataTransfer.files[i]
const transfer = new HTMLFileUpload(file)
this.fileTransferStarted.next(transfer)
result.push(transfer)
if (!multiple) {
break
}
}
return result
}
getConfigPath (): string|null {
return null
}
showItemInFolder (path: string): void {
throw new Error('Not implemented')
}
async isProcessRunning (name: string): Promise<boolean> {
return false
}
async installPlugin (name: string, version: string): Promise<void> {
throw new Error('Not implemented')
}
async uninstallPlugin (name: string): Promise<void> {
throw new Error('Not implemented')
}
getWinSCPPath (): string|null {
throw new Error('Not implemented')
}
async exec (app: string, argv: string[]): Promise<void> {
throw new Error('Not implemented')
}
isShellIntegrationSupported (): boolean {
return false
}
async isShellIntegrationInstalled (): Promise<boolean> {
return false
}
async installShellIntegration (): Promise<void> {
throw new Error('Not implemented')
}
async uninstallShellIntegration (): Promise<void> {
throw new Error('Not implemented')
}
openPath (path: string): void {
throw new Error('Not implemented')
}
getTheme (): PlatformTheme {
return 'dark'
}
abstract getOSRelease (): string
abstract getAppVersion (): string
abstract openExternal (url: string): void
abstract listFonts (): Promise<string[]>
abstract setErrorHandler (handler: (_: any) => void): void
abstract popupContextMenu (menu: MenuItemOptions[], event?: MouseEvent): void
abstract showMessageBox (options: MessageBoxOptions): Promise<MessageBoxResult>
abstract pickDirectory (): Promise<string>
abstract quit (): void
}
export class HTMLFileUpload extends FileUpload {
private stream: ReadableStream
private reader: ReadableStreamDefaultReader
constructor (private file: File) {
super()
this.stream = this.file.stream()
this.reader = this.stream.getReader()
}
getName (): string {
return this.file.name
}
getMode (): number {
return 0o644
}
getSize (): number {
return this.file.size
}
async read (): Promise<Buffer> {
const result: any = await this.reader.read()
if (result.done || !result.value) {
return Buffer.from('')
}
const chunk = Buffer.from(result.value)
this.increaseProgress(chunk.length)
return chunk
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
bringToFront (): void { }
// eslint-disable-next-line @typescript-eslint/no-empty-function
close (): void { }
}