Skip to content

Commit

Permalink
Update react native direct upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalumickas committed Sep 10, 2021
1 parent d75fe65 commit 182b234
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
20 changes: 14 additions & 6 deletions packages/react-native/src/createBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 5 additions & 2 deletions packages/react-native/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Upload[]>([]);
const { host, mountPath } = useConfig();

Expand Down Expand Up @@ -98,4 +98,4 @@ const useUpload = ({ multiple = false }: UseUploadOptions = {}) => {
};
};

export default useUpload;
export default useDirectUpload;

0 comments on commit 182b234

Please sign in to comment.