-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
fileProviders.service.ts
54 lines (50 loc) · 1.75 KB
/
fileProviders.service.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
import { Inject, Injectable } from '@angular/core'
import { TranslateService } from '@ngx-translate/core'
import { FileProvider, NotificationsService, SelectorService } from '../api'
@Injectable({ providedIn: 'root' })
export class FileProvidersService {
/** @hidden */
private constructor (
private selector: SelectorService,
private notifications: NotificationsService,
private translate: TranslateService,
@Inject(FileProvider) private fileProviders: FileProvider[],
) { }
async selectAndStoreFile (description: string): Promise<string> {
return this.selectProvider().then(p => {
return p.selectAndStoreFile(description)
})
}
async retrieveFile (key: string): Promise<Buffer> {
for (const p of this.fileProviders) {
try {
return await p.retrieveFile(key)
} catch {
continue
}
}
throw new Error('Not found')
}
async selectProvider (): Promise<FileProvider> {
const providers: FileProvider[] = []
await Promise.all(this.fileProviders.map(async p => {
if (await p.isAvailable()) {
providers.push(p)
}
}))
if (!providers.length) {
this.notifications.error(this.translate.instant('Vault master passphrase needs to be set to allow storing secrets'))
throw new Error('No available file providers')
}
if (providers.length === 1) {
return providers[0]
}
return this.selector.show(
this.translate.instant('Select file storage'),
providers.map(p => ({
name: p.name,
result: p,
})),
)
}
}