diff options
author | Ivan Chen <ivan@tagg.id> | 2021-06-22 18:20:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 18:20:03 -0400 |
commit | cd5863264d0fe954e63d8cc93fc4ee6ab509f49b (patch) | |
tree | 78b2a4b840e51cec2364cae5980585371f6b74c2 /src/services/MomentService.ts | |
parent | 78f32c1400eff46d4c768b78fbaf672826c74285 (diff) | |
parent | 270cec35e17ff4a1fa2ee1690e596b3e3003fe34 (diff) |
Merge pull request #468 from grusuTagg/tma926-video-upload
[TMA-926] Video Upload
Diffstat (limited to 'src/services/MomentService.ts')
-rw-r--r-- | src/services/MomentService.ts | 129 |
1 files changed, 128 insertions, 1 deletions
diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts index b837585a..d0ed56ab 100644 --- a/src/services/MomentService.ts +++ b/src/services/MomentService.ts @@ -1,12 +1,19 @@ import AsyncStorage from '@react-native-community/async-storage'; +import {Image, Video} from 'react-native-image-crop-picker'; import RNFetchBlob from 'rn-fetch-blob'; import { MOMENTS_ENDPOINT, MOMENTTAG_ENDPOINT, MOMENT_TAGS_ENDPOINT, MOMENT_THUMBNAIL_ENDPOINT, + PRESIGNED_URL_ENDPOINT, + TAGG_CUSTOMER_SUPPORT, } from '../constants'; -import {MomentPostType, MomentTagType} from '../types'; +import { + ERROR_SOMETHING_WENT_WRONG, + ERROR_SOMETHING_WENT_WRONG_REFRESH, +} from '../constants/strings'; +import {MomentPostType, MomentTagType, PresignedURLResponse} from '../types'; import {checkImageUploadStatus} from '../utils'; export const postMoment = async ( @@ -18,6 +25,7 @@ export const postMoment = async ( ) => { try { const request = new FormData(); + //Manipulating filename to end with .jpg instead of .heic if (fileName.endsWith('.heic') || fileName.endsWith('.HEIC')) { fileName = fileName.split('.')[0] + '.jpg'; @@ -208,3 +216,122 @@ export const deleteMomentTag = async (moment_tag_id: string) => { return false; } }; +/** + * This function makes a request to the server in order to provide the client with a presigned URL. + * This is called first, in order for the client to directly upload a file to S3 + * @param value: string | undefined + * @param filename: string | undefined + * @returns a PresignedURLResponse object + */ +export const handlePresignedURL = async ( + filename: string | undefined, + momentCategory: string, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(PRESIGNED_URL_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + filename, + category: momentCategory, + }), + }); + const status = response.status; + let data: PresignedURLResponse = await response.json(); + if (status === 200) { + console.log('done'); + return data; + } else { + if (status === 404) { + console.log( + `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, + ); + } else { + console.log(ERROR_SOMETHING_WENT_WRONG_REFRESH); + } + console.log(response); + } + } catch (error) { + console.log(error); + console.log(ERROR_SOMETHING_WENT_WRONG); + } +}; +/** + * 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 urlObj PresignedURLResponse | undefined + * @returns responseURL or boolean + */ +export const handleVideoUpload = async ( + file: Video | Image | undefined, + 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; + } + //build formData for POST request + // Could not get a forEach to work and could not assign directly, will look into cleaning this series of appends up later. + const form = new FormData(); + form.append('key', urlObj.response_url.fields.key); + form.append( + 'x-amz-algorithm', + urlObj.response_url.fields['x-amz-algorithm'], + ); + form.append( + 'x-amz-credential', + urlObj.response_url.fields['x-amz-credential'], + ); + form.append('x-amz-date', urlObj.response_url.fields['x-amz-date']); + form.append('policy', urlObj.response_url.fields.policy); + form.append( + 'x-amz-signature', + urlObj.response_url.fields['x-amz-signature'], + ); + form.append('file', { + uri: file?.sourceURL, + // 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, + }); + const response = await fetch(urlObj.response_url.url, { + method: 'POST', + headers: { + 'Content-Type': 'multipart/form-data', + }, + body: form, + }); + const status = response.status; + // let data = await response.json(); + if (status === 200 || status === 204) { + console.log('complete'); + return response; + } else { + if (status === 404) { + console.log( + `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, + ); + } else { + console.log('FFFFFF \n'); + console.log(response); + console.log(ERROR_SOMETHING_WENT_WRONG_REFRESH); + } + console.log(response); + return false; + } + } catch (error) { + console.log(error); + console.log(ERROR_SOMETHING_WENT_WRONG); + return false; + } +}; |