]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.service.ts
Server: avoid request entity too large for requests between pods
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
CommitLineData
230809ef 1import { Injectable } from '@angular/core';
aff038cd 2import { Http, Response, URLSearchParams } from '@angular/http';
5555f886 3import { Observable } from 'rxjs/Observable';
dc8bc31b 4
41a2aee3 5import { Pagination } from './pagination.model';
4a6995be 6import { Search } from '../../shared';
41a2aee3 7import { SortField } from './sort-field.type';
bd5c83a8 8import { AuthHttp, AuthService } from '../../shared';
41a2aee3 9import { Video } from './video.model';
dc8bc31b
C
10
11@Injectable()
41a2aee3 12export class VideoService {
ccf6ed16 13 private static BASE_VIDEO_URL = '/api/v1/videos/';
dc8bc31b 14
4fd8aa32
C
15 constructor(
16 private authService: AuthService,
bd5c83a8 17 private authHttp: AuthHttp,
4fd8aa32
C
18 private http: Http
19 ) {}
20
21 getVideo(id: string) {
22 return this.http.get(VideoService.BASE_VIDEO_URL + id)
23 .map(res => <Video> res.json())
24 .catch(this.handleError);
25 }
dc8bc31b 26
cf20596c 27 getVideos(pagination: Pagination, sort: SortField) {
471bc22f 28 const params = this.createPaginationParams(pagination);
cf20596c 29
aff038cd 30 if (sort) params.set('sort', sort);
cf20596c 31
ccf6ed16 32 return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
501bc6c2
C
33 .map(res => res.json())
34 .map(this.extractVideos)
dc8bc31b
C
35 .catch(this.handleError);
36 }
37
dc8bc31b 38 removeVideo(id: string) {
bd5c83a8
C
39 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
40 .map(res => <number> res.status)
41 .catch(this.handleError);
dc8bc31b
C
42 }
43
cf20596c 44 searchVideos(search: Search, pagination: Pagination, sort: SortField) {
471bc22f 45 const params = this.createPaginationParams(pagination);
cf20596c 46
471bc22f 47 if (search.field) params.set('field', search.field);
aff038cd 48 if (sort) params.set('sort', sort);
cf20596c 49
ccf6ed16 50 return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
501bc6c2
C
51 .map(res => res.json())
52 .map(this.extractVideos)
98b01bac
C
53 .catch(this.handleError);
54 }
55
4fd8aa32
C
56 private createPaginationParams(pagination: Pagination) {
57 const params = new URLSearchParams();
58 const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
59 const count: number = pagination.itemsPerPage;
60
61 params.set('start', start.toString());
62 params.set('count', count.toString());
63
64 return params;
65 }
66
ccf6ed16 67 private extractVideos(body: any) {
32294074
C
68 const videos_json = body.data;
69 const totalVideos = body.total;
501bc6c2 70 const videos = [];
32294074 71 for (const video_json of videos_json) {
501bc6c2
C
72 videos.push(new Video(video_json));
73 }
74
32294074 75 return { videos, totalVideos };
501bc6c2
C
76 }
77
ccf6ed16 78 private handleError(error: Response) {
dc8bc31b
C
79 console.error(error);
80 return Observable.throw(error.json().error || 'Server error');
81 }
82}