verdnatura-chat/app/lib/methods/mediaPicker.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
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
}
const permission = await ImagePicker.getCameraPermissionsAsync();
if (!permission.granted) {
return false;
2022-08-23 12:00:06 +00:00
}
return true;
2022-08-23 12:00:06 +00:00
};
const pickFromCamera = async (
allowsEditing: boolean,
2022-09-15 13:13:02 +00:00
mediaType: ImagePicker.MediaTypeOptions
): Promise<ImagePicker.ImagePickerResult | null> => {
2022-08-23 12:00:06 +00:00
try {
const hasPermission = await handlePermission();
if (!hasPermission) return null;
2022-09-15 13:13:02 +00:00
const image = await ImagePicker.launchCameraAsync({
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-09-15 13:13:02 +00:00
export const pickImageAndVideoFromLibrary = async (): Promise<ImagePicker.ImagePickerResult[] | null> => {
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;
}
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);
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
2022-09-15 13:13:02 +00:00
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images);