handleImageBase64

This commit is contained in:
Reinaldo Neto 2023-10-06 17:28:27 -03:00
parent 2589707929
commit e2f0fc2c81
3 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,23 @@
const imageBase64 = /^data:image\/[bmp,gif,ico,jpg,png,svg,webp,x\-icon,svg+xml]+;base64,/;
const imageBase64RegExp = new RegExp(imageBase64);
export function isImageBase64(data: string): boolean {
return !!data && imageBase64RegExp.test(data);
}
const regExpOf120Characters = new RegExp(/^data:image\/[bmp,gif,ico,jpg,png,svg,webp,x\-icon,svg+xml]+;base64,([\w+\/=]{1,120})/);
export function valueOfFirst120CharactersOfImageBase64(imageBase64: string): string | undefined {
const result = imageBase64.match(regExpOf120Characters)?.[0];
if (result) {
return result.replace(imageBase64RegExp, '');
}
}
// I don't know if we should test this function, because the regex will pass through all the data on the base64
// "maybe", depending the length of the data, will waste to much time
// https://github.com/wix/react-native-ui-lib/blob/cf700e0d65caa0a0b601fe1edbd763ad4e6748a4/src/utils/imageUtils.ts#L14-L18
export function isImageBase64ImageContent(data: string): boolean {
const imageBase64Content = data.split(',')[1];
const imageBase64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
return imageBase64regex.test(imageBase64Content);
}

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import { URL } from 'react-native-url-polyfill';
import { LOCAL_DOCUMENT_DIRECTORY } from '../handleMediaDownload';
import { isImageBase64 } from '../handleBase64';
function setParamInUrl({ url, token, userId }: { url: string; token: string; userId: string }) {
const urlObj = new URL(url);
@ -10,7 +11,10 @@ function setParamInUrl({ url, token, userId }: { url: string; token: string; use
}
export const formatAttachmentUrl = (attachmentUrl: string | undefined, userId: string, token: string, server: string): string => {
if (LOCAL_DOCUMENT_DIRECTORY && attachmentUrl?.startsWith(LOCAL_DOCUMENT_DIRECTORY)) {
if (
(attachmentUrl && isImageBase64(attachmentUrl)) ||
(LOCAL_DOCUMENT_DIRECTORY && attachmentUrl?.startsWith(LOCAL_DOCUMENT_DIRECTORY))
) {
return attachmentUrl;
}
if (attachmentUrl && attachmentUrl.startsWith('http')) {