All files / app/component/backup backup.component.ts

56.92% Statements 37/65
46.87% Branches 15/32
25% Functions 5/20
55.76% Lines 29/52

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158  3x                     3x 3x 3x               3x         1x   1x   1x           1x 1x       1x     1x 1x 1x 1x 1x 1x   1x   1x                       4x               4x 4x 4x 4x 4x 4x       2x                                           1x                                                                           1x                            
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, ElementRef, HostBinding, Input, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core';
import { ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { catchError, Observable, of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { BackupOptions } from '../../model/backup';
import { AdminService } from '../../service/admin.service';
import { BackupService } from '../../service/api/backup.service';
import { Store } from '../../store/store';
import { readableBytes } from '../../util/format';
import { printError } from '../../util/http';
import { ConfirmActionComponent } from '../action/confirm-action/confirm-action.component';
 
@Component({
  selector: 'app-backup',
  templateUrl: './backup.component.html',
  styleUrls: ['./backup.component.scss'],
  imports: [RouterLink, ConfirmActionComponent, ReactiveFormsModule]
})
export class BackupComponent {
 
  @Input()
  id!: string;
  @Input()
  size? = 0;
  @Input()
  origin = '';
 
  @ViewChild('restoreButton', { read: ElementRef })
  restoreButton?: ElementRef<HTMLElement>;
  @ViewChild('restoreOptions')
  restoreOptionsTemplate!: TemplateRef<any>;
 
  @HostBinding('class.deleted')
  deleted = false;
  serverError: string[] = [];
  restoreOptionsForm: UntypedFormGroup;
  restoreOptionsRef?: OverlayRef;
 
  private backupKey = '';
 
  constructor(
    public admin: AdminService,
    public backups: BackupService,
    public store: Store,
    private fb: UntypedFormBuilder,
    private overlay: Overlay,
    private viewContainerRef: ViewContainerRef,
  ) {
    backups.getDownloadKey()
      .subscribe(key => this.backupKey = key);
    this.restoreOptionsForm = fb.group({
      cache: [false],
      ref: [true],
      ext: [true],
      user: [true],
      plugin: [false],
      template: [false],
      newerThan: [''],
    });
  }
 
  get inProgress() {
    return this.id.startsWith('_');
  }
 
  get fileSize() {
    return readableBytes(this.size || 0);
  }
 
  get downloadLink() {
    var link = this.backups.base + '/' + this.id;
    Eif (link.startsWith('//')) link = location.protocol + link;
    Iif (link.startsWith("_")) link = link.substring(1);
    Eif (!link.endsWith(".zip")) link = link + '.zip';
    Iif (this.origin) link += '?origin=' + encodeURIComponent(this.origin)
    return link;
  }
 
  get downloadLinkAuth() {
    return this.downloadLink + (this.origin ? '&' : '?') + 'p=' + encodeURIComponent(this.backupKey);
  }
 
  showRestoreOptions() {
    if (this.restoreOptionsRef || !this.restoreButton) return;
    const positionStrategy = this.overlay.position()
      .flexibleConnectedTo(this.restoreButton)
      .withPositions([{
        originX: 'start',
        originY: 'bottom',
        overlayX: 'start',
        overlayY: 'top',
        offsetY: 4,
      }]);
    this.restoreOptionsRef = this.overlay.create({
      hasBackdrop: false,
      positionStrategy,
      scrollStrategy: this.overlay.scrollStrategies.reposition()
    });
    this.restoreOptionsRef.attach(new TemplatePortal(this.restoreOptionsTemplate, this.viewContainerRef));
  }
 
  restore$ = () => {
    return of(null).pipe(
      tap(() => this.showRestoreOptions()),
    );
  }
 
  confirmRestore() {
    const options: BackupOptions = {
      cache: this.restoreOptionsForm.value.cache,
      ref: this.restoreOptionsForm.value.ref,
      ext: this.restoreOptionsForm.value.ext,
      user: this.restoreOptionsForm.value.user,
      plugin: this.restoreOptionsForm.value.plugin,
      template: this.restoreOptionsForm.value.template,
      newerThan: this.restoreOptionsForm.value.newerThan || undefined,
    };
    this.closeRestoreOptions();
    this.backups.restore(this.origin, this.id, options).pipe(
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
      tap(() => {
        this.serverError = [];
      }),
    ).subscribe();
  }
 
  cancelRestore() {
    this.closeRestoreOptions();
  }
 
  closeRestoreOptions() {
    this.restoreOptionsRef?.detach();
    this.restoreOptionsRef?.dispose();
    this.restoreOptionsRef = undefined;
  }
 
  delete$ = () => {
    return this.backups.delete(this.origin, this.id).pipe(
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
      tap(() => {
        this.serverError = [];
        this.deleted = true;
      }),
    );
  }
 
}