diff --git a/Services/client/src/stores/file-info-data.ts b/Services/client/src/stores/file-info-data.ts index f030f9f..d7b9450 100644 --- a/Services/client/src/stores/file-info-data.ts +++ b/Services/client/src/stores/file-info-data.ts @@ -12,8 +12,8 @@ export interface TypeSetting { } export const useFileInfoStore = defineStore('info', () => { - const fileInfo = ref() const isFilePreview = ref(false) + const fileInfo = ref() const fileIcon: TypeSetting = { word: { icon: 'mdi-file-word-outline', color: 'blue-11' }, excel: { icon: 'mdi-file-excel-outline', color: 'green-4' }, @@ -23,103 +23,55 @@ export const useFileInfoStore = defineStore('info', () => { image: { icon: 'mdi-file-image-outline', color: 'blue-11' }, } const mimeFileMapping: MimeMap = { - 'application/msword': { - ...fileIcon.word, - }, - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': { - ...fileIcon.word, - }, - 'application/vnd.openxmlformats-officedocument.wordprocessingml.template': { - ...fileIcon.word, - }, - 'application/vnd.ms-word.document.macroEnabled.12': { - ...fileIcon.word, - }, - 'application/vnd.ms-word.template.macroEnabled.12': { - ...fileIcon.word, - }, - - 'application/vnd.ms-excel': { - ...fileIcon.excel, - }, - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { - ...fileIcon.excel, - }, - 'application/vnd.openxmlformats-officedocument.spreadsheetml.template': { - ...fileIcon.excel, - }, - 'application/vnd.ms-excel.sheet.macroEnabled.12': { - ...fileIcon.excel, - }, - 'application/vnd.ms-excel.template.macroEnabled.12': { - ...fileIcon.excel, - }, - 'application/vnd.ms-excel.addin.macroEnabled.12': { - ...fileIcon.excel, - }, - 'application/vnd.ms-excel.sheet.binary.macroEnabled.12': { - ...fileIcon.excel, - }, - - 'application/vnd.ms-powerpoint': { - ...fileIcon.powerpoint, - }, + 'application/msword': fileIcon.word, + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + fileIcon.word, + 'application/vnd.openxmlformats-officedocument.wordprocessingml.template': + fileIcon.word, + 'application/vnd.ms-word.document.macroEnabled.12': fileIcon.word, + 'application/vnd.ms-word.template.macroEnabled.12': fileIcon.word, + 'application/vnd.ms-excel': fileIcon.excel, + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': + fileIcon.excel, + 'application/vnd.openxmlformats-officedocument.spreadsheetml.template': + fileIcon.excel, + 'application/vnd.ms-excel.sheet.macroEnabled.12': fileIcon.excel, + 'application/vnd.ms-excel.template.macroEnabled.12': fileIcon.excel, + 'application/vnd.ms-excel.addin.macroEnabled.12': fileIcon.excel, + 'application/vnd.ms-excel.sheet.binary.macroEnabled.12': fileIcon.excel, + 'application/vnd.ms-powerpoint': fileIcon.powerpoint, 'application/vnd.openxmlformats-officedocument.presentationml.presentation': - { - ...fileIcon.powerpoint, - }, - 'application/vnd.openxmlformats-officedocument.presentationml.template': { - ...fileIcon.powerpoint, - }, - 'application/vnd.openxmlformats-officedocument.presentationml.slideshow': { - ...fileIcon.powerpoint, - }, - 'application/vnd.ms-powerpoint.addin.macroEnabled.12': { - ...fileIcon.powerpoint, - }, - 'application/vnd.ms-powerpoint.presentation.macroEnabled.12': { - ...fileIcon.powerpoint, - }, - 'application/vnd.ms-powerpoint.template.macroEnabled.12': { - ...fileIcon.powerpoint, - }, - 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12': { - ...fileIcon.powerpoint, - }, - - 'application/pdf': { - ...fileIcon.pdf, - }, - - 'text/plain': { - ...fileIcon.txt, - }, - - 'image/png': { - ...fileIcon.image, - }, - 'image/jpeg': { - ...fileIcon.image, - }, + fileIcon.powerpoint, + 'application/vnd.openxmlformats-officedocument.presentationml.template': + fileIcon.powerpoint, + 'application/vnd.openxmlformats-officedocument.presentationml.slideshow': + fileIcon.powerpoint, + 'application/vnd.ms-powerpoint.addin.macroEnabled.12': fileIcon.powerpoint, + 'application/vnd.ms-powerpoint.presentation.macroEnabled.12': + fileIcon.powerpoint, + 'application/vnd.ms-powerpoint.template.macroEnabled.12': + fileIcon.powerpoint, + 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12': + fileIcon.powerpoint, + 'application/pdf': fileIcon.pdf, + 'text/plain': fileIcon.txt, + 'image/png': fileIcon.image, + 'image/jpeg': fileIcon.image, } function getType( mimeType: string | undefined, fileName: string | undefined, ): string { - if (mimeType === undefined) { - return 'ไม่ทราบประเภท' + if (mimeType === undefined) return 'ไม่ทราบประเภท' + + const extension = mime.getExtension(mimeType) + + if (extension) return '.' + extension + if (fileName && fileName.includes('.')) { + return fileName.substring(fileName.lastIndexOf('.')) } - const extFomMime = mime.getExtension(mimeType) - if (extFomMime) { - return '.' + extFomMime - } - if (fileName && fileName.includes('.')) { - const dotIndex = fileName.lastIndexOf('.') - const extension = fileName.substring(dotIndex) - return extension - } return 'ไม่ทราบประเภท' } @@ -136,15 +88,14 @@ export const useFileInfoStore = defineStore('info', () => { } function getSize(size: string | undefined): string { - if (size === undefined) { - return 'ไม่ทราบขนาด' - } + if (size === undefined) return 'ไม่ทราบขนาด' + const units = ['B', 'KB', 'MB', 'GB', 'TB'] + let i = 0 let sizeNumber = parseFloat(size) - while (sizeNumber >= 1024 && i < units.length - 1) { + while (sizeNumber >= 1024 && i++ < units.length - 1) { sizeNumber /= 1024 - i++ } return sizeNumber.toFixed(2) + ' ' + units[i] }