From 182b234557d6f228eb860c7617dbc7132172962e Mon Sep 17 00:00:00 2001 From: Justas Palumickas Date: Fri, 10 Sep 2021 13:46:52 +0300 Subject: [PATCH] Update react native direct upload --- packages/react-native/src/createBlob.ts | 20 +++++++++++++------ packages/react-native/src/index.ts | 2 +- packages/react-native/src/types.ts | 7 +++++-- .../src/{useUpload.ts => useDirectUpload.ts} | 4 ++-- 4 files changed, 22 insertions(+), 11 deletions(-) rename packages/react-native/src/{useUpload.ts => useDirectUpload.ts} (96%) diff --git a/packages/react-native/src/createBlob.ts b/packages/react-native/src/createBlob.ts index f660650..3e5a2a9 100644 --- a/packages/react-native/src/createBlob.ts +++ b/packages/react-native/src/createBlob.ts @@ -9,22 +9,30 @@ interface Options { } const createBlob = async (file: File, { host, mountPath = '/uploads' }: Options) => { + if (file.contentType && !file.contentType.match(/.+\/.+/)) { + return { data: null, error: 'Invalid content type' }; + } + const filePath = file.localUri || file.uri; const fileData = await getFileInfo(filePath); if (!fileData) return { data: null, error: 'Cannot get data from file' }; - const fileName = file.filename || filePath.replace(/^.*[\\\/]/, ''); + const fileName = file.fileName || filePath.replace(/^.*[\\\/]/, ''); + + const metadata = { + ...file.metadata, + } + + if (file.width) { metadata.width = file.width } + if (file.height) { metadata.height = file.height } const requestData = { checksum: await checksum(fileData.md5), size: fileData.size, fileName, - contentType: file.type || mime.getType(fileName), - metadata: { - width: file.width, - height: file.height, - }, + contentType: file.contentType || mime.getType(fileName), + metadata, }; try { diff --git a/packages/react-native/src/index.ts b/packages/react-native/src/index.ts index 0835458..f12ff31 100644 --- a/packages/react-native/src/index.ts +++ b/packages/react-native/src/index.ts @@ -1,4 +1,4 @@ export { default as Context } from './Context'; export { default as UploProvider } from './UploProvider'; -export { default as useUpload } from './useUpload'; +export { default as useDirectUpload } from './useDirectUpload'; export * from './types'; diff --git a/packages/react-native/src/types.ts b/packages/react-native/src/types.ts index 13150fd..51952a8 100644 --- a/packages/react-native/src/types.ts +++ b/packages/react-native/src/types.ts @@ -2,10 +2,13 @@ export interface File { id?: string; localUri?: string; uri: string; - filename?: string; - type?: string; + fileName?: string; + contentType?: string; width?: number; height?: number; + metadata?: { + [key: string]: string | number; + }; } export interface Upload { diff --git a/packages/react-native/src/useUpload.ts b/packages/react-native/src/useDirectUpload.ts similarity index 96% rename from packages/react-native/src/useUpload.ts rename to packages/react-native/src/useDirectUpload.ts index 943ae0b..24aa4ce 100644 --- a/packages/react-native/src/useUpload.ts +++ b/packages/react-native/src/useDirectUpload.ts @@ -4,7 +4,7 @@ import useConfig from './useConfig'; import createBlob from './createBlob'; import { Upload, File, UseUploadOptions } from './types'; -const useUpload = ({ multiple = false }: UseUploadOptions = {}) => { +const useDirectUpload = ({ multiple = false }: UseUploadOptions = {}) => { const [uploads, setUploads] = useState([]); const { host, mountPath } = useConfig(); @@ -98,4 +98,4 @@ const useUpload = ({ multiple = false }: UseUploadOptions = {}) => { }; }; -export default useUpload; +export default useDirectUpload;