diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/moments/Moment.tsx | 46 | ||||
-rw-r--r-- | src/services/MomentService.ts | 15 |
2 files changed, 44 insertions, 17 deletions
diff --git a/src/components/moments/Moment.tsx b/src/components/moments/Moment.tsx index 9bce6c74..91c14f7d 100644 --- a/src/components/moments/Moment.tsx +++ b/src/components/moments/Moment.tsx @@ -1,5 +1,6 @@ import {useNavigation} from '@react-navigation/native'; import React from 'react'; +import {launchCamera} from 'react-native-image-picker'; import {Alert, StyleProp, StyleSheet, View, ViewStyle} from 'react-native'; import {Text} from 'react-native-animatable'; import {ScrollView, TouchableOpacity} from 'react-native-gesture-handler'; @@ -53,6 +54,15 @@ const Moment: React.FC<MomentProps> = ({ const { user: {userId}, } = useSelector((state: RootState) => state.user); + + const uploadVideo = async (filename: string, filePath: string) => { + let presignedURL = await handlePresignedURL(filename, title); + console.log('presigned' + JSON.stringify(presignedURL)); + Alert.alert('Uploading...'); + await handleVideoUpload(filename, filePath, presignedURL); + Alert.alert('Finish uploading, refreshing moments...'); + dispatch(loadUserMoments(userId)); + }; /** * This function opens the ImagePicker, only lets you select video files, * formats the file extension, then makes a call to the server to get the presigned URL, @@ -74,13 +84,9 @@ const Moment: React.FC<MomentProps> = ({ }) .then(async (vid) => { if ('path' in vid) { - if (vid.filename) { - let presignedURL = await handlePresignedURL(vid.filename, title); - console.log('presigned' + JSON.stringify(presignedURL)); - Alert.alert('Uploading...'); - await handleVideoUpload(vid, presignedURL); - Alert.alert('Finish uploading, refreshing moments...'); - dispatch(loadUserMoments(userId)); + console.log(vid); + if (vid.filename && vid.sourceURL) { + uploadVideo(vid.filename, vid.sourceURL); } } }) @@ -158,7 +164,31 @@ const Moment: React.FC<MomentProps> = ({ <PlusIcon width={23} height={23} - onPress={() => navigateToVideoPicker()} + onPress={() => + Alert.alert('Video Upload', 'pick one', [ + { + text: 'gallery', + onPress: navigateToVideoPicker, + }, + { + text: 'camera (simulator will not work)', + onPress: () => + launchCamera( + { + mediaType: 'video', + durationLimit: 60, + videoQuality: 'medium', + }, + (response) => { + console.log(response); + const file = response.assets[0]; + // TODO: not tested + uploadVideo(file.fileName, file.uri); + }, + ), + }, + ]) + } color={'black'} style={styles.horizontalMargin} /> diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts index d0ed56ab..da6f9690 100644 --- a/src/services/MomentService.ts +++ b/src/services/MomentService.ts @@ -262,20 +262,17 @@ export const handlePresignedURL = async ( /** * This util function takes in the file object and the PresignedURLResponse object, creates form data from the latter, * and makes a post request to the presigned URL, sending the file object inside of the form data. - * @param file: Video, Image, Undefined + * @param filename: the filename + * @param filePath: the path to the file, including filename * @param urlObj PresignedURLResponse | undefined * @returns responseURL or boolean */ export const handleVideoUpload = async ( - file: Video | Image | undefined, + filename: string, + filePath: string, urlObj: PresignedURLResponse | undefined, ) => { try { - let fileName = file?.filename; - if (fileName === null || '') { - console.log('Invalid filename'); - return false; - } if (urlObj === null || urlObj === undefined) { console.log('Invalid urlObj'); return false; @@ -299,10 +296,10 @@ export const handleVideoUpload = async ( urlObj.response_url.fields['x-amz-signature'], ); form.append('file', { - uri: file?.sourceURL, + uri: filePath, // other types such as 'quicktime' 'image' etc exist, and we can programmatically type this, but for now sticking with simple 'video' type: 'video', - name: fileName, + name: filename, }); const response = await fetch(urlObj.response_url.url, { method: 'POST', |