File size: 622 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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();
};