tipoArquivo

This commit is contained in:
Luiz Silva 2024-09-16 19:53:13 -03:00
parent e87d0a0239
commit eae585645d
5 changed files with 51 additions and 5 deletions

View file

@ -1,6 +1,8 @@
type tiposArquivo = "imagem" | "documento" | "vídeo" | "outros"
export const extensoes: {
ext: string
tipo: "imagem" | "documento" | "vídeo"
tipo: tiposArquivo
mime: string
}[] = [
{
@ -154,3 +156,22 @@ export const extensoes: {
mime: "video/mpeg",
},
]
/**
* Função que retorna o tipo do arquivo
* @param nomeArquivo
* @returns
*/
export const tipoArquivo = (
nomeArquivo: string | null | undefined,
): tiposArquivo => {
// extenssão do arquivo
const extArquivo = String(nomeArquivo || "")
.toLocaleLowerCase()
.split(".")
.pop()
// procura a extensão do arquivo na lista de extensões
const extensao = extensoes.find((extensao) => extensao.ext === extArquivo)
// retorna o tipo do arquivo
return extensao?.tipo || "outros"
}