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 FileSystem from 'expo-file-system';
|
||||||
import * as ImagePicker from 'expo-image-picker';
|
import * as ImagePicker from 'expo-image-picker';
|
||||||
import { PermissionsAndroid } from 'react-native';
|
import { PermissionsAndroid } from 'react-native';
|
||||||
|
@ -6,6 +7,13 @@ import * as mime from 'react-native-mime-types';
|
||||||
import { isAndroid } from './helpers';
|
import { isAndroid } from './helpers';
|
||||||
import log from './helpers/log';
|
import log from './helpers/log';
|
||||||
|
|
||||||
|
interface ImagePickerFile extends ImagePicker.ImageInfo {
|
||||||
|
path: string;
|
||||||
|
filename: string;
|
||||||
|
size?: number;
|
||||||
|
mime: string;
|
||||||
|
}
|
||||||
|
|
||||||
const handlePermission = async (): Promise<boolean> => {
|
const handlePermission = async (): Promise<boolean> => {
|
||||||
if (isAndroid) {
|
if (isAndroid) {
|
||||||
const permissions = await PermissionsAndroid.requestMultiple([
|
const permissions = await PermissionsAndroid.requestMultiple([
|
||||||
|
@ -30,10 +38,22 @@ const handlePermission = async (): Promise<boolean> => {
|
||||||
return true;
|
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 (
|
const pickFromCamera = async (
|
||||||
allowsEditing: boolean,
|
allowsEditing: boolean,
|
||||||
mediaType: ImagePicker.MediaTypeOptions
|
mediaType: ImagePicker.MediaTypeOptions
|
||||||
): Promise<ImagePicker.ImagePickerResult | null> => {
|
): Promise<ImagePickerFile | null> => {
|
||||||
try {
|
try {
|
||||||
const hasPermission = await handlePermission();
|
const hasPermission = await handlePermission();
|
||||||
if (!hasPermission) return null;
|
if (!hasPermission) return null;
|
||||||
|
@ -42,18 +62,7 @@ const pickFromCamera = async (
|
||||||
quality: 1,
|
quality: 1,
|
||||||
allowsEditing
|
allowsEditing
|
||||||
});
|
});
|
||||||
|
if (!image.cancelled) return addAdditionalPropsToFile(image);
|
||||||
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;
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(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 {
|
try {
|
||||||
const result = await ImagePicker.launchImageLibraryAsync({
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
||||||
|
@ -69,16 +78,7 @@ export const pickImageAndVideoFromLibrary = async (): Promise<ImagePicker.ImageP
|
||||||
allowsMultipleSelection: true
|
allowsMultipleSelection: true
|
||||||
});
|
});
|
||||||
if (!result.cancelled) {
|
if (!result.cancelled) {
|
||||||
const selectedFiles = result.selected.map(async file => {
|
const selectedFiles = result.selected.map(file => addAdditionalPropsToFile(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 files = await Promise.all(selectedFiles);
|
const files = await Promise.all(selectedFiles);
|
||||||
return files;
|
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);
|
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);
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);
|
||||||
|
|
Loading…
Reference in New Issue