Fix circular dependencies

This commit is contained in:
Chocobozzz 2020-08-14 17:28:54 +02:00
parent 93e903ac16
commit b4c3c51dc8
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 46 additions and 37 deletions

View File

@ -4,7 +4,7 @@ import { catchError, concatMap, filter, first, map, shareReplay, throttleTime, t
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { AuthService } from '@app/core/auth'
import { BytesPipe } from '@app/shared/shared-main'
import { getBytes } from '@root-helpers/bytes'
import { UserLocalStorageKeys } from '@root-helpers/users'
import {
Avatar,
@ -27,8 +27,6 @@ import { User } from './user.model'
export class UserService {
static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
private bytesPipe = new BytesPipe()
private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
constructor (
@ -365,19 +363,19 @@ export class UserService {
if (user.videoQuota === -1) {
videoQuota = '∞'
} else {
videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
videoQuota = getBytes(user.videoQuota, 0)
}
const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
const videoQuotaUsed = getBytes(user.videoQuotaUsed, 0)
let videoQuotaDaily: string
let videoQuotaUsedDaily: string
if (user.videoQuotaDaily === -1) {
videoQuotaDaily = '∞'
videoQuotaUsedDaily = this.bytesPipe.transform(0, 0) + ''
videoQuotaUsedDaily = getBytes(0, 0) + ''
} else {
videoQuotaDaily = this.bytesPipe.transform(user.videoQuotaDaily, 0) + ''
videoQuotaUsedDaily = this.bytesPipe.transform(user.videoQuotaUsedDaily || 0, 0) + ''
videoQuotaDaily = getBytes(user.videoQuotaDaily, 0) + ''
videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
}
const roleLabels: { [ id in UserRole ]: string } = {

View File

@ -1,7 +1,9 @@
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
import { Notifier, ServerService } from '@app/core'
import { Account, BytesPipe, VideoChannel } from '@app/shared/shared-main'
import { getBytes } from '@root-helpers/bytes'
import { ServerConfig } from '@shared/models'
import { VideoChannel } from '../video-channel/video-channel.model'
import { Account } from '../account/account.model'
@Component({
selector: 'my-actor-avatar-info',
@ -18,13 +20,11 @@ export class ActorAvatarInfoComponent implements OnInit {
maxSizeText: string
private serverConfig: ServerConfig
private bytesPipe: BytesPipe
constructor (
private serverService: ServerService,
private notifier: Notifier
) {
this.bytesPipe = new BytesPipe()
this.maxSizeText = $localize`max size`
}
@ -52,7 +52,7 @@ export class ActorAvatarInfoComponent implements OnInit {
}
get maxAvatarSizeInBytes () {
return this.bytesPipe.transform(this.maxAvatarSize)
return getBytes(this.maxAvatarSize)
}
get avatarExtensions () {

View File

@ -1,34 +1,12 @@
import { Pipe, PipeTransform } from '@angular/core'
import { getBytes } from '@root-helpers/bytes'
// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts
@Pipe({ name: 'bytes' })
export class BytesPipe implements PipeTransform {
private dictionary: Array<{ max: number; type: string }> = [
{ max: 1024, type: 'B' },
{ max: 1048576, type: 'KB' },
{ max: 1073741824, type: 'MB' },
{ max: 1.0995116e12, type: 'GB' }
]
transform (value: number, precision?: number | undefined): string | number {
const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
const calc = value / (format.max / 1024)
const num = precision === undefined
? calc
: applyPrecision(calc, precision)
return `${num} ${format.type}`
return getBytes(value, precision)
}
}
function applyPrecision (num: number, precision: number) {
if (precision <= 0) {
return Math.round(num)
}
const tho = 10 ** precision
return Math.round(num * tho) / tho
}

View File

@ -21,7 +21,8 @@ import {
VideoUpdate
} from '@shared/models'
import { environment } from '../../../../environments/environment'
import { Account, AccountService } from '../account'
import { Account } from '../account/account.model'
import { AccountService } from '../account/account.service'
import { VideoChannel, VideoChannelService } from '../video-channel'
import { VideoDetails } from './video-details.model'
import { VideoEdit } from './video-edit.model'

View File

@ -0,0 +1,31 @@
const dictionary: Array<{ max: number; type: string }> = [
{ max: 1024, type: 'B' },
{ max: 1048576, type: 'KB' },
{ max: 1073741824, type: 'MB' },
{ max: 1.0995116e12, type: 'GB' }
]
function getBytes (value: number, precision?: number | undefined): string | number {
const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]
const calc = value / (format.max / 1024)
const num = precision === undefined
? calc
: applyPrecision(calc, precision)
return `${num} ${format.type}`
}
function applyPrecision (num: number, precision: number) {
if (precision <= 0) {
return Math.round(num)
}
const tho = 10 ** precision
return Math.round(num * tho) / tho
}
export {
getBytes
}

View File

@ -1,3 +1,4 @@
export * from './users'
export * from './bytes'
export * from './peertube-web-storage'
export * from './utils'