Feature/shared refactor (#1176)

* refactor: move most of the shared entities to @start8labs/shared library
This commit is contained in:
Alex Inkin
2022-02-15 18:13:05 +03:00
committed by GitHub
parent 1769f7e2e6
commit 7c26b18c73
164 changed files with 2908 additions and 2860 deletions

View File

@@ -21,7 +21,7 @@ import { LoadingPageModule } from './pages/loading/loading.module'
import { ProdKeyModalModule } from './modals/prod-key-modal/prod-key-modal.module'
import { ProductKeyPageModule } from './pages/product-key/product-key.module'
import { RecoverPageModule } from './pages/recover/recover.module'
import { WorkspaceConfig } from '@shared'
import { WorkspaceConfig } from '@start9labs/shared'
const { useMocks } = require('../../../../config.json') as WorkspaceConfig

View File

@@ -2,10 +2,10 @@ import { NgModule } from '@angular/core'
import { CommonModule } from '@angular/common'
import { IonicModule } from '@ionic/angular'
import { FormsModule } from '@angular/forms'
import { UnitConversionPipesModule } from '@start9labs/shared'
import { EmbassyPage } from './embassy.page'
import { PasswordPageModule } from '../../modals/password/password.module'
import { EmbassyPageRoutingModule } from './embassy-routing.module'
import { PipesModule } from 'src/app/pipes/pipe.module'
@NgModule({
imports: [
@@ -14,8 +14,8 @@ import { PipesModule } from 'src/app/pipes/pipe.module'
IonicModule,
EmbassyPageRoutingModule,
PasswordPageModule,
PipesModule,
UnitConversionPipesModule,
],
declarations: [EmbassyPage],
})
export class EmbassyPageModule { }
export class EmbassyPageModule {}

View File

@@ -2,11 +2,11 @@ import { NgModule } from '@angular/core'
import { CommonModule } from '@angular/common'
import { IonicModule } from '@ionic/angular'
import { FormsModule } from '@angular/forms'
import { UnitConversionPipesModule } from '@start9labs/shared'
import { DriveStatusComponent, RecoverPage } from './recover.page'
import { PasswordPageModule } from '../../modals/password/password.module'
import { ProdKeyModalModule } from '../../modals/prod-key-modal/prod-key-modal.module'
import { RecoverPageRoutingModule } from './recover-routing.module'
import { PipesModule } from 'src/app/pipes/pipe.module'
import { CifsModalModule } from 'src/app/modals/cifs-modal/cifs-modal.module'
@NgModule({
@@ -18,8 +18,8 @@ import { CifsModalModule } from 'src/app/modals/cifs-modal/cifs-modal.module'
RecoverPageRoutingModule,
PasswordPageModule,
ProdKeyModalModule,
PipesModule,
UnitConversionPipesModule,
CifsModalModule,
],
})
export class RecoverPageModule { }
export class RecoverPageModule {}

View File

@@ -1,18 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
// converts bytes to gigabytes
@Pipe({
name: 'convertBytes',
})
export class ConvertBytesPipe implements PipeTransform {
transform (bytes: number): string {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
}
}

View File

@@ -1,10 +0,0 @@
import { NgModule } from '@angular/core'
import { ConvertBytesPipe } from './convert-bytes.pipe'
@NgModule({
declarations: [ConvertBytesPipe],
imports: [],
exports: [ConvertBytesPipe],
})
export class PipesModule { }

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'
import { pauseFor } from 'src/app/util/misc.util'
import { pauseFor } from '@start9labs/shared'
import { ApiService, CifsRecoverySource, SetupEmbassyReq } from './api.service'
let tries = 0

View File

@@ -1,8 +1,12 @@
import { Injectable } from '@angular/core'
import { BehaviorSubject } from 'rxjs'
import { ApiService, CifsRecoverySource, DiskRecoverySource } from './api/api.service'
import {
ApiService,
CifsRecoverySource,
DiskRecoverySource,
} from './api/api.service'
import { ErrorToastService } from './error-toast.service'
import { pauseFor } from '../util/misc.util'
import { pauseFor } from '@start9labs/shared'
@Injectable({
providedIn: 'root',
@@ -17,7 +21,11 @@ export class StateService {
recoverySource: CifsRecoverySource | DiskRecoverySource
recoveryPassword: string
dataTransferProgress: { bytesTransferred: number, totalBytes: number, complete: boolean } | null
dataTransferProgress: {
bytesTransferred: number
totalBytes: number
complete: boolean
} | null
dataProgress = 0
dataCompletionSubject = new BehaviorSubject(false)
@@ -25,28 +33,27 @@ export class StateService {
lanAddress: string
cert: string
constructor (
constructor(
private readonly apiService: ApiService,
private readonly errorToastService: ErrorToastService,
) { }
) {}
async pollDataTransferProgress () {
async pollDataTransferProgress() {
this.polling = true
await pauseFor(500)
if (
this.dataTransferProgress?.complete
) {
if (this.dataTransferProgress?.complete) {
this.dataCompletionSubject.next(true)
return
}
let progress
try {
progress = await this.apiService.getRecoveryStatus()
} catch (e) {
this.errorToastService.present(`${e.message}: ${e.details}.\nRestart Embassy to try again.`)
this.errorToastService.present(
`${e.message}: ${e.details}.\nRestart Embassy to try again.`,
)
}
if (progress) {
this.dataTransferProgress = {
@@ -55,20 +62,25 @@ export class StateService {
complete: progress.complete,
}
if (this.dataTransferProgress.totalBytes) {
this.dataProgress = this.dataTransferProgress.bytesTransferred / this.dataTransferProgress.totalBytes
this.dataProgress =
this.dataTransferProgress.bytesTransferred /
this.dataTransferProgress.totalBytes
}
}
setTimeout(() => this.pollDataTransferProgress(), 0) // prevent call stack from growing
}
async importDrive (guid: string): Promise<void> {
async importDrive(guid: string): Promise<void> {
const ret = await this.apiService.importDrive(guid)
this.torAddress = ret['tor-address']
this.lanAddress = ret['lan-address']
this.cert = ret['root-ca']
}
async setupEmbassy (storageLogicalname: string, password: string): Promise<void> {
async setupEmbassy(
storageLogicalname: string,
password: string,
): Promise<void> {
const ret = await this.apiService.setupEmbassy({
'embassy-logicalname': storageLogicalname,
'embassy-password': password,

View File

@@ -1,3 +0,0 @@
export const pauseFor = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}