]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/angular/videos/videos.service.ts
Use progress bar from bootstrap for the upload
[github/Chocobozzz/PeerTube.git] / client / angular / videos / videos.service.ts
CommitLineData
230809ef 1import { Injectable } from '@angular/core';
32294074 2import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http';
1553e15d 3import { Observable } from 'rxjs/Rx';
dc8bc31b 4
32294074 5import { Pagination } from './pagination';
501bc6c2
C
6import { Video } from './video';
7import { AuthService } from '../users/services/auth.service';
471bc22f 8import { Search } from '../app/search';
dc8bc31b
C
9
10@Injectable()
11export class VideosService {
12 private _baseVideoUrl = '/api/v1/videos/';
13
1553e15d 14 constructor (private http: Http, private _authService: AuthService) {}
dc8bc31b 15
32294074 16 getVideos(pagination: Pagination) {
471bc22f
C
17 const params = this.createPaginationParams(pagination);
18 return this.http.get(this._baseVideoUrl, { search: params })
501bc6c2
C
19 .map(res => res.json())
20 .map(this.extractVideos)
dc8bc31b
C
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) {
501bc6c2
C
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);
dc8bc31b
C
35 }
36
471bc22f
C
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 })
501bc6c2
C
41 .map(res => res.json())
42 .map(this.extractVideos)
98b01bac
C
43 .catch(this.handleError);
44 }
45
32294074
C
46 private extractVideos (body: any) {
47 const videos_json = body.data;
48 const totalVideos = body.total;
501bc6c2 49 const videos = [];
32294074 50 for (const video_json of videos_json) {
501bc6c2
C
51 videos.push(new Video(video_json));
52 }
53
32294074 54 return { videos, totalVideos };
501bc6c2
C
55 }
56
dc8bc31b
C
57 private handleError (error: Response) {
58 console.error(error);
59 return Observable.throw(error.json().error || 'Server error');
60 }
32294074
C
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 }
dc8bc31b 72}