Skip to content

Commit

Permalink
fix(apps/mobile): fix issue preventing camera uploads from working
Browse files Browse the repository at this point in the history
Files uploaded directly from the camera do not have a filename property, so we now generate one for them.
  • Loading branch information
hassankhan committed Oct 13, 2024
1 parent 4d1651c commit 75da54b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions apps/mobile/src/store/services/CatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export type ApiFavourite = {
image: Pick<ApiImage, 'id' | 'url'>;
};

const generateFileName = (uri: string) => {
const parts = uri.split('.');
const extension = parts[parts.length - 1];

return `IMG_${Date.now()}.${extension}`;
};

export const CatApi = createApi({
reducerPath: 'catsApi',
baseQuery: fetchBaseQuery({
Expand Down Expand Up @@ -77,11 +84,13 @@ export const CatApi = createApi({

// This is a hack/trick (tr-hack? hatrick?) because React Native/Expo
// Blob/File objects are FUBAR.
body.append('file', {
const rnBlob = {
uri: image.uri,
type: image.type,
name: image.fileName,
} as unknown as Blob);
type: image.mimeType,
name: image.fileName ?? generateFileName(image.uri),
} as unknown as Blob;

body.append('file', rnBlob);

return {
url: 'images/upload',
Expand Down

0 comments on commit 75da54b

Please sign in to comment.