ucap-doc/documents/업무/6월/2째주/backup/file-viewer.dialog.component.ts
Park Byung Eun 7dcec1aaad sync
2020-06-09 09:12:32 +09:00

179 lines
4.8 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 { 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 { FileInfo, isMedia } from '@ucap-webmessenger/protocol-file';
import { SelectFileInfo, SnackBarService } from '@ucap-webmessenger/ui';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import { TranslateService } from '@ngx-translate/core';
export interface FileViewerDialogData {
fileInfos: FileInfo[];
selectFileInfo: SelectFileInfo;
downloadUrl: string;
userSeq: number;
deviceType: DeviceType;
token: string;
}
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 {
isMediaType: boolean;
fileInfo: {
fileInfos: FileInfo[];
selectFileInfo: SelectFileInfo;
};
currentFileInfo: FileInfo;
constructor(
public dialogRef: MatDialogRef<
FileViewerDialogData,
FileViewerDialogResult
>,
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
@Inject(MAT_DIALOG_DATA) public data: FileViewerDialogData,
private snackBarService: SnackBarService,
private commonApiService: CommonApiService,
private appFileService: AppFileService,
private translateService: TranslateService,
private logger: NGXLogger
) {
this.currentFileInfo = this.data.fileInfos.find(
(f) => f.seq === this.data.selectFileInfo.attachmentSeq
);
if (!this.currentFileInfo) {
this.logger.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() {}
ngOnDestroy(): void {}
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.nativeService
.selectSaveFilePath(fileName)
.then((result) => {
if (!result) {
return;
}
if (result.canceled) {
} else {
this.saveFile(info, result.filePath);
}
})
.catch((reason) => {
this.snackBarService.open(
this.translateService.instant('common.file.errors.failToSpecifyPath'),
this.translateService.instant('common.file.errors.label')
);
});
}
saveFile(
info: {
attachmentSeq?: number;
downloadUrl?: string;
fileName: string;
fileDownloadItem: FileDownloadItem;
roomSeq?: number;
eventSeq?: number;
},
savePath: string
) {
if (!!info.roomSeq && !!info.eventSeq) {
this.appFileService.fileTalkDownloadMulti({
req: {
userSeq: this.data.userSeq,
deviceType: this.data.deviceType,
token: this.data.token,
roomSeq: info.roomSeq,
eventSeq: info.eventSeq,
thumbUrl: info.fileName,
fileDownloadItem: info.fileDownloadItem
},
fileName: info.fileName,
savePath
});
} else {
this.appFileService.fileTalkDownlod({
req: {
userSeq: this.data.userSeq,
deviceType: this.data.deviceType,
token: this.data.token,
attachmentsSeq: info.attachmentSeq,
fileDownloadItem: info.fileDownloadItem
},
fileName: info.fileName,
fileDownloadUrl: this.data.downloadUrl,
savePath
});
}
}
onClosedViewer(): void {
this.dialogRef.close();
}
fileDownloadUrl = (attachmentSeq: number) => {
return this.commonApiService.urlForFileTalkDownload(
{
userSeq: this.data.userSeq,
deviceType: this.data.deviceType,
token: this.data.token,
attachmentsSeq: attachmentSeq
},
this.data.downloadUrl
);
};
}