aboutsummaryrefslogtreecommitdiffhomepage
path: root/client
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-14 17:28:54 +0200
committerChocobozzz <me@florianbigard.com>2020-08-14 17:28:54 +0200
commitb4c3c51dc874711febf43b719ca878436b31084d (patch)
tree19cc50cc8e28c9de33f001595a4c6d4ced9690cf /client
parent93e903ac165eed918986e415127d6ea9730e572c (diff)
downloadPeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.gz
PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.zst
PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.zip
Fix circular dependencies
Diffstat (limited to 'client')
-rw-r--r--client/src/app/core/users/user.service.ts14
-rw-r--r--client/src/app/shared/shared-main/account/actor-avatar-info.component.ts8
-rw-r--r--client/src/app/shared/shared-main/angular/bytes.pipe.ts26
-rw-r--r--client/src/app/shared/shared-main/video/video.service.ts3
-rw-r--r--client/src/root-helpers/bytes.ts31
-rw-r--r--client/src/root-helpers/index.ts1
6 files changed, 46 insertions, 37 deletions
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
4import { HttpClient, HttpParams } from '@angular/common/http' 4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core' 5import { Injectable } from '@angular/core'
6import { AuthService } from '@app/core/auth' 6import { AuthService } from '@app/core/auth'
7import { BytesPipe } from '@app/shared/shared-main' 7import { getBytes } from '@root-helpers/bytes'
8import { UserLocalStorageKeys } from '@root-helpers/users' 8import { UserLocalStorageKeys } from '@root-helpers/users'
9import { 9import {
10 Avatar, 10 Avatar,
@@ -27,8 +27,6 @@ import { User } from './user.model'
27export class UserService { 27export class UserService {
28 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' 28 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
29 29
30 private bytesPipe = new BytesPipe()
31
32 private userCache: { [ id: number ]: Observable<UserServerModel> } = {} 30 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
33 31
34 constructor ( 32 constructor (
@@ -365,19 +363,19 @@ export class UserService {
365 if (user.videoQuota === -1) { 363 if (user.videoQuota === -1) {
366 videoQuota = '∞' 364 videoQuota = '∞'
367 } else { 365 } else {
368 videoQuota = this.bytesPipe.transform(user.videoQuota, 0) 366 videoQuota = getBytes(user.videoQuota, 0)
369 } 367 }
370 368
371 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) 369 const videoQuotaUsed = getBytes(user.videoQuotaUsed, 0)
372 370
373 let videoQuotaDaily: string 371 let videoQuotaDaily: string
374 let videoQuotaUsedDaily: string 372 let videoQuotaUsedDaily: string
375 if (user.videoQuotaDaily === -1) { 373 if (user.videoQuotaDaily === -1) {
376 videoQuotaDaily = '∞' 374 videoQuotaDaily = '∞'
377 videoQuotaUsedDaily = this.bytesPipe.transform(0, 0) + '' 375 videoQuotaUsedDaily = getBytes(0, 0) + ''
378 } else { 376 } else {
379 videoQuotaDaily = this.bytesPipe.transform(user.videoQuotaDaily, 0) + '' 377 videoQuotaDaily = getBytes(user.videoQuotaDaily, 0) + ''
380 videoQuotaUsedDaily = this.bytesPipe.transform(user.videoQuotaUsedDaily || 0, 0) + '' 378 videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
381 } 379 }
382 380
383 const roleLabels: { [ id in UserRole ]: string } = { 381 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 @@
1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { Notifier, ServerService } from '@app/core' 2import { Notifier, ServerService } from '@app/core'
3import { Account, BytesPipe, VideoChannel } from '@app/shared/shared-main' 3import { getBytes } from '@root-helpers/bytes'
4import { ServerConfig } from '@shared/models' 4import { ServerConfig } from '@shared/models'
5import { VideoChannel } from '../video-channel/video-channel.model'
6import { Account } from '../account/account.model'
5 7
6@Component({ 8@Component({
7 selector: 'my-actor-avatar-info', 9 selector: 'my-actor-avatar-info',
@@ -18,13 +20,11 @@ export class ActorAvatarInfoComponent implements OnInit {
18 maxSizeText: string 20 maxSizeText: string
19 21
20 private serverConfig: ServerConfig 22 private serverConfig: ServerConfig
21 private bytesPipe: BytesPipe
22 23
23 constructor ( 24 constructor (
24 private serverService: ServerService, 25 private serverService: ServerService,
25 private notifier: Notifier 26 private notifier: Notifier
26 ) { 27 ) {
27 this.bytesPipe = new BytesPipe()
28 this.maxSizeText = $localize`max size` 28 this.maxSizeText = $localize`max size`
29 } 29 }
30 30
@@ -52,7 +52,7 @@ export class ActorAvatarInfoComponent implements OnInit {
52 } 52 }
53 53
54 get maxAvatarSizeInBytes () { 54 get maxAvatarSizeInBytes () {
55 return this.bytesPipe.transform(this.maxAvatarSize) 55 return getBytes(this.maxAvatarSize)
56 } 56 }
57 57
58 get avatarExtensions () { 58 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 @@
1import { Pipe, PipeTransform } from '@angular/core' 1import { Pipe, PipeTransform } from '@angular/core'
2import { getBytes } from '@root-helpers/bytes'
2 3
3// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts 4// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts
4 5
5@Pipe({ name: 'bytes' }) 6@Pipe({ name: 'bytes' })
6export class BytesPipe implements PipeTransform { 7export class BytesPipe implements PipeTransform {
7 private dictionary: Array<{ max: number; type: string }> = [
8 { max: 1024, type: 'B' },
9 { max: 1048576, type: 'KB' },
10 { max: 1073741824, type: 'MB' },
11 { max: 1.0995116e12, type: 'GB' }
12 ]
13 8
14 transform (value: number, precision?: number | undefined): string | number { 9 transform (value: number, precision?: number | undefined): string | number {
15 const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] 10 return getBytes(value, precision)
16 const calc = value / (format.max / 1024)
17
18 const num = precision === undefined
19 ? calc
20 : applyPrecision(calc, precision)
21
22 return `${num} ${format.type}`
23 } 11 }
24} 12}
25
26function applyPrecision (num: number, precision: number) {
27 if (precision <= 0) {
28 return Math.round(num)
29 }
30
31 const tho = 10 ** precision
32
33 return Math.round(num * tho) / tho
34}
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 {
21 VideoUpdate 21 VideoUpdate
22} from '@shared/models' 22} from '@shared/models'
23import { environment } from '../../../../environments/environment' 23import { environment } from '../../../../environments/environment'
24import { Account, AccountService } from '../account' 24import { Account } from '../account/account.model'
25import { AccountService } from '../account/account.service'
25import { VideoChannel, VideoChannelService } from '../video-channel' 26import { VideoChannel, VideoChannelService } from '../video-channel'
26import { VideoDetails } from './video-details.model' 27import { VideoDetails } from './video-details.model'
27import { VideoEdit } from './video-edit.model' 28import { 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 @@
1const dictionary: Array<{ max: number; type: string }> = [
2 { max: 1024, type: 'B' },
3 { max: 1048576, type: 'KB' },
4 { max: 1073741824, type: 'MB' },
5 { max: 1.0995116e12, type: 'GB' }
6]
7
8function getBytes (value: number, precision?: number | undefined): string | number {
9 const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]
10 const calc = value / (format.max / 1024)
11
12 const num = precision === undefined
13 ? calc
14 : applyPrecision(calc, precision)
15
16 return `${num} ${format.type}`
17}
18
19function applyPrecision (num: number, precision: number) {
20 if (precision <= 0) {
21 return Math.round(num)
22 }
23
24 const tho = 10 ** precision
25
26 return Math.round(num * tho) / tho
27}
28
29export {
30 getBytes
31}
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 @@
1export * from './users' 1export * from './users'
2export * from './bytes'
2export * from './peertube-web-storage' 3export * from './peertube-web-storage'
3export * from './utils' 4export * from './utils'