import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";

@Component({
  selector: "app-blob-viewer",
  templateUrl: "./blob-viewer.component.html",
  styleUrls: ["./blob-viewer.component.scss"],
})
export class BlobViewerComponent implements OnInit, OnDestroy {
  public TYPE_IMAGE = "image";

  @Input("blob") blob;
  @Input("type") type;

  public imgURL;

  constructor(private sanitizer: DomSanitizer) {}

  ngOnInit(): void {
    if (this.type == this.TYPE_IMAGE) {
      this.imgURL = this.sanitizer.bypassSecurityTrustUrl(
        URL.createObjectURL(this.blob)
      );
    }
  }

  ngOnDestroy() {
    if (this.type == this.TYPE_IMAGE) {
      URL.revokeObjectURL(this.imgURL);
    }
  }
}
