import { Injectable } from '@angular/core' ; import { Torrent } from '../classes/torrent' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Observable , of } from 'rxjs' ; import { catchError , tap } from 'rxjs/operators' ; import { ConfigService } from './config.service' ; @ Injectable ( { providedIn : 'root' } ) export class TorrentService { addOneTorrentUrl = this . configService . baseUrl + '/torrent' + '/addOneFile' ; getAllEngineTorrentsUrl = this . configService . baseUrl + '/torrent' + '/getAllEngineTorrents' ; getAllTorrentsUrl = this . configService . baseUrl + '/torrent' + '/getAllTorrents' ; getCompletedTorrentsUrl = this . configService . baseUrl + '/torrent' + '/getCompletedTorrents' ; downloadOneTorrentUrl = this . configService . baseUrl + '/torrent' + '/startDownload' ; stopTorrentUrl = this . configService . baseUrl + '/torrent' + '/stopDownload' ; deleteTorrentUrl = this . configService . baseUrl + '/torrent' + '/delOne' ; sendMagnetUrl = this . configService . baseUrl + '/magnet' + '/addOneMagent' ; refleshTime = 1000 ; private formHttpOptions = { headers : new HttpHeaders ( { 'enctype' : 'multipart/form-data' , 'Access-Control-Allow-Origin' : this . configService . addr , } ) } ; constructor ( private httpClient , private configService ) { } addOneTorrent ( file ) { const formData = new FormData ( ) ; formData . append ( 'oneTorrentFile' , file , file . name ) ; return this . httpClient . post < boolean > ( this . addOneTorrentUrl , formData , this . formHttpOptions ) . pipe ( tap ( _ => console . log ( 'addOne torrent' ) ) , catchError ( this . handleError < boolean > ( 'add one torrent' ) ) ) ; } sendMagnet ( magnet ) { const formData = new FormData ( ) ; formData . append ( 'linkAddress' , magnet ) ; return this . httpClient . post < boolean > ( this . sendMagnetUrl , formData , this . formHttpOptions ) . pipe ( tap ( _ => console . log ( 'addOne magnet' ) ) , catchError ( this . handleError < boolean > ( 'add one magnet' ) ) ) ; } private getTorrents ( url ) { return this . httpClient . get < Torrent [ ] > ( url ) . pipe ( tap ( _ => console . log ( 'get torrents' ) ) , catchError ( this . handleError < Torrent [ ] > ( 'add one torrent' ) ) ) ; } getSelectedTorrents ( state ) { if ( state === 'running' ) { return this . getTorrents ( this . getAllEngineTorrentsUrl ) ; } else if ( state === 'total' ) { return this . getTorrents ( this . getAllTorrentsUrl ) ; } else if ( state === 'completed' ) { return this . getTorrents ( this . getCompletedTorrentsUrl ) ; } } startDownloadOneTorrent ( hexString ) { return this . operateOneTorrent ( this . downloadOneTorrentUrl , hexString ) ; } stopDownloadOneTorrent ( hexString ) { return this . operateOneTorrent ( this . stopTorrentUrl , hexString ) ; } deleteDownloadOneTorrent ( hexString ) { return this . operateOneTorrent ( this . deleteTorrentUrl , hexString ) ; } private operateOneTorrent ( operateUrl , hexString ) { const formData = new FormData ( ) ; formData . append ( 'hexString' , hexString ) ; return this . httpClient . post < JSON > ( operateUrl , formData , this . formHttpOptions ) . pipe ( tap ( _ => console . log ( operateUrl + 'operate torrent' ) ) , catchError ( this . handleError < JSON > ( 'failed to operate one torrent' ) ) ) ; } param operation param result private handleError < T > ( operation = 'operation' , result ? ) { return ( error ) : Observable < T > => { console . error ( error ) ; return of ( result as T ) ; } ; } }