create pickImageFromLibrary method
This commit is contained in:
parent
f878986edb
commit
ff2ff5ccd8
|
@ -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<boolean> => {
|
||||
if (isAndroid) {
|
||||
const permissions = await PermissionsAndroid.requestMultiple([
|
||||
|
@ -30,10 +38,22 @@ const handlePermission = async (): Promise<boolean> => {
|
|||
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<ImagePicker.ImagePickerResult | null> => {
|
||||
): Promise<ImagePickerFile | null> => {
|
||||
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<ImagePicker.ImagePickerResult[] | null> => {
|
||||
export const pickMultipleImageAndVideoFromLibrary = async (): Promise<ImagePickerFile[] | null> => {
|
||||
try {
|
||||
const result = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
||||
|
@ -69,16 +78,7 @@ export const pickImageAndVideoFromLibrary = async (): Promise<ImagePicker.ImageP
|
|||
allowsMultipleSelection: true
|
||||
});
|
||||
if (!result.cancelled) {
|
||||
const selectedFiles = result.selected.map(async file => {
|
||||
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<ImagePicker.ImageP
|
|||
}
|
||||
};
|
||||
|
||||
export const pickVideoFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
||||
// 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<ImagePickerFile | null>;
|
||||
export async function pickImageFromLibrary(base64?: boolean): Promise<ImagePickerFile | (ImagePickerFile & { data: string }) | null> {
|
||||
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<ImagePickerFile | null> =>
|
||||
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos);
|
||||
|
||||
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
||||
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePickerFile | null> =>
|
||||
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);
|
||||
|
|
Loading…
Reference in New Issue