dialog.ts 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

S
Shu Muto 已提交
15
import {HttpClient, HttpEventType, HttpRequest, HttpResponse} from '@angular/common/http';
16 17
import {Component, Inject, OnDestroy} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
18
import * as FileSaver from 'file-saver';
19
import {Subscription} from 'rxjs';
20

21
import {LogService} from '../../services/global/logs';
22 23 24 25

export interface LogsDownloadDialogMeta {
  pod: string;
  container: string;
S
Sebastian Florek 已提交
26
  namespace: string;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
}

@Component({
  selector: 'kd-logs-download-dialog',
  templateUrl: 'template.html',
  styleUrls: ['style.scss'],
})
export class LogsDownloadDialog implements OnDestroy {
  loaded = 0;
  finished = false;
  result: Blob;
  downloadSubscription: Subscription;
  error: number;

  constructor(
S
Shu Muto 已提交
42 43 44 45 46 47 48 49
    public dialogRef: MatDialogRef<LogsDownloadDialog>,
    @Inject(MAT_DIALOG_DATA) public data: LogsDownloadDialogMeta,
    private readonly logService: LogService,
    private readonly http_: HttpClient,
  ) {
    const logUrl = `api/v1/log/file/${data.namespace}/${data.pod}/${
      data.container
    }?previous=${this.logService.getPrevious()}`;
50

S
Shu Muto 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    this.downloadSubscription = this.http_
      .request(new HttpRequest('GET', logUrl, {}, {reportProgress: true, responseType: 'blob'}))
      .subscribe(
        event => {
          if (event.type === HttpEventType.DownloadProgress) {
            this.loaded = event.loaded;
          } else if (event instanceof HttpResponse) {
            this.finished = true;
            // @ts-ignore
            this.result = new Blob([event.body], {type: 'text/plan'});
          }
        },
        error => {
          this.error = error.status;
        },
      );
67 68 69 70 71 72 73 74 75 76 77 78 79 80
  }

  ngOnDestroy(): void {
    if (this.downloadSubscription) {
      this.downloadSubscription.unsubscribe();
    }
  }

  hasForbiddenError(): boolean {
    return this.error !== undefined && this.error === 403;
  }

  save(): void {
    FileSaver.saveAs(
S
Shu Muto 已提交
81 82 83
      this.result,
      this.logService.getLogFileName(this.data.pod, this.data.container),
    );
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    this.dialogRef.close();
  }

  abort(): void {
    if (this.downloadSubscription) {
      this.downloadSubscription.unsubscribe();
    }
    this.dialogRef.close();
  }

  getDownloadMode(): string {
    return this.finished ? 'determinate' : 'indeterminate';
  }
}