Skip to content

The open alternative to Salesforce, designed for AI.

622 B
Raw Blame
type getImageAbsoluteURIProps = {
  imageUrl: string;
  baseUrl: string;
};

export const getImageAbsoluteURI = ({
  imageUrl,
  baseUrl,
}: getImageAbsoluteURIProps): string => {
  const lowerCaseImageUrl = imageUrl.toLowerCase();
  const isAlreadyAbsoluteUri =
    ['http:', 'https:', 'data:', 'blob:'].some((scheme) =>
      lowerCaseImageUrl.startsWith(scheme),
    ) || imageUrl.startsWith('//');

  if (isAlreadyAbsoluteUri) {
    return imageUrl;
  }

  if (imageUrl.startsWith('/')) {
    return new URL(`/files${imageUrl}`, baseUrl).toString();
  }

  return new URL(`/files/${imageUrl}`, baseUrl).toString();
};