30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
import { getFileIcon } from './fileIcons'
|
|
|
|
export const getPreviewType = (document) => {
|
|
if (!document) { return null }
|
|
const mime = (document.mimeType || '').toLowerCase()
|
|
const path = document.path || ''
|
|
|
|
const check = prefix => mime.startsWith(prefix) || path.startsWith(`data:${prefix}`)
|
|
|
|
if (check('image/')) { return 'image' }
|
|
if (mime === 'application/pdf' || path.startsWith('data:application/pdf')) { return 'pdf' }
|
|
if (check('audio/')) { return 'audio' }
|
|
if (check('video/')) { return 'video' }
|
|
if (check('text/') || mime.includes('json') || mime.includes('xml') || path.startsWith('data:application/json')) { return 'text' }
|
|
return null
|
|
}
|
|
|
|
export const canPreviewDocument = (document = {}) => !!getPreviewType(document)
|
|
|
|
export const isImageDocument = (document = {}) => getPreviewType(document) === 'image'
|
|
|
|
export const isPdfDocument = (document = {}) => getPreviewType(document) === 'pdf'
|
|
|
|
export const describeDocument = (document) => {
|
|
if (!document) { return '' }
|
|
const name = document.filename || document.name || ''
|
|
const icon = getFileIcon({ name, mime: document.mimeType })
|
|
return icon.label
|
|
}
|