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 thumbnailfile: video.thumbnailfile,
66 previewfile: video.previewfile
69 const data = objectToFormData(body)
71 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
72 .map(this.restExtractor.extractDataBool)
73 .catch(this.restExtractor.handleError)
76 uploadVideo (video: FormData) {
77 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
81 .catch(this.restExtractor.handleError)
84 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
85 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
87 let params = new HttpParams()
88 params = this.restService.addRestGetParams(params, pagination, sort)
90 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
91 .map(this.extractVideos)
92 .catch((res) => this.restExtractor.handleError(res))
95 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
96 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
98 let params = new HttpParams()
99 params = this.restService.addRestGetParams(params, pagination, sort)
102 .get(VideoService.BASE_VIDEO_URL, { params })
103 .map(this.extractVideos)
104 .catch((res) => this.restExtractor.handleError(res))
109 videoPagination: ComponentPagination,
111 ): Observable<{ videos: Video[], totalVideos: number}> {
112 const url = VideoService.BASE_VIDEO_URL + 'search'
114 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
116 let params = new HttpParams()
117 params = this.restService.addRestGetParams(params, pagination, sort)
118 params = params.append('search', search)
121 .get<ResultList<VideoServerModel>>(url, { params })
122 .map(this.extractVideos)
123 .catch((res) => this.restExtractor.handleError(res))
126 removeVideo (id: number) {
128 .delete(VideoService.BASE_VIDEO_URL + id)
129 .map(this.restExtractor.extractDataBool)
130 .catch((res) => this.restExtractor.handleError(res))
133 loadCompleteDescription (descriptionPath: string) {
135 .get(environment.apiUrl + descriptionPath)
136 .map(res => res['description'])
137 .catch((res) => this.restExtractor.handleError(res))
140 setVideoLike (id: number) {
141 return this.setVideoRate(id, 'like')
144 setVideoDislike (id: number) {
145 return this.setVideoRate(id, 'dislike')
148 unsetVideoLike (id: number) {
149 return this.setVideoRate(id, 'none')
152 getUserVideoRating (id: number): Observable<UserVideoRate> {
153 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
157 .catch(res => this.restExtractor.handleError(res))
160 private setVideoRate (id: number, rateType: VideoRateType) {
161 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
162 const body: UserVideoRateUpdate = {
168 .map(this.restExtractor.extractDataBool)
169 .catch(res => this.restExtractor.handleError(res))
172 private extractVideos (result: ResultList<VideoServerModel>) {
173 const videosJson = result.data
174 const totalVideos = result.total
177 for (const videoJson of videosJson) {
178 videos.push(new Video(videoJson))
181 return { videos, totalVideos }