File size: 1,183 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import path from 'path';
import { URL } from 'url';

const imageExtensionRegex = /\.(jpg|jpeg|png|gif|bmp|tiff|svg|webp)$/i;

/**
 * Extracts the image basename from a given URL.
 *
 * @param urlString - The URL string from which the image basename is to be extracted.
 * @returns The basename of the image file from the URL.
 * Returns an empty string if the URL does not contain a valid image basename.
 */
export function getImageBasename(urlString: string) {
  try {
    const url = new URL(urlString);
    const basename = path.basename(url.pathname);

    return imageExtensionRegex.test(basename) ? basename : '';
  } catch {
    // If URL parsing fails, return an empty string
    return '';
  }
}

/**
 * Extracts the basename of a file from a given URL.
 *
 * @param urlString - The URL string from which the file basename is to be extracted.
 * @returns The basename of the file from the URL.
 * Returns an empty string if the URL parsing fails.
 */
export function getFileBasename(urlString: string) {
  try {
    const url = new URL(urlString);
    return path.basename(url.pathname);
  } catch {
    // If URL parsing fails, return an empty string
    return '';
  }
}