All files / app/service/api backup.service.ts

57.57% Statements 19/33
66.66% Branches 6/9
22.22% Functions 4/18
53.57% Lines 15/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97  4x     4x 4x 4x                 4x   3x       3x 3x 3x       6x                         1x               1x 1x               1x   1x                                                                          
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { delay } from 'lodash-es';
import { catchError, map, Observable, shareReplay } from 'rxjs';
import { all, BackupOptions } from '../../model/backup';
import { params } from '../../util/http';
import { CACHE_MS } from '../account.service';
import { ConfigService } from '../config.service';
import { LoginService } from '../login.service';
 
export type BackupRef = {  id: string, size?: number };
 
@Injectable({
  providedIn: 'root'
})
export class BackupService {
 
  private backupKey = '';
  private _backupKey$?: Observable<string>;
 
  constructor(
    private http: HttpClient,
    private config: ConfigService,
    private login: LoginService,
  ) { }
 
  public get base() {
    return this.config.api + '/api/v1/backup';
  }
 
  create(origin: string, options: BackupOptions = all): Observable<string> {
    return this.http.post(`${this.base}`, options, {
      params: params({ origin }),
      responseType: 'text'
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  list(origin: string): Observable<BackupRef[]> {
    return this.http.get<BackupRef[]>(`${this.base}`, {
      params: params({ origin }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  getDownloadKey(): Observable<string> {
    Eif (!this._backupKey$) {
      this._backupKey$ = this.http.post(`${this.base}/key`, null, {
        responseType: 'text',
        params: params({ key: this.backupKey }),
      }).pipe(
        map(res => this.backupKey = res as string),
        shareReplay(1),
        catchError(err => this.login.handleHttpError(err)),
      );
      delay(() => this._backupKey$ = undefined, CACHE_MS);
    }
    return this._backupKey$;
  }
 
  restore(origin: string, id: string, options: BackupOptions = all) {
    return this.http.post(`${this.base}/restore/${id}`, options, {
      params: params({ origin }),
      responseType: 'text'
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  regen(origin: string): Observable<void> {
    return this.http.post<void>(`${this.base}/regen`, null, {
      params: params({ origin }),
    }).pipe(
        catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  delete(origin: string, id: string): Observable<void> {
    return this.http.delete<void>(`${this.base}/${id}`, {
      params: params({ origin }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  upload(origin: string, file: File): Observable<string> {
    return this.http.post(`${this.base}/upload/${file.name}`, file, {
      params: params({ origin }),
      responseType: 'text'
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
}