Files
formbricks/apps/web/modules/storage/url-helpers.ts
T
Anshuman Pandey ff10ca7d6a fix: allows local ip images (#7189)
Co-authored-by: pandeymangg <pandeyman@Anshumans-MacBook-Air.local>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Matti Nannt <matti@formbricks.com>
2026-02-10 17:29:27 +01:00

31 lines
1.1 KiB
TypeScript

/**
* Client-safe URL helper utilities for storage files.
* These functions can be used in both server and client components.
*/
/**
* Extracts the original file name from a storage URL.
* Handles both relative paths (/storage/...) and absolute URLs.
* @param fileURL The storage URL to parse
* @returns The original file name, or empty string if parsing fails
*/
export const getOriginalFileNameFromUrl = (fileURL: string): string => {
try {
const lastSegment = fileURL.startsWith("/storage/")
? (fileURL.split("/").pop() ?? "")
: (new URL(fileURL).pathname.split("/").pop() ?? "");
const fileNameFromURL = lastSegment.split(/[?#]/)[0];
const [namePart, fidPart] = fileNameFromURL.split("--fid--");
if (!fidPart) return namePart ? decodeURIComponent(namePart) : "";
const dotIdx = fileNameFromURL.lastIndexOf(".");
const hasExt = dotIdx > fileNameFromURL.indexOf("--fid--");
const ext = hasExt ? fileNameFromURL.slice(dotIdx + 1) : "";
return decodeURIComponent(ext ? `${namePart}.${ext}` : namePart);
} catch {
return "";
}
};