From b4c3c51dc874711febf43b719ca878436b31084d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 14 Aug 2020 17:28:54 +0200 Subject: [PATCH] Fix circular dependencies --- client/src/app/core/users/user.service.ts | 14 ++++----- .../account/actor-avatar-info.component.ts | 8 ++--- .../shared/shared-main/angular/bytes.pipe.ts | 26 ++-------------- .../shared/shared-main/video/video.service.ts | 3 +- client/src/root-helpers/bytes.ts | 31 +++++++++++++++++++ client/src/root-helpers/index.ts | 1 + 6 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 client/src/root-helpers/bytes.ts diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts index 01c9be5bf..2f3945169 100644 --- a/client/src/app/core/users/user.service.ts +++ b/client/src/app/core/users/user.service.ts @@ -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 } = {} 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 } = { diff --git a/client/src/app/shared/shared-main/account/actor-avatar-info.component.ts b/client/src/app/shared/shared-main/account/actor-avatar-info.component.ts index 3a86e5b21..5daa54cb5 100644 --- a/client/src/app/shared/shared-main/account/actor-avatar-info.component.ts +++ b/client/src/app/shared/shared-main/account/actor-avatar-info.component.ts @@ -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 () { diff --git a/client/src/app/shared/shared-main/angular/bytes.pipe.ts b/client/src/app/shared/shared-main/angular/bytes.pipe.ts index f4f473568..f3f57b825 100644 --- a/client/src/app/shared/shared-main/angular/bytes.pipe.ts +++ b/client/src/app/shared/shared-main/angular/bytes.pipe.ts @@ -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 -} diff --git a/client/src/app/shared/shared-main/video/video.service.ts b/client/src/app/shared/shared-main/video/video.service.ts index b01e919aa..48aff82b4 100644 --- a/client/src/app/shared/shared-main/video/video.service.ts +++ b/client/src/app/shared/shared-main/video/video.service.ts @@ -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' diff --git a/client/src/root-helpers/bytes.ts b/client/src/root-helpers/bytes.ts new file mode 100644 index 000000000..ec8b55faa --- /dev/null +++ b/client/src/root-helpers/bytes.ts @@ -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 +} diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts index 59468b31c..036a7677d 100644 --- a/client/src/root-helpers/index.ts +++ b/client/src/root-helpers/index.ts @@ -1,3 +1,4 @@ export * from './users' +export * from './bytes' export * from './peertube-web-storage' export * from './utils' -- 2.41.0