diff --git a/app/lib/methods/mediaPicker.ts b/app/lib/methods/mediaPicker.ts index 874cca39d..64aee7ff3 100644 --- a/app/lib/methods/mediaPicker.ts +++ b/app/lib/methods/mediaPicker.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-redeclare */ import * as FileSystem from 'expo-file-system'; import * as ImagePicker from 'expo-image-picker'; import { PermissionsAndroid } from 'react-native'; @@ -6,6 +7,13 @@ import * as mime from 'react-native-mime-types'; import { isAndroid } from './helpers'; import log from './helpers/log'; +interface ImagePickerFile extends ImagePicker.ImageInfo { + path: string; + filename: string; + size?: number; + mime: string; +} + const handlePermission = async (): Promise => { if (isAndroid) { const permissions = await PermissionsAndroid.requestMultiple([ @@ -30,10 +38,22 @@ const handlePermission = async (): Promise => { return true; }; +const addAdditionalPropsToFile = async (file: ImagePicker.ImageInfo) => { + const fileInfo = await FileSystem.getInfoAsync(file.uri); + const data = { + ...file, + path: file.uri, + filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`, + size: fileInfo.size, + mime: mime.lookup(file.uri) + }; + return data; +}; + const pickFromCamera = async ( allowsEditing: boolean, mediaType: ImagePicker.MediaTypeOptions -): Promise => { +): Promise => { try { const hasPermission = await handlePermission(); if (!hasPermission) return null; @@ -42,18 +62,7 @@ const pickFromCamera = async ( quality: 1, allowsEditing }); - - if (!image.cancelled) { - const file = await FileSystem.getInfoAsync(image.uri); - const data = { - ...image, - path: image.uri, - filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`, - size: file.size, - mime: mime.lookup(image.uri) - }; - return data; - } + if (!image.cancelled) return addAdditionalPropsToFile(image); return null; } catch (error) { log(error); @@ -61,7 +70,7 @@ const pickFromCamera = async ( } }; -export const pickImageAndVideoFromLibrary = async (): Promise => { +export const pickMultipleImageAndVideoFromLibrary = async (): Promise => { try { const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.All, @@ -69,16 +78,7 @@ export const pickImageAndVideoFromLibrary = async (): Promise { - const fileInfo = await FileSystem.getInfoAsync(file.uri); - return { - ...file, - path: file.uri, - filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`, - size: fileInfo.size, - mime: mime.lookup(file.uri) - }; - }); + const selectedFiles = result.selected.map(file => addAdditionalPropsToFile(file)); const files = await Promise.all(selectedFiles); return files; } @@ -89,8 +89,28 @@ export const pickImageAndVideoFromLibrary = async (): Promise => +// Function Overload - https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads +export async function pickImageFromLibrary(base64: true): Promise<(ImagePickerFile & { data: string }) | null>; +export async function pickImageFromLibrary(base64?: false): Promise; +export async function pickImageFromLibrary(base64?: boolean): Promise { + try { + const image = await ImagePicker.launchImageLibraryAsync({ + mediaTypes: ImagePicker.MediaTypeOptions.Images, + quality: undefined, // to force animated gifs + base64 + }); + if (!image.cancelled) return addAdditionalPropsToFile(image); + return null; + } catch (error) { + log(error); + return null; + } +} + +export const pickImageFromLibraryC = () => pickImageFromLibrary; + +export const pickVideoFromCamera = (allowsEditing = false): Promise => pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos); -export const pickImageFromCamera = (allowsEditing = false): Promise => +export const pickImageFromCamera = (allowsEditing = false): Promise => pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);