refactor: move /services/Files/images/parse to TS API

This commit is contained in:
Danny Avila 2025-08-20 00:54:02 -04:00
parent c160e7c7d5
commit 1c7b3b53da
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
4 changed files with 13 additions and 19 deletions

View file

@ -1 +1,2 @@
export * from './mistral/crud';
export * from './parse';

View file

@ -0,0 +1,40 @@
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 '';
}
}