136 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-11-06 13:48:06 +09:00
import {
Component,
OnInit,
OnDestroy,
Inject,
EventEmitter
} from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { NGXLogger } from 'ngx-logger';
import { FileEventJson } from '@ucap-webmessenger/protocol-event';
2019-11-07 13:48:25 +09:00
import { DeviceType, FileUtil, MimeUtil } from '@ucap-webmessenger/core';
2019-11-07 11:37:33 +09:00
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
2019-11-07 13:48:25 +09:00
import { take, map, finalize, tap } from 'rxjs/operators';
2019-11-07 11:37:33 +09:00
import { SnackBarService } from '@ucap-webmessenger/ui';
2019-11-07 13:48:25 +09:00
import {
FileDownloadItem,
CommonApiService
} from '@ucap-webmessenger/api-common';
2019-11-06 13:48:06 +09:00
export interface FileViewerDialogData {
fileInfo: FileEventJson;
downloadUrl: string;
userSeq: number;
deviceType: DeviceType;
token: string;
}
2019-11-06 13:48:06 +09:00
export interface FileViewerDialogResult {}
@Component({
selector: 'app-layout-common-file-viewer',
templateUrl: './file-viewer.dialog.component.html',
styleUrls: ['./file-viewer.dialog.component.scss']
})
export class FileViewerDialogComponent implements OnInit, OnDestroy {
fileInfo: FileEventJson;
downloadUrl: string;
userSeq: number;
deviceType: DeviceType;
token: string;
2019-11-07 13:48:25 +09:00
fileDownloadItem: FileDownloadItem;
fileDownloadUrl: string;
2019-11-06 13:48:06 +09:00
constructor(
public dialogRef: MatDialogRef<
FileViewerDialogData,
FileViewerDialogResult
>,
@Inject(MAT_DIALOG_DATA) public data: FileViewerDialogData,
2019-11-07 11:37:33 +09:00
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
private snackBarService: SnackBarService,
2019-11-07 13:48:25 +09:00
private commonApiService: CommonApiService,
2019-11-06 13:48:06 +09:00
private logger: NGXLogger
) {
this.fileInfo = data.fileInfo;
this.downloadUrl = data.downloadUrl;
this.userSeq = data.userSeq;
this.deviceType = data.deviceType;
this.token = data.token;
2019-11-07 13:48:25 +09:00
this.fileDownloadUrl = this.commonApiService.urlForFileTalkDownload(
{
userSeq: this.userSeq,
deviceType: this.deviceType,
token: this.token,
attachmentsSeq: this.fileInfo.attachmentSeq
},
this.downloadUrl
);
}
2019-11-06 13:48:06 +09:00
ngOnInit() {}
ngOnDestroy(): void {}
2019-11-07 13:48:25 +09:00
onDownload(): void {
this.fileDownloadItem = new FileDownloadItem();
this.commonApiService
.fileTalkDownload(
{
userSeq: this.userSeq,
deviceType: this.deviceType,
token: this.token,
attachmentsSeq: this.fileInfo.attachmentSeq,
fileDownloadItem: this.fileDownloadItem
},
this.downloadUrl
)
.pipe(
take(1),
map(async rawBlob => {
const blob = rawBlob.slice(
0,
rawBlob.size,
MimeUtil.getMimeFromExtension(this.fileInfo.fileExt)
);
FileUtil.fromBlobToBuffer(blob)
.then(buffer => {
this.nativeService
.saveFile(buffer, this.fileInfo.fileName)
.pipe(take(1))
.subscribe(result => {
if (!!result) {
this.snackBarService.open(
`파일이 경로[${result}]에 저장되었습니다.`,
'',
{
duration: 3000,
verticalPosition: 'bottom'
}
);
} else {
this.snackBarService.open('파일 저장에 실패하였습니다.');
}
});
})
.catch(reason => {
this.logger.error('download', reason);
});
}),
finalize(() => {
this.fileDownloadItem = undefined;
})
)
.subscribe();
2019-11-06 18:19:37 +09:00
}
onClosedViewer(): void {
this.dialogRef.close();
}
2019-11-06 13:48:06 +09:00
}