2022-09-15 17:24:30 +00:00
|
|
|
/* eslint-disable no-redeclare */
|
2022-08-23 12:00:06 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
import * as ImagePicker from 'expo-image-picker';
|
|
|
|
import { PermissionsAndroid } from 'react-native';
|
2022-09-15 13:13:02 +00:00
|
|
|
import * as mime from 'react-native-mime-types';
|
2022-08-23 12:00:06 +00:00
|
|
|
|
|
|
|
import { isAndroid } from './helpers';
|
|
|
|
import log from './helpers/log';
|
|
|
|
|
2022-09-15 17:24:30 +00:00
|
|
|
interface ImagePickerFile extends ImagePicker.ImageInfo {
|
|
|
|
path: string;
|
|
|
|
filename: string;
|
|
|
|
size?: number;
|
|
|
|
mime: string;
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:32:28 +00:00
|
|
|
const handlePermission = async (): Promise<boolean> => {
|
|
|
|
if (isAndroid) {
|
|
|
|
const permissions = await PermissionsAndroid.requestMultiple([
|
|
|
|
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
|
|
|
PermissionsAndroid.PERMISSIONS.CAMERA
|
|
|
|
]);
|
|
|
|
if (permissions['android.permission.CAMERA'] !== 'granted') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const permission = await ImagePicker.requestCameraPermissionsAsync();
|
|
|
|
if (!permission.granted) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-23 12:00:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 13:32:28 +00:00
|
|
|
const permission = await ImagePicker.getCameraPermissionsAsync();
|
|
|
|
if (!permission.granted) {
|
|
|
|
return false;
|
2022-08-23 12:00:06 +00:00
|
|
|
}
|
2022-08-23 13:32:28 +00:00
|
|
|
|
|
|
|
return true;
|
2022-08-23 12:00:06 +00:00
|
|
|
};
|
|
|
|
|
2022-09-15 17:24:30 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-08-23 13:32:28 +00:00
|
|
|
const pickFromCamera = async (
|
|
|
|
allowsEditing: boolean,
|
2022-09-15 13:13:02 +00:00
|
|
|
mediaType: ImagePicker.MediaTypeOptions
|
2022-09-15 17:24:30 +00:00
|
|
|
): Promise<ImagePickerFile | null> => {
|
2022-08-23 12:00:06 +00:00
|
|
|
try {
|
2022-08-23 13:32:28 +00:00
|
|
|
const hasPermission = await handlePermission();
|
|
|
|
if (!hasPermission) return null;
|
2022-09-15 13:13:02 +00:00
|
|
|
const image = await ImagePicker.launchCameraAsync({
|
2022-08-23 13:32:28 +00:00
|
|
|
mediaTypes: mediaType,
|
2022-08-23 12:00:06 +00:00
|
|
|
quality: 1,
|
|
|
|
allowsEditing
|
|
|
|
});
|
2022-09-15 17:24:30 +00:00
|
|
|
if (!image.cancelled) return addAdditionalPropsToFile(image);
|
2022-08-23 12:00:06 +00:00
|
|
|
return null;
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2022-08-23 13:32:28 +00:00
|
|
|
|
2022-09-15 17:24:30 +00:00
|
|
|
export const pickMultipleImageAndVideoFromLibrary = async (): Promise<ImagePickerFile[] | null> => {
|
2022-08-23 13:32:28 +00:00
|
|
|
try {
|
|
|
|
const result = await ImagePicker.launchImageLibraryAsync({
|
|
|
|
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
|
|
|
quality: undefined, // to force animated gifs
|
|
|
|
allowsMultipleSelection: true
|
|
|
|
});
|
|
|
|
if (!result.cancelled) {
|
2022-09-15 17:24:30 +00:00
|
|
|
const selectedFiles = result.selected.map(file => addAdditionalPropsToFile(file));
|
2022-09-15 13:13:02 +00:00
|
|
|
const files = await Promise.all(selectedFiles);
|
|
|
|
return files;
|
2022-08-23 13:32:28 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-15 17:24:30 +00:00
|
|
|
// 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 pickVideoFromCamera = (allowsEditing = false): Promise<ImagePickerFile | null> =>
|
2022-09-15 13:13:02 +00:00
|
|
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos);
|
2022-08-23 13:32:28 +00:00
|
|
|
|
2022-09-15 17:24:30 +00:00
|
|
|
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePickerFile | null> =>
|
2022-09-15 13:13:02 +00:00
|
|
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);
|