]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/videos.service.ts
Use progress bar from bootstrap for the upload
[github/Chocobozzz/PeerTube.git] / client / angular / videos / videos.service.ts
1 import { Injectable } from '@angular/core';
2 import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http';
3 import { Observable } from 'rxjs/Rx';
4
5 import { Pagination } from './pagination';
6 import { Video } from './video';
7 import { AuthService } from '../users/services/auth.service';
8 import { Search } from '../app/search';
9
10 @Injectable()
11 export class VideosService {
12 private _baseVideoUrl = '/api/v1/videos/';
13
14 constructor (private http: Http, private _authService: AuthService) {}
15
16 getVideos(pagination: Pagination) {
17 const params = this.createPaginationParams(pagination);
18 return this.http.get(this._baseVideoUrl, { search: params })
19 .map(res => res.json())
20 .map(this.extractVideos)
21 .catch(this.handleError);
22 }
23
24 getVideo(id: string) {
25 return this.http.get(this._baseVideoUrl + id)
26 .map(res => <Video> res.json())
27 .catch(this.handleError);
28 }
29
30 removeVideo(id: string) {
31 const options = this._authService.getAuthRequestOptions();
32 return this.http.delete(this._baseVideoUrl + id, options)
33 .map(res => <number> res.status)
34 .catch(this.handleError);
35 }
36
37 searchVideos(search: Search, pagination: Pagination) {
38 const params = this.createPaginationParams(pagination);
39 if (search.field) params.set('field', search.field);
40 return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search.value), { search: params })
41 .map(res => res.json())
42 .map(this.extractVideos)
43 .catch(this.handleError);
44 }
45
46 private extractVideos (body: any) {
47 const videos_json = body.data;
48 const totalVideos = body.total;
49 const videos = [];
50 for (const video_json of videos_json) {
51 videos.push(new Video(video_json));
52 }
53
54 return { videos, totalVideos };
55 }
56
57 private handleError (error: Response) {
58 console.error(error);
59 return Observable.throw(error.json().error || 'Server error');
60 }
61
62 private createPaginationParams(pagination: Pagination) {
63 const params = new URLSearchParams();
64 const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
65 const count: number = pagination.itemsPerPage;
66
67 params.set('start', start.toString());
68 params.set('count', count.toString());
69
70 return params;
71 }
72 }