mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-23 13:48:58 -05:00
ff10ca7d6a
Co-authored-by: pandeymangg <pandeyman@Anshumans-MacBook-Air.local> Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Matti Nannt <matti@formbricks.com>
31 lines
1.1 KiB
TypeScript
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 "";
|
|
}
|
|
};
|