Skip to content

Instantly share code, notes, and snippets.

@nemrosim
Created January 12, 2023 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemrosim/6eb86cdb620c75cdc694da5b5cc9283f to your computer and use it in GitHub Desktop.
Save nemrosim/6eb86cdb620c75cdc694da5b5cc9283f to your computer and use it in GitHub Desktop.
import { ApolloClient, gql, useApolloClient } from '@apollo/client';
import Uppy from '@uppy/core';
// https://github.com/uuidjs/uuid#getrandomvalues-not-supported
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
import { translateScalar } from '@landr/mfe/src/utils/translateScalar';
import { useMemo } from 'react';
import { FileUploaderProps } from '@landr/mfe';
import { useFileUploader } from '@landr/mfe/src/customHooks/useFileUploader';
import * as DocumentPicker from 'expo-document-picker';
export const CreateMultipartUploadMutation = gql`
mutation createAssetMultipartUpload($input: CreateAssetMultipartUploadInput!) {
createAssetMultipartUpload(input: $input) {
assetId
uploadId
}
}
`;
export function getCreateMultipartUpload(apolloClient: ApolloClient<object>, interactionSource?: any) {
return async (
file: Uppy.UppyFile<{
folderId?: string;
}>,
): Promise<{
uploadId: string;
key: string;
}> => {
const response = await apolloClient.mutate({
mutation: CreateMultipartUploadMutation,
variables: {
input: {
fileName: file.name,
isMaster: true,
folderId: file?.meta?.folderId,
},
},
...(interactionSource && {
context: {
headers: {
'X-LANDR-USER-INTERACTION-SOURCE': interactionSource,
},
},
}),
});
if (response.errors || !response.data) {
const error = new Error('Error while creating multipart upload') as any;
error.file = file;
error.graphQLErrors = response.errors;
throw error;
}
(file as any).assetId = response.data.createAssetMultipartUpload.assetId;
return {
uploadId: response.data.createAssetMultipartUpload.uploadId,
key: response.data.createAssetMultipartUpload.assetId,
};
};
}
export const defaultUppyOptions: Uppy.UppyOptions<Record<string, never>> | undefined = {
onBeforeFileAdded: (currentFile) => {
if (currentFile.meta) {
// need this to allow duplicated files: https://uppy.io/docs/uppy/#uppy-addFile-fileObject
(currentFile.meta as any).relativePath = uuidv4();
} else {
currentFile.meta = {
relativePath: uuidv4(),
} as any;
}
return currentFile;
},
restrictions: {
maxFileSize: translateScalar('4GB'),
},
};
export const useSendFile = () => {
const apolloClient = useApolloClient();
const context = useMemo(() => ({ apolloClient }), [apolloClient]);
const defaultProps: FileUploaderProps<any> = {
createMultipartUploadMethod: useMemo(() => getCreateMultipartUpload(apolloClient), [apolloClient]),
context,
uppyOptions: defaultUppyOptions,
};
const results = useFileUploader({
...defaultProps,
});
const sendFile = async () => {
const documentPickResult = await DocumentPicker.getDocumentAsync({
copyToCacheDirectory: true, // default
// mp3 - audio/mpeg
// wav - audio/x-wav
type: ['audio/*'],
});
if (documentPickResult.type === 'success') {
const blob = await (await fetch(documentPickResult.uri)).blob();
const file = new File([blob], documentPickResult.name);
try {
const res = await results.upload({
file,
name: documentPickResult.name,
});
return true;
} catch (e) {
console.log('ERROR', JSON.stringify(e)); // <<<
return false;
}
}
};
return sendFile;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment