UNPKG

@c8y/ngx-components

Version:

Angular modules for Cumulocity IoT applications

1 lines 7.12 kB
{"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 { AlertService, gettext, 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":";;;;;;;;;;;;MAUa,yBAAyB,CAAA;AACpC,IAAA,WAAA,CACU,MAAc,EACd,KAAqB,EACrB,gBAAkC,EAClC,YAA0B,EAC1B,YAA0B,EAC1B,SAA2B,EAC3B,eAAuC,EAAA;QANvC,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;QACd,IAAK,CAAA,KAAA,GAAL,KAAK,CAAgB;QACrB,IAAgB,CAAA,gBAAA,GAAhB,gBAAgB,CAAkB;QAClC,IAAY,CAAA,YAAA,GAAZ,YAAY,CAAc;QAC1B,IAAY,CAAA,YAAA,GAAZ,YAAY,CAAc;QAC1B,IAAS,CAAA,SAAA,GAAT,SAAS,CAAkB;QAC3B,IAAe,CAAA,eAAA,GAAf,eAAe,CAAwB;KAC7C;IAEJ,GAAG,GAAA;QACD,IAAI,CAAC,MAAM,CAAC,MAAM;AACf,aAAA,IAAI,CACH,MAAM,CAAC,KAAK,IAAG;AACb,YAAA,OAAO,KAAK,YAAY,aAAa,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,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