refactor: code

This commit is contained in:
Methapon2001 2023-12-11 10:11:18 +07:00
parent 52cbf57613
commit 5a7c56a67e
No known key found for this signature in database
GPG key ID: 849924FEF46BD132

View file

@ -12,8 +12,8 @@ export interface TypeSetting {
} }
export const useFileInfoStore = defineStore('info', () => { export const useFileInfoStore = defineStore('info', () => {
const fileInfo = ref<EhrFile>()
const isFilePreview = ref<Boolean>(false) const isFilePreview = ref<Boolean>(false)
const fileInfo = ref<EhrFile>()
const fileIcon: TypeSetting = { const fileIcon: TypeSetting = {
word: { icon: 'mdi-file-word-outline', color: 'blue-11' }, word: { icon: 'mdi-file-word-outline', color: 'blue-11' },
excel: { icon: 'mdi-file-excel-outline', color: 'green-4' }, 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' }, image: { icon: 'mdi-file-image-outline', color: 'blue-11' },
} }
const mimeFileMapping: MimeMap = { const mimeFileMapping: MimeMap = {
'application/msword': { 'application/msword': fileIcon.word,
...fileIcon.word, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
}, fileIcon.word,
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': { 'application/vnd.openxmlformats-officedocument.wordprocessingml.template':
...fileIcon.word, fileIcon.word,
}, 'application/vnd.ms-word.document.macroEnabled.12': fileIcon.word,
'application/vnd.openxmlformats-officedocument.wordprocessingml.template': { 'application/vnd.ms-word.template.macroEnabled.12': fileIcon.word,
...fileIcon.word, 'application/vnd.ms-excel': fileIcon.excel,
}, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
'application/vnd.ms-word.document.macroEnabled.12': { fileIcon.excel,
...fileIcon.word, 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
}, fileIcon.excel,
'application/vnd.ms-word.template.macroEnabled.12': { 'application/vnd.ms-excel.sheet.macroEnabled.12': fileIcon.excel,
...fileIcon.word, '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-excel': { 'application/vnd.ms-powerpoint': fileIcon.powerpoint,
...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': 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
{ fileIcon.powerpoint,
...fileIcon.powerpoint, 'application/vnd.openxmlformats-officedocument.presentationml.template':
}, fileIcon.powerpoint,
'application/vnd.openxmlformats-officedocument.presentationml.template': { 'application/vnd.openxmlformats-officedocument.presentationml.slideshow':
...fileIcon.powerpoint, fileIcon.powerpoint,
}, 'application/vnd.ms-powerpoint.addin.macroEnabled.12': fileIcon.powerpoint,
'application/vnd.openxmlformats-officedocument.presentationml.slideshow': { 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
...fileIcon.powerpoint, fileIcon.powerpoint,
}, 'application/vnd.ms-powerpoint.template.macroEnabled.12':
'application/vnd.ms-powerpoint.addin.macroEnabled.12': { fileIcon.powerpoint,
...fileIcon.powerpoint, 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12':
}, fileIcon.powerpoint,
'application/vnd.ms-powerpoint.presentation.macroEnabled.12': { 'application/pdf': fileIcon.pdf,
...fileIcon.powerpoint, 'text/plain': fileIcon.txt,
}, 'image/png': fileIcon.image,
'application/vnd.ms-powerpoint.template.macroEnabled.12': { 'image/jpeg': fileIcon.image,
...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( function getType(
mimeType: string | undefined, mimeType: string | undefined,
fileName: string | undefined, fileName: string | undefined,
): string { ): string {
if (mimeType === undefined) { if (mimeType === undefined) return 'ไม่ทราบประเภท'
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 'ไม่ทราบประเภท' return 'ไม่ทราบประเภท'
} }
@ -136,15 +88,14 @@ export const useFileInfoStore = defineStore('info', () => {
} }
function getSize(size: string | undefined): string { function getSize(size: string | undefined): string {
if (size === undefined) { if (size === undefined) return 'ไม่ทราบขนาด'
return 'ไม่ทราบขนาด'
}
const units = ['B', 'KB', 'MB', 'GB', 'TB'] const units = ['B', 'KB', 'MB', 'GB', 'TB']
let i = 0 let i = 0
let sizeNumber = parseFloat(size) let sizeNumber = parseFloat(size)
while (sizeNumber >= 1024 && i < units.length - 1) { while (sizeNumber >= 1024 && i++ < units.length - 1) {
sizeNumber /= 1024 sizeNumber /= 1024
i++
} }
return sizeNumber.toFixed(2) + ' ' + units[i] return sizeNumber.toFixed(2) + ' ' + units[i]
} }