import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { Component, OnInit, OnDestroy, Inject } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { select, Store } from '@ngrx/store'; import { DeviceType, FileDownloadItem } from '@ucap/domain-common'; import { User } from '@ucap/domain-organization'; import { LoginSession, LoginInfo } from '@ucap/domain-authentication'; import { FileEventJson, FileInfo, isMedia } from '@ucap/domain-chat'; import { LogService } from '@ucap/ng-logger'; import { CommonApiService } from '@ucap/ng-api-common'; import { UserSelector } from '@ucap/ng-store-organization'; import { LoginSelector, AuthorizationSelector } from '@ucap/ng-store-authentication'; import { SelectFileInfo } from '@ucap/ng-ui/viewer'; import { AppFileService } from '@app/services/app-file.service'; import { AppAuthenticationService } from '@app/services/app-authentication.service'; import { UserPermission } from '@ucap/domain-authorization'; export interface FileViewerDialogData { fileInfos: FileInfo[]; selectFileInfo: SelectFileInfo; downloadUrl: string; userSeq: string; deviceType: DeviceType; token: string; } export interface FileViewerDialogResult {} @Component({ selector: 'app-dialog-chat-file-viewer', templateUrl: './file-viewer.dialog.component.html', styleUrls: ['./file-viewer.dialog.component.scss'] }) export class FileViewerDialogComponent implements OnInit, OnDestroy { private ngOnDestroySubject = new Subject(); isMediaType: boolean; fileInfo: { fileInfos: FileInfo[]; selectFileInfo: SelectFileInfo; }; currentFileInfo: FileInfo; loginInfo: LoginInfo; user: User; loginSession: LoginSession; userPermission: UserPermission; constructor( private store: Store, public dialogRef: MatDialogRef< FileViewerDialogData, FileViewerDialogResult >, @Inject(MAT_DIALOG_DATA) public data: FileViewerDialogData, // private snackBarService: SnackBarService, private commonApiService: CommonApiService, private appFileService: AppFileService, private appAuthenticationService: AppAuthenticationService, // private translateService: TranslateService, private logService: LogService ) { this.currentFileInfo = this.data.fileInfos.find( (f) => f.seq === this.data.selectFileInfo.attachmentSeq ); if (!this.currentFileInfo) { this.logService.warn( 'file info is exist', this.data.fileInfos, this.data.selectFileInfo ); this.dialogRef.close(); return; } this.isMediaType = isMedia(this.currentFileInfo); this.fileInfo = { fileInfos: this.data.fileInfos.filter((f) => { const i = isMedia(f); return this.isMediaType ? i : !i; }), selectFileInfo: this.data.selectFileInfo }; } ngOnInit() { this.store .pipe(takeUntil(this.ngOnDestroySubject), select(LoginSelector.loginInfo)) .subscribe((loginInfo) => (this.loginInfo = loginInfo)); this.store .pipe(takeUntil(this.ngOnDestroySubject), select(UserSelector.user)) .subscribe((user) => (this.user = user)); this.loginSession = this.appAuthenticationService.getLoginSession(); // auth.fileTransferAllowedCompanyList check. this.store .pipe( takeUntil(this.ngOnDestroySubject), select(AuthorizationSelector.userPermission) ) .subscribe((userPermission) => { if (!!userPermission) { this.userPermission = userPermission; } if ( !!userPermission && !!this.data.fileInfos && this.data.fileInfos.length > 0 ) { this.fileInfo = { fileInfos: this.data.fileInfos .filter((item) => { // filtered userPermission.fileTransferAllowedCompanyList if ( !!userPermission.fileTransferAllowedCompanyList && userPermission.fileTransferAllowedCompanyList.length > 0 ) { return ( userPermission.fileTransferAllowedCompanyList.indexOf( item.sentMessageJson.companyCode ) > -1 ); } return true; }) .sort((a, b) => a.eventSeq < b.eventSeq ? 1 : a.eventSeq > b.eventSeq ? -1 : 0 ), selectFileInfo: this.data.selectFileInfo }; } }); } ngOnDestroy(): void { if (!!this.ngOnDestroySubject) { this.ngOnDestroySubject.next(); this.ngOnDestroySubject.complete(); } } onDownload(info: { attachmentSeq?: number; fileName: string; fileDownloadItem: FileDownloadItem; roomSeq?: number; eventSeq?: number; }): void { let fileName; const startIdx = info.fileName.indexOf('.thumb'); if (startIdx < 0) { fileName = info.fileName; } else { const endIdx = info.fileName.lastIndexOf('.'); const startFileName = info.fileName.substring(0, startIdx - 4); const endFileName = info.fileName.substring(endIdx, info.fileName.length); fileName = startFileName.concat(endFileName); } this.appFileService.saveFile( { fileInfo: { // require parameters. fileName, attachmentSeq: info.attachmentSeq } as FileEventJson, fileDownloadItem: info.fileDownloadItem, type: 'save', fileName, fileDownloadUrl: undefined, savePath: undefined }, this.loginInfo, this.user, this.loginSession ); } onClosedViewer(): void { this.dialogRef.close(); } fileDownloadUrl = (attachmentSeq: number) => { const urlDownload = this.commonApiService.urlForFileTalkDownload( { userSeq: String(this.data.userSeq), deviceType: this.data.deviceType, token: this.data.token, attachmentsSeq: String(attachmentSeq) }, this.data.downloadUrl ); return urlDownload; }; }