1 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { Observable } from 'rxjs/Observable'
6 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
7 import { ResultList } from '../../../../../shared/models/result-list.model'
8 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
9 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
10 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
11 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
12 import { environment } from '../../../environments/environment'
13 import { ComponentPagination } from '../rest/component-pagination.model'
14 import { RestExtractor } from '../rest/rest-extractor.service'
15 import { RestService } from '../rest/rest.service'
16 import { UserService } from '../users/user.service'
17 import { SortField } from './sort-field.type'
18 import { VideoDetails } from './video-details.model'
19 import { VideoEdit } from './video-edit.model'
20 import { Video } from './video.model'
21 import { objectToFormData } from '@app/shared/misc/utils'
24 export class VideoService {
25 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
28 private authHttp: HttpClient,
29 private restExtractor: RestExtractor,
30 private restService: RestService
33 getVideoViewUrl (uuid: string) {
34 return VideoService.BASE_VIDEO_URL + uuid + '/views'
37 getVideo (uuid: string): Observable<VideoDetails> {
38 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
39 .map(videoHash => new VideoDetails(videoHash))
40 .catch((res) => this.restExtractor.handleError(res))
43 viewVideo (uuid: string): Observable<VideoDetails> {
44 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
45 .map(this.restExtractor.extractDataBool)
46 .catch(this.restExtractor.handleError)
49 updateVideo (video: VideoEdit) {
50 const language = video.language || undefined
51 const licence = video.licence || undefined
52 const category = video.category || undefined
53 const description = video.description || undefined
55 const body: VideoUpdate = {
61 privacy: video.privacy,
64 commentsEnabled: video.commentsEnabled,
65 support: video.support,
66 thumbnailfile: video.thumbnailfile,
67 previewfile: video.previewfile
70 const data = objectToFormData(body)
72 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
73 .map(this.restExtractor.extractDataBool)
74 .catch(this.restExtractor.handleError)
77 uploadVideo (video: FormData) {
78 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
82 .catch(this.restExtractor.handleError)
85 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
86 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
88 let params = new HttpParams()
89 params = this.restService.addRestGetParams(params, pagination, sort)
91 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
92 .map(this.extractVideos)
93 .catch((res) => this.restExtractor.handleError(res))
96 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
97 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
99 let params = new HttpParams()
100 params = this.restService.addRestGetParams(params, pagination, sort)
103 .get(VideoService.BASE_VIDEO_URL, { params })
104 .map(this.extractVideos)
105 .catch((res) => this.restExtractor.handleError(res))
110 videoPagination: ComponentPagination,
112 ): Observable<{ videos: Video[], totalVideos: number}> {
113 const url = VideoService.BASE_VIDEO_URL + 'search'
115 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
117 let params = new HttpParams()
118 params = this.restService.addRestGetParams(params, pagination, sort)
119 params = params.append('search', search)
122 .get<ResultList<VideoServerModel>>(url, { params })
123 .map(this.extractVideos)
124 .catch((res) => this.restExtractor.handleError(res))
127 removeVideo (id: number) {
129 .delete(VideoService.BASE_VIDEO_URL + id)
130 .map(this.restExtractor.extractDataBool)
131 .catch((res) => this.restExtractor.handleError(res))
134 loadCompleteDescription (descriptionPath: string) {
136 .get(environment.apiUrl + descriptionPath)
137 .map(res => res['description'])
138 .catch((res) => this.restExtractor.handleError(res))
141 setVideoLike (id: number) {
142 return this.setVideoRate(id, 'like')
145 setVideoDislike (id: number) {
146 return this.setVideoRate(id, 'dislike')
149 unsetVideoLike (id: number) {
150 return this.setVideoRate(id, 'none')
153 getUserVideoRating (id: number): Observable<UserVideoRate> {
154 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
158 .catch(res => this.restExtractor.handleError(res))
161 private setVideoRate (id: number, rateType: VideoRateType) {
162 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
163 const body: UserVideoRateUpdate = {
169 .map(this.restExtractor.extractDataBool)
170 .catch(res => this.restExtractor.handleError(res))
173 private extractVideos (result: ResultList<VideoServerModel>) {
174 const videosJson = result.data
175 const totalVideos = result.total
178 for (const videoJson of videosJson) {
179 videos.push(new Video(videoJson))
182 return { videos, totalVideos }