import * as angular from "angular"; import { IFileInfo, FileRestService } from 'isogd-file-rest'; class FileController { fileInfo: IFileInfo; onOpen: (args: any) => void; notOpenable: boolean; fileExtension: string; fileSize: string; fileName: string; myBlockUI: any; availableExtensions: string[] = ['csv', 'doc', 'docx', 'dwg', 'jpg', 'jpeg', 'pdf', 'png', 'rar', 'sig', 'tiff', 'tif', 'txt', 'xml', 'zip']; folderId: string; editor: boolean; isOld: boolean; deletable: boolean; static $inject = ['fileRestService', 'toastr', '$uibModal', 'blockUI', '$state']; constructor(private fileRestService: FileRestService, private toastr: Toastr, private $uibModal: angular.ui.bootstrap.IModalService, private blockUI: any, private $state: ng.ui.IStateService) { } $onInit() { this.myBlockUI = this.blockUI.instances.get('folderLoad'); this.fileExtension = this.fileInfo.fileName.split('.').pop().toLowerCase(); this.fileName = this.removeExtension(this.fileInfo.fileName); let correctExtension = false; this.availableExtensions.forEach(ex => { if (ex === this.fileExtension) { correctExtension = true; } }); if (!correctExtension) { this.fileExtension = 'unknown'; } let sizeKB = this.fileInfo.contentLength / 1024.0; let isKB = (sizeKB < 1024.0) ? true : false; this.fileSize = (isKB) ? sizeKB.toFixed(1).toString() + ' Кб' : (sizeKB / 1024.0).toFixed(2).toString() + ' Мб'; } $onDestroy() { this.myBlockUI.reset(); } removeExtension(filename: string): string { var lastDotPosition = filename.lastIndexOf('.'); if (lastDotPosition === -1) { return filename; } else { return filename.substr(0, lastDotPosition); } } openFile(id: string) { this.openFileInNewWindow(id); } open(id: string) { this.openFileInNewWindow(id); } openFileInNewWindow(id: string) { let url; if (!this.isOld) { url = `isogd-api/v2/files/view/${id}`; } else { url = `isogd-oldsystem-rest/v1/files/${id}/view`; } window.open(url, '_blank'); } delete(id: string) { let modalInstance = this.$uibModal.open({ template: require('../../common/confirmation.html'), controller: 'ConfirmController as ctrl', size: "md", backdrop: 'static', keyboard: false, windowClass: "delete-file-modal", resolve: { title: () => { return "Удаление файла" }, message: () => { return "Удалить файл '" + this.fileInfo.fileName + "' из FileNet?" }, btnOk: () => { return "Удалить" } } }); modalInstance.result.then((result: any) => { this.myBlockUI.start(); this.fileRestService.deleteFile(this.fileInfo.fileId).then(response => { this.toastr.success("Файл '" + this.fileInfo.fileName + "' успешно удален"); this.$state.reload(); this.myBlockUI.stop(); }).catch(error => { this.toastr.error("Ошибка при удалении файла!"); this.myBlockUI.stop(); }); }).catch(error => { }); } sign() { let modalInstance = this.$uibModal.open({ template: require('./signFile.html'), controller: 'SignFileController as ctrl', backdrop: 'static', keyboard: false, size: "md", resolve: { fileInfo: this.fileInfo, folderGuid: () => { return this.folderId; } } }); modalInstance.result.then((result: any) => { if (result.error === null) { this.toastr.success("Время подписания: " + result.result.time + " мс.", "Файл " + this.fileInfo.fileName + " подписан."); this.$state.reload(); } else { this.toastr.error("Ошибка при подписании файла: " + result.error.message); this.$state.reload(); } }); } verify() { let modalInstance = this.$uibModal.open({ template: require('./verify.html'), controller: 'VerifyController as ctrl', size: "md", backdrop: 'static', keyboard: false, resolve: { fileInfo: this.fileInfo } }); modalInstance.result.then((result: any) => { }).catch(error => { }); } uploadSign(files: File[]) { if (files && files.length) { if (files.length > 1) { this.toastr.error('Допустимое число файлов: ' + 1); return; } else { let fileExtension = files[0].name.split('.').pop(); if (fileExtension !== "sig") { this.toastr.error('Допустимые расширения файлов: .sig'); return; } else { var fd = new FormData(); var _file = files[0]; fd.append('signature', _file); this.fileRestService.handleFileUploadWithSign(this.fileInfo.fileId, fd).then(resp => { this.toastr.success('Подпись успешно добавлена к файлу!'); this.$state.reload(); this.fileInfo.signed = true; }).catch(resp => { this.toastr.error('Ошибка при добавлении подписи к файлу!'); this.fileInfo.signed = false; }); } } } } } export const file: angular.IComponentOptions = { controller: FileController, template: require('./file.html'), bindings: { fileInfo: '<', onOpen: '&', folderId: "<", editor: '<', isOld: '<', notOpenable: '