]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Proxify local storage and handle if it is unavailable
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { HttpClient } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
4 import 'rxjs/add/operator/do'
5 import { ReplaySubject } from 'rxjs/ReplaySubject'
6 import { ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { environment } from '../../../environments/environment'
9
10 @Injectable()
11 export class ServerService {
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
14 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
15
16 configLoaded = new ReplaySubject<boolean>(1)
17 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
18 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
19 videoLicencesLoaded = new ReplaySubject<boolean>(1)
20 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
21
22 private config: ServerConfig = {
23 instance: {
24 name: 'PeerTube',
25 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
26 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
27 defaultClientRoute: '',
28 customizations: {
29 javascript: '',
30 css: ''
31 }
32 },
33 serverVersion: 'Unknown',
34 signup: {
35 allowed: false
36 },
37 transcoding: {
38 enabledResolutions: []
39 },
40 avatar: {
41 file: {
42 size: { max: 0 },
43 extensions: []
44 }
45 },
46 video: {
47 image: {
48 size: { max: 0 },
49 extensions: []
50 },
51 file: {
52 extensions: []
53 }
54 }
55 }
56 private videoCategories: Array<{ id: number, label: string }> = []
57 private videoLicences: Array<{ id: number, label: string }> = []
58 private videoLanguages: Array<{ id: number, label: string }> = []
59 private videoPrivacies: Array<{ id: number, label: string }> = []
60
61 constructor (private http: HttpClient) {
62 this.loadConfigLocally()
63 }
64
65 loadConfig () {
66 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
67 .do(this.saveConfigLocally)
68 .subscribe(data => {
69 this.config = data
70
71 this.configLoaded.next(true)
72 })
73 }
74
75 loadVideoCategories () {
76 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
77 }
78
79 loadVideoLicences () {
80 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
81 }
82
83 loadVideoLanguages () {
84 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
85 }
86
87 loadVideoPrivacies () {
88 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
89 }
90
91 getConfig () {
92 return this.config
93 }
94
95 getVideoCategories () {
96 return this.videoCategories
97 }
98
99 getVideoLicences () {
100 return this.videoLicences
101 }
102
103 getVideoLanguages () {
104 return this.videoLanguages
105 }
106
107 getVideoPrivacies () {
108 return this.videoPrivacies
109 }
110
111 getAbout () {
112 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
113 }
114
115 private loadVideoAttributeEnum (
116 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
117 hashToPopulate: { id: number, label: string }[],
118 notifier: ReplaySubject<boolean>,
119 sort = false
120 ) {
121 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
122 .subscribe(data => {
123 Object.keys(data)
124 .forEach(dataKey => {
125 hashToPopulate.push({
126 id: parseInt(dataKey, 10),
127 label: data[dataKey]
128 })
129 })
130
131 if (sort === true) {
132 hashToPopulate.sort((a, b) => {
133 if (a.label < b.label) return -1
134 if (a.label === b.label) return 0
135 return 1
136 })
137 }
138
139 notifier.next(true)
140 })
141 }
142
143 private saveConfigLocally (config: ServerConfig) {
144 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
145 }
146
147 private loadConfigLocally () {
148 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
149
150 if (configString) {
151 try {
152 const parsed = JSON.parse(configString)
153 Object.assign(this.config, parsed)
154 } catch (err) {
155 console.error('Cannot parse config saved in local storage.', err)
156 }
157 }
158 }
159 }