# All files / src/Thumbnail Thumbnail.utils.ts

94.11% Statements 16/17
100% Branches 8/8
100% Functions 4/4
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    757x

    7x

    73x

    7x

    7x

    98x
    98x
    343x

    98x

    7x

    104x

    104x
    104x
    104x

     import { getFileExtension } from '../_utils/filename'
    import { ThumbnailDocumentIconVariant } from './ThumbnailDocumentIcon'

    export function getIsFilePreviewableAsImage(
      src: File | string | null | undefined
    ) {
      return (
        src instanceof File &&
        src.type.startsWith('image/') &&
        // not allow previewing vendor image mime types, such as "image/vnd.adobe.photoshop"
        !src.type.startsWith('image/vnd.') &&
        // not allow previewing .tiff images, as a not browser-compatible format
        src.type !== 'image/tiff'
      )
    }

    const extensions: { [k in ThumbnailDocumentIconVariant]?: string[] } = {
      image: ['png', 'jpg', 'jpeg', 'gif', 'psd', 'tiff', 'tif', 'bmp'],
      drawing: ['cad', 'dwg', 'dwf', 'dwfx'],
      email: ['msg', 'eml'],
      gantt: ['mpp', 'p6', 'xer'],
      zip: ['zip', 'rar', '7zip'],
      xml: ['rtf', 'txt'],
      word: ['doc', 'docx'],
      ini: ['ini', 'xml', 'exe'],
      mov: ['mov', 'avi'],
      pdf: ['pdf'],
      powerpoint: ['ppt', 'pptx'],
      excel: ['csv', 'xls', 'xlsx'],
      ['3d']: [
        'stl',
        'obj',
        'ma',
        'fbx',
        'dae',
        '3ds',
        'iges',
        'step',
        'wrl',
        'rvt',
        'ifc',
        'nwc',
        'nwd',
        'nwf',
      ],
      unknown: [],
    }

    const extensionsByVariant = Object.entries(extensions) as [
      ThumbnailDocumentIconVariant,
      string[]
    ][]

    const variantByExtension = extensionsByVariant.reduce((acc, [type, exts]) => {
      exts.forEach((ext) => {
        acc[ext] = type
      })
      return acc
    }, {} as Record<string, ThumbnailDocumentIconVariant>)

    export function getThumbnailVariantForFilename(
      filename: string
    ): ThumbnailDocumentIconVariant {
      const defaultUnknownName = 'unknown'

      try {
        const ext = getFileExtension(filename)
        return ext
          ? variantByExtension[ext] || defaultUnknownName
          : defaultUnknownName
      } catch {
        return defaultUnknownName
      }
    }