All files / src/app/services/file-service file.service.ts

98.21% Statements 55/56
83.33% Branches 10/12
100% Functions 13/13
98.04% Lines 50/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x     1x 1x 1x     1x   1x     13x 13x 13x 13x 13x 13x     6x 6x 6x 2x   4x 4x 4x 3x 7x 1x 3x       4x 4x 3x     1x 1x   1x 1x 1x 1x 1x 1x   1x     1x 3x     3x 3x 3x 3x 2x       2x   3x   1x  
import '../../rxjs-extensions';
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { NgRedux, select } from '@angular-redux/store';
 
import { IAppState } from '../../store/state/AppState';
import { Logger } from '../../common/mw.common.module';
import { UiActions } from 'app/common/ui.actions/ui.actions';
import { FileActions } from './file.actions';
 
@Injectable()
export class FileService {
 
  @select(['file', 'fileList']) storeFileList$;
 
  constructor (
    public http: Http,
    public router: Router,
    public ngRedux: NgRedux<IAppState>,
    public logger: Logger,
    public fileActions: FileActions,
    public uiActions: UiActions
  ) {}
 
  public getFileList(baseUrl: string): void {
    this.storeFileList$.subscribe(fileList => {
      if (fileList.files || fileList.pending) {
        return;
      }
      const url = `${baseUrl}filelist.txt`;
      this.fileActions.setFileListPending(true);
      this.http.get(url)
        .map((res: Response) => res.text().replace(/\r/, '').split(/\n/))
        .map(files => files.filter(file => file))  // remove blank lines
        .catch(error => this.handleFileListError(error))
        .subscribe(files => this.fileActions.setFileListSuccess(files));
    });
  }
 
  public getFile(url: string): Observable<Response> {
    return this.http.get(url)
      .catch(error => this.handleError(error, 'getFile'));
  }
 
  private handleFileListError(error: Response | any) {
    this.fileActions.setFileListFailed(error);
 
    const caller = `${this.constructor.name}.getFileList`;
    const msg = `Caller: ${caller}, ${error}. Error status: ${error.status} }`;
    error.caller = caller;
    Eif (error.status === 404) {
      this.uiActions.setFour0FourMessage(caller, msg, error.url);
      this.router.navigate(['/404'] );
    }
    return Observable.throw(error);
  }
 
  private handleError(error: Response | any, method: string) {
    Iif (method === 'getFileList') {
      this.fileActions.setFileListFailed(error);
    }
    const caller = `${this.constructor.name}.${method}`;
    const msg = `Caller: ${caller}, ${error}. Error status: ${error.status} }`;
    error.caller = caller;
    if (error instanceof Response && error.status === 404) {
      this.ngRedux.dispatch( {
        type: UiActions.FOUR0FOUR_MESSAGE,
        four0four: { caller: caller,  url: error.url }
      });
      this.router.navigate(['/404'] );
    }
    return Observable.throw(error);
  }
}