import * as angular from "angular"; import { IFileInfo, FileRestService } from 'isogd-file-rest'; import { FileService } from "../../../services/FileService"; declare var cadesplugin: any; declare var FileAPI: any; export class SignFileController { private pluginInfo: any; private certificates: any[] = []; private selectedCertificate: any = null; private certificatesState: boolean; private certificateInfoEx: any = null; private finishing: boolean = false; private fileExtension: string; private fileSize: string; private isKB: boolean; private signature: any; private state: string; private stateValue: string; static $inject = ['fileRestService', '$uibModalInstance', 'toastr', 'cryptoService', '$log', 'fileService', 'fileInfo', 'folderGuid']; constructor(private fileRestService: FileRestService, private $uibModalInstance: angular.ui.bootstrap.IModalServiceInstance, private toastr: Toastr, private cryptoService: any, private $log: ng.ILogService, private fileService: FileService, private fileInfo: IFileInfo, private folderGuid: string) { this.fileExtension = this.fileInfo.fileName.split('.').pop(); let sizeKB = this.fileInfo.contentLength / 1024.0; this.isKB = (sizeKB < 1024.0) ? true : false; this.fileSize = (this.isKB) ? sizeKB.toFixed(1) : (sizeKB / 1024.0).toFixed(2); this.pluginInfo = { version: "", cryptoVersion: "", crypto: "" }; if (cadesplugin) { this.CheckPlugIn(); } else { this.state = "danger"; this.stateValue = "Плагин не загружен"; } } ok() { this.finishing = true; if (!this.fileInfo.signed) { this.fileRestService.getFileHash(this.fileInfo.fileId).then(data => { this.cryptoService.signHash(this.selectedCertificate, data.hash, "http://cryptopro.ru/tsp/").then((response) => { this.signature = response; let formData = new FormData(); formData.append("folderGuid", this.folderGuid); formData.append("signature", new Blob([this.signature.value], { type: 'application/octet-stream' }), this.fileInfo.fileName + ".sig"); this.fileRestService.handleFileUploadWithSign(this.fileInfo.fileId, formData).then(response => { this.$uibModalInstance.close({ result: { guid: this.fileInfo.fileId, time: this.signature.time }, error: null }); }).catch(error => { this.toastr.error("Ошибка при сохранении файла с подписью!"); this.$uibModalInstance.close({ result: null, error: "Ошибка при сохранении файла с подписью!" }); }); }).catch((error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); }, (error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); } else { this.fileRestService.getFileHash(this.fileInfo.fileId).then(data => { let hash: string = data.hash; this.fileRestService.getSign(this.fileInfo.fileId).then(data => { let sign: Blob = data; this.fileService.getContentFromBlobAsBinaryString(sign).then((result) => { this.cryptoService.coSignHash(this.selectedCertificate, hash, result, "http://cryptopro.ru/tsp/").then((response) => { this.signature = response; let formData = new FormData(); formData.append("folderGuid", this.folderGuid); let _sig = new Blob([this.signature.value], { type: 'application/octet-stream' }); formData.append("signature", _sig, this.fileInfo.fileName + ".sig"); this.fileRestService.handleFileUploadWithSign(this.fileInfo.fileId, formData).then(response => { this.$uibModalInstance.close({ result: { guid: this.fileInfo.fileId, time: this.signature.time }, error: null }); }).catch(error => { this.toastr.error("Ошибка при сохранении файла с подписью!"); this.$uibModalInstance.close({ result: null, error: "Ошибка при сохранении файла с подписью!" }); }); }).catch((error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); }).catch((error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); }, (error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); }, (error) => { this.$log.error(error); this.$uibModalInstance.close({ result: null, error: error }); }); } }; cancel() { this.$uibModalInstance.dismiss('cancel'); }; CheckPlugIn() { this.state = "success"; this.stateValue = "Плагин загружен."; this.cryptoService.getInfoAboutPlugin().then((pluginInfo: any) => { this.pluginInfo = pluginInfo; this.cryptoService.getCertificates().then((certificates: any[]) => { this.certificates = certificates; if (this.certificates.length > 0) { this.selectedCertificate = this.certificates[0]; this.onChangeCertificate(); } this.certificatesState = true; }, (error) => { this.$log.error(error); this.certificatesState = false; }); }, (err) => { this.$log.error("Ошибка при загрузке плагина CryptoPro: " + err); this.state = "danger"; this.stateValue = "Ошибка при работе с плагином!"; this.certificatesState = false; }); } onChangeCertificate() { this.cryptoService.fillCertInfo(this.selectedCertificate).then((certificateInfoEx: any) => { this.certificateInfoEx = certificateInfoEx; }, (err) => { this.$log.error("Ошибка при загрузке сертификата: " + err); }); } }