dialog.ts 3.0 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 16 17 18 19 20 21 22 23
import {
  HttpClient,
  HttpEventType,
  HttpRequest,
  HttpResponse,
} from '@angular/common/http';
import { Component, Inject, OnDestroy } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { StateService } from '@uirouter/core';
24
import * as FileSaver from 'file-saver';
S
Shu Muto 已提交
25 26
import { Subscription } from 'rxjs';
import { LogService } from '../../services/global/logs';
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

export interface LogsDownloadDialogMeta {
  pod: string;
  container: string;
}

@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 已提交
46 47 48 49 50 51
    public dialogRef: MatDialogRef<LogsDownloadDialog>,
    @Inject(MAT_DIALOG_DATA) public data: LogsDownloadDialogMeta,
    private readonly logService: LogService,
    private readonly state_: StateService,
    private readonly http_: HttpClient
  ) {
52
    const namespace = this.state_.params.resourceNamespace;
S
Shu Muto 已提交
53 54 55
    const logUrl = `api/v1/log/file/${namespace}/${data.pod}/${
      data.container
    }?previous=${this.logService.getPrevious()}`;
56

S
Shu Muto 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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;
        }
      );
80 81 82 83 84 85 86 87 88 89 90 91 92 93
  }

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

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

  save(): void {
    FileSaver.saveAs(
S
Shu Muto 已提交
94 95 96
      this.result,
      this.logService.getLogFileName(this.data.pod, this.data.container)
    );
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    this.dialogRef.close();
  }

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

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