{"version":3,"file":"c8y-ngx-components-binary-file-download.mjs","sources":["../../binary-file-download/binary-file-download.service.ts","../../binary-file-download/binary-file-download.module.ts","../../binary-file-download/c8y-ngx-components-binary-file-download.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { Router, NavigationEnd, ActivatedRoute } from '@angular/router';\nimport { filter, map } from 'rxjs/operators';\nimport { IManagedObject, InventoryBinaryService, InventoryService } from '@c8y/client';\nimport { gettext } from '@c8y/ngx-components/gettext';\nimport { AlertService, ModalService, Status } from '@c8y/ngx-components';\nimport { saveAs } from 'file-saver';\nimport { isUndefined } from 'lodash-es';\nimport { TranslateService } from '@ngx-translate/core';\n\n@Injectable()\nexport class BinaryFileDownloadService {\n  constructor(\n    private router: Router,\n    private route: ActivatedRoute,\n    private inventoryService: InventoryService,\n    private alertService: AlertService,\n    private modalService: ModalService,\n    private translate: TranslateService,\n    private inventoryBinary: InventoryBinaryService\n  ) {}\n\n  run() {\n    this.router.events\n      .pipe(\n        filter(event => {\n          return event instanceof NavigationEnd && this.route.snapshot.queryParams.download;\n        }),\n        map(() => this.route.snapshot.queryParams.download)\n      )\n      .subscribe(async downloadId => {\n        try {\n          const { data } = await this.inventoryService.detail(downloadId);\n          if (!isUndefined(data.c8y_IsBinary)) {\n            this.showDownloadModal(data);\n          } else {\n            const alertMessage = this.translate.instant(\n              gettext(\n                'Could not download the file: object with ID \"{{ id }}\" is not a valid binary.'\n              ),\n              { id: data.id }\n            );\n            this.alertService.danger(alertMessage);\n          }\n        } catch (ex) {\n          this.alertService.addServerFailure(ex);\n        }\n      });\n  }\n\n  async showDownloadModal(binaryMo: IManagedObject) {\n    try {\n      const modalBody = this.translate.instant(\n        gettext('You are about to download file \"{{ fileName }}\". Do you want to proceed?'),\n        { fileName: binaryMo.name }\n      );\n      await this.modalService.confirm(gettext('File download'), modalBody, Status.INFO, {\n        ok: gettext('Download')\n      });\n      const arrayBuffer = await this.getArrayBuffer(binaryMo.id);\n      const fileBinary = new File([arrayBuffer], binaryMo.name, { type: binaryMo.contentType });\n      saveAs(fileBinary);\n    } catch (e) {\n      // empty body :(\n      console.log({ e });\n    }\n  }\n\n  private async getArrayBuffer(binaryId): Promise<ArrayBuffer> {\n    let arrayBuffer: ArrayBuffer;\n    try {\n      const res = await this.inventoryBinary.download(binaryId);\n      arrayBuffer = await res.arrayBuffer();\n    } catch (ex) {\n      const msg = gettext('Could not get the binary.');\n      this.alertService.danger(msg);\n    }\n\n    return arrayBuffer;\n  }\n}\n","import { NgModule } from '@angular/core';\nimport { CommonModule } from '@c8y/ngx-components';\nimport { BinaryFileDownloadService } from './binary-file-download.service';\nimport { RouterModule } from '@angular/router';\n\n@NgModule({\n  imports: [CommonModule, RouterModule],\n  providers: [BinaryFileDownloadService]\n})\nexport class BinaryFileDownloadModule {\n  constructor(binaryFileDownloadService: BinaryFileDownloadService) {\n    binaryFileDownloadService.run();\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["i1.BinaryFileDownloadService"],"mappings":";;;;;;;;;;;;;MAWa,yBAAyB,CAAA;AACpC,IAAA,WAAA,CACU,MAAc,EACd,KAAqB,EACrB,gBAAkC,EAClC,YAA0B,EAC1B,YAA0B,EAC1B,SAA2B,EAC3B,eAAuC,EAAA;QANvC,IAAA,CAAA,MAAM,GAAN,MAAM;QACN,IAAA,CAAA,KAAK,GAAL,KAAK;QACL,IAAA,CAAA,gBAAgB,GAAhB,gBAAgB;QAChB,IAAA,CAAA,YAAY,GAAZ,YAAY;QACZ,IAAA,CAAA,YAAY,GAAZ,YAAY;QACZ,IAAA,CAAA,SAAS,GAAT,SAAS;QACT,IAAA,CAAA,eAAe,GAAf,eAAe;IACtB;IAEH,GAAG,GAAA;QACD,IAAI,CAAC,MAAM,CAAC;AACT,aAAA,IAAI,CACH,MAAM,CAAC,KAAK,IAAG;AACb,YAAA,OAAO,KAAK,YAAY,aAAa,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CA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