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 { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
11 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
12 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
13 import { environment } from '../../../environments/environment'
14 import { ComponentPagination } from '../rest/component-pagination.model'
15 import { RestExtractor } from '../rest/rest-extractor.service'
16 import { RestService } from '../rest/rest.service'
17 import { UserService } from '../users/user.service'
18 import { SortField } from './sort-field.type'
19 import { VideoDetails } from './video-details.model'
20 import { VideoEdit } from './video-edit.model'
21 import { Video } from './video.model'
22 import { objectToFormData } from '@app/shared/misc/utils'
25 export class VideoService {
26 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
29 private authHttp: HttpClient,
30 private restExtractor: RestExtractor,
31 private restService: RestService
34 getVideoViewUrl (uuid: string) {
35 return VideoService.BASE_VIDEO_URL + uuid + '/views'
38 getVideo (uuid: string): Observable<VideoDetails> {
39 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
40 .map(videoHash => new VideoDetails(videoHash))
41 .catch((res) => this.restExtractor.handleError(res))
44 viewVideo (uuid: string): Observable<VideoDetails> {
45 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
46 .map(this.restExtractor.extractDataBool)
47 .catch(this.restExtractor.handleError)
50 updateVideo (video: VideoEdit) {
51 const language = video.language || undefined
52 const licence = video.licence || undefined
53 const category = video.category || undefined
54 const description = video.description || undefined
55 const support = video.support || undefined
57 const body: VideoUpdate = {
64 privacy: video.privacy,
67 commentsEnabled: video.commentsEnabled,
68 thumbnailfile: video.thumbnailfile,
69 previewfile: video.previewfile
72 const data = objectToFormData(body)
74 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
75 .map(this.restExtractor.extractDataBool)
76 .catch(this.restExtractor.handleError)
79 uploadVideo (video: FormData) {
80 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
84 .catch(this.restExtractor.handleError)
87 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
88 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
90 let params = new HttpParams()
91 params = this.restService.addRestGetParams(params, pagination, sort)
93 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
94 .map(this.extractVideos)
95 .catch((res) => this.restExtractor.handleError(res))
99 videoPagination: ComponentPagination,
102 ): Observable<{ videos: Video[], totalVideos: number}> {
103 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
105 let params = new HttpParams()
106 params = this.restService.addRestGetParams(params, pagination, sort)
109 params = params.set('filter', filter)
113 .get(VideoService.BASE_VIDEO_URL, { params })
114 .map(this.extractVideos)
115 .catch((res) => this.restExtractor.handleError(res))
120 videoPagination: ComponentPagination,
122 ): Observable<{ videos: Video[], totalVideos: number}> {
123 const url = VideoService.BASE_VIDEO_URL + 'search'
125 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
127 let params = new HttpParams()
128 params = this.restService.addRestGetParams(params, pagination, sort)
129 params = params.append('search', search)
132 .get<ResultList<VideoServerModel>>(url, { params })
133 .map(this.extractVideos)
134 .catch((res) => this.restExtractor.handleError(res))
137 removeVideo (id: number) {
139 .delete(VideoService.BASE_VIDEO_URL + id)
140 .map(this.restExtractor.extractDataBool)
141 .catch((res) => this.restExtractor.handleError(res))
144 loadCompleteDescription (descriptionPath: string) {
146 .get(environment.apiUrl + descriptionPath)
147 .map(res => res['description'])
148 .catch((res) => this.restExtractor.handleError(res))
151 setVideoLike (id: number) {
152 return this.setVideoRate(id, 'like')
155 setVideoDislike (id: number) {
156 return this.setVideoRate(id, 'dislike')
159 unsetVideoLike (id: number) {
160 return this.setVideoRate(id, 'none')
163 getUserVideoRating (id: number): Observable<UserVideoRate> {
164 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
168 .catch(res => this.restExtractor.handleError(res))
171 private setVideoRate (id: number, rateType: VideoRateType) {
172 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
173 const body: UserVideoRateUpdate = {
179 .map(this.restExtractor.extractDataBool)
180 .catch(res => this.restExtractor.handleError(res))
183 private extractVideos (result: ResultList<VideoServerModel>) {
184 const videosJson = result.data
185 const totalVideos = result.total
188 for (const videoJson of videosJson) {
189 videos.push(new Video(videoJson))
192 return { videos, totalVideos }