145 lines
4.2 KiB
TypeScript

import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { NGXLogger } from 'ngx-logger';
import { FileEventJson } from '@ucap-webmessenger/protocol-event';
import { DeviceType } from '@ucap-webmessenger/core';
import { FileDownloadItem } from '@ucap-webmessenger/api';
import { CommonApiService } from '@ucap-webmessenger/api-common';
import { AppFileService } from '@app/services/file.service';
import { ImageOnlyDataInfo, SnackBarService } from '@ucap-webmessenger/ui';
import { UCAP_NATIVE_SERVICE, NativeService } from '@ucap-webmessenger/native';
import { TranslateService } from '@ngx-translate/core';
export interface FileViewerDialogData {
fileInfo: FileEventJson;
downloadUrl: string;
userSeq: number;
deviceType: DeviceType;
token: string;
imageOnly?: boolean;
imageOnlyData?: ImageOnlyDataInfo;
}
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;
fileDownloadUrl: string;
imageOnly = false;
imageOnlyData: ImageOnlyDataInfo;
constructor(
public dialogRef: MatDialogRef<
FileViewerDialogData,
FileViewerDialogResult
>,
@Inject(MAT_DIALOG_DATA) public data: FileViewerDialogData,
private commonApiService: CommonApiService,
private appFileService: AppFileService,
private translateService: TranslateService,
private snackBarService: SnackBarService,
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
private logger: NGXLogger
) {
this.fileInfo = data.fileInfo;
this.downloadUrl = data.downloadUrl;
this.userSeq = data.userSeq;
this.deviceType = data.deviceType;
this.token = data.token;
}
ngOnInit() {
if (!!this.data.imageOnly) {
this.imageOnly = this.data.imageOnly;
this.imageOnlyData = this.data.imageOnlyData;
}
if (!!this.imageOnly) {
this.fileDownloadUrl = this.imageOnlyData.imageUrl;
} else {
this.fileDownloadUrl = this.commonApiService.urlForFileTalkDownload(
{
userSeq: this.userSeq,
deviceType: this.deviceType,
token: this.token,
attachmentsSeq: this.fileInfo.attachmentSeq
},
this.downloadUrl
);
}
}
ngOnDestroy(): void {}
onDownload(fileDownloadItem: FileDownloadItem): void {
this.appFileService.fileTalkDownlod({
req: {
userSeq: this.userSeq,
deviceType: this.deviceType,
token: this.token,
attachmentsSeq: this.fileInfo.attachmentSeq,
fileDownloadItem
},
fileName: this.fileInfo.fileName,
fileDownloadUrl: this.downloadUrl
});
}
onSaveAs(fileDownloadItem: FileDownloadItem): void {
this.nativeService
.selectSaveFilePath(this.fileInfo.fileName)
.then(result => {
if (!result) {
return;
}
if (result.canceled) {
// this.snackBarService.open(
// this.translateService.instant('common.file.results.canceled'),
// this.translateService.instant('common.file.errors.label'),
// {
// duration: 1000
// }
// );
} else {
this.saveFile(fileDownloadItem, result.filePath);
}
})
.catch(reason => {
this.snackBarService.open(
this.translateService.instant('common.file.errors.failToSpecifyPath'),
this.translateService.instant('common.file.errors.label')
);
});
}
saveFile(fileDownloadItem: FileDownloadItem, savePath?: string) {
this.appFileService.fileTalkDownlod({
req: {
userSeq: this.userSeq,
deviceType: this.deviceType,
token: this.token,
attachmentsSeq: this.fileInfo.attachmentSeq,
fileDownloadItem
},
fileName: this.fileInfo.fileName,
savePath
});
}
onClosedViewer(): void {
this.dialogRef.close();
}
}