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-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-08-23 13:32:28 +00:00
|
|
|
const pickFromCamera = async (
|
|
|
|
allowsEditing: boolean,
|
2022-09-15 13:13:02 +00:00
|
|
|
mediaType: ImagePicker.MediaTypeOptions
|
2022-08-23 13:32:28 +00:00
|
|
|
): Promise<ImagePicker.ImagePickerResult | 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 13:13:02 +00:00
|
|
|
if (!image.cancelled) {
|
|
|
|
const file = await FileSystem.getInfoAsync(image.uri);
|
2022-08-23 12:00:06 +00:00
|
|
|
const data = {
|
2022-09-15 13:13:02 +00:00
|
|
|
...image,
|
|
|
|
path: image.uri,
|
2022-08-23 12:00:06 +00:00
|
|
|
filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`,
|
|
|
|
size: file.size,
|
2022-09-15 13:13:02 +00:00
|
|
|
mime: mime.lookup(image.uri)
|
2022-08-23 12:00:06 +00:00
|
|
|
};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2022-08-23 13:32:28 +00:00
|
|
|
|
2022-09-15 13:13:02 +00:00
|
|
|
export const pickImageAndVideoFromLibrary = async (): Promise<ImagePicker.ImagePickerResult[] | 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 13:13:02 +00:00
|
|
|
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 files = await Promise.all(selectedFiles);
|
|
|
|
return files;
|
2022-08-23 13:32:28 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const pickVideoFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
2022-09-15 13:13:02 +00:00
|
|
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos);
|
2022-08-23 13:32:28 +00:00
|
|
|
|
|
|
|
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
2022-09-15 13:13:02 +00:00
|
|
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);
|