refactor: support icon for another file and unknow

This commit is contained in:
puri-ph4tt 2023-12-07 16:22:49 +07:00
parent 17522c62be
commit 87fb1e753a
7 changed files with 104 additions and 88 deletions

View file

@ -1,5 +1,6 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
import mime from 'mime'
import type { EhrFile } from '@/stores/tree-data'
@ -13,7 +14,7 @@ export interface TypeSetting {
export const useFileInfoStore = defineStore('info', () => {
const fileInfo = ref<EhrFile>()
const isFilePreview = ref<Boolean>(false)
const file: TypeSetting = {
const fileIcon: TypeSetting = {
word: { icon: 'mdi-file-word-outline', color: 'blue-11' },
excel: { icon: 'mdi-file-excel-outline', color: 'green-4' },
powerpoint: { icon: 'mdi-file-powerpoint-outline', color: 'orange-4' },
@ -23,119 +24,108 @@ export const useFileInfoStore = defineStore('info', () => {
}
const mimeFileMapping: MimeMap = {
'application/msword': {
...file.word,
type: '.doc',
...fileIcon.word,
},
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': {
...file.word,
type: '.docx',
...fileIcon.word,
},
'application/vnd.openxmlformats-officedocument.wordprocessingml.template': {
...file.word,
type: '.dotx',
...fileIcon.word,
},
'application/vnd.ms-word.document.macroEnabled.12': {
...file.word,
type: '.docm',
...fileIcon.word,
},
'application/vnd.ms-word.template.macroEnabled.12': {
...file.word,
type: '.dotm',
...fileIcon.word,
},
'application/vnd.ms-excel': {
...file.excel,
type: '.xls',
...fileIcon.excel,
},
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
...file.excel,
type: '.xlsx',
...fileIcon.excel,
},
'application/vnd.openxmlformats-officedocument.spreadsheetml.template': {
...file.excel,
type: '.xltx',
...fileIcon.excel,
},
'application/vnd.ms-excel.sheet.macroEnabled.12': {
...file.excel,
type: '.xlsm',
...fileIcon.excel,
},
'application/vnd.ms-excel.template.macroEnabled.12': {
...file.excel,
type: '.xltm',
...fileIcon.excel,
},
'application/vnd.ms-excel.addin.macroEnabled.12': {
...file.excel,
type: '.xlam',
...fileIcon.excel,
},
'application/vnd.ms-excel.sheet.binary.macroEnabled.12': {
...file.excel,
type: '.xlsb',
...fileIcon.excel,
},
'application/vnd.ms-powerpoint': {
...file.powerpoint,
type: '.ppt',
...fileIcon.powerpoint,
},
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
{
...file.powerpoint,
type: '.pptx',
...fileIcon.powerpoint,
},
'application/vnd.openxmlformats-officedocument.presentationml.template': {
...file.powerpoint,
type: '.potx',
...fileIcon.powerpoint,
},
'application/vnd.openxmlformats-officedocument.presentationml.slideshow': {
...file.powerpoint,
type: '.ppsx',
...fileIcon.powerpoint,
},
'application/vnd.ms-powerpoint.addin.macroEnabled.12': {
...file.powerpoint,
type: '.ppam',
...fileIcon.powerpoint,
},
'application/vnd.ms-powerpoint.presentation.macroEnabled.12': {
...file.powerpoint,
type: '.pptm',
...fileIcon.powerpoint,
},
'application/vnd.ms-powerpoint.template.macroEnabled.12': {
...file.powerpoint,
type: '.potm',
...fileIcon.powerpoint,
},
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12': {
...file.powerpoint,
type: '.ppsm',
...fileIcon.powerpoint,
},
'application/pdf': {
...file.pdf,
type: '.pdf',
...fileIcon.pdf,
},
'text/plain': {
...file.txt,
type: '.txt',
...fileIcon.txt,
},
'image/png': {
...file.image,
type: '.png',
...fileIcon.image,
},
'image/jpeg': {
...file.image,
type: '.jpg, jpeg',
...fileIcon.image,
},
}
function getType(mimeType: string | undefined): string {
if (!!mimeType && mimeFileMapping.hasOwnProperty(mimeType)) {
return mimeFileMapping[mimeType].type
function getType(
mimeType: string | undefined,
fileName: string | undefined,
): string {
if (mimeType === undefined) {
return 'ไม่ทราบประเภท'
}
return 'unknown type'
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 'ไม่ทราบประเภท'
}
function getFormatDate(dateTime: string | undefined): string {
if (dateTime === undefined) {
return 'unknown date'
return 'ไม่ทราบวันที่'
}
const date = new Date(dateTime)
return date.toLocaleDateString('th-TH', {
@ -145,19 +135,9 @@ export const useFileInfoStore = defineStore('info', () => {
})
}
function getFileNameFormat(fileName: string | undefined): string {
if (fileName === undefined) {
return 'unknow name'
}
const dotIndex = fileName.lastIndexOf('.')
const fileNameOnly = fileName.substring(0, dotIndex)
return fileNameOnly
}
function getSize(size: string | undefined): string {
if (size === undefined) {
return 'unknow size'
return 'ไม่ทราบขนาด'
}
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let i = 0
@ -169,7 +149,7 @@ export const useFileInfoStore = defineStore('info', () => {
return sizeNumber.toFixed(2) + ' ' + units[i]
}
async function getFileInfo(data: EhrFile) {
function getFileInfo(data: EhrFile) {
isFilePreview.value = true
fileInfo.value = data
}
@ -180,7 +160,6 @@ export const useFileInfoStore = defineStore('info', () => {
fileInfo,
getType,
getFormatDate,
getFileNameFormat,
getSize,
getFileInfo,
}