Skip to content

Commit

Permalink
extract url to base64 function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
CDFN committed May 14, 2024
1 parent 4aa4c00 commit 5bdff97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/frontend/hooks/server/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {URL} from 'react-native-url-polyfill';
import {DraftPhoto} from '../../contexts/PhotoPromiseContext/types';
import {useProject} from './projects';
import {Buffer} from 'buffer';
import {convertUrlToBase64} from '../../utils/base64.ts';

type SavablePhoto = SetRequired<
Pick<DraftPhoto, 'originalUri' | 'previewUri' | 'thumbnailUri'>,
Expand Down Expand Up @@ -102,12 +103,8 @@ export function useAttachmentsBase64Query(
return {
queryKey: ['blobAsBase64', attachment.hash],
queryFn: async () => {
const imageResponse = await fetch(attachment.url);
const imageType = imageResponse.headers.get('content-type');
const arrayBuffer = await imageResponse.arrayBuffer();
const base64 = Buffer.from(arrayBuffer).toString('base64');

return `data:${imageType};base64,${base64}`;
const base64Uri = await convertUrlToBase64(attachment.url);
return {...attachment, base64Uri};
},
};
}),
Expand Down
9 changes: 9 additions & 0 deletions src/frontend/utils/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Buffer} from 'buffer';

export const convertUrlToBase64 = async (url: string) => {
const imageResponse = await fetch(url);
const imageType = imageResponse.headers.get('content-type');
const arrayBuffer = await imageResponse.arrayBuffer();
const base64 = Buffer.from(arrayBuffer).toString('base64');
return `data:${imageType};base64,${base64}`;
};

0 comments on commit 5bdff97

Please sign in to comment.