about summary refs log tree commit diff
path: root/src/lib/media/picker.shared.ts
blob: 21e68083241f5682474c8bf1348346e0499752e5 (plain) (blame)
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
import {
  type ImagePickerOptions,
  launchImageLibraryAsync,
  MediaTypeOptions,
} from 'expo-image-picker'
import {t} from '@lingui/macro'

import * as Toast from '#/view/com/util/Toast'
import {getDataUriSize} from './util'

export type PickerImage = {
  mime: string
  height: number
  width: number
  path: string
  size: number
}

export async function openPicker(opts?: ImagePickerOptions) {
  const response = await launchImageLibraryAsync({
    exif: false,
    mediaTypes: MediaTypeOptions.Images,
    quality: 1,
    ...opts,
    legacy: true,
  })

  if (response.assets && response.assets.length > 4) {
    Toast.show(t`You may only select up to 4 images`, 'exclamation-circle')
  }

  return (response.assets ?? [])
    .slice(0, 4)
    .filter(asset => {
      if (asset.mimeType?.startsWith('image/')) return true
      Toast.show(t`Only image files are supported`, 'exclamation-circle')
      return false
    })
    .map(image => ({
      mime: image.mimeType || 'image/jpeg',
      height: image.height,
      width: image.width,
      path: image.uri,
      size: getDataUriSize(image.uri),
    }))
}