mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 08:36:01 +00:00
public commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { randomString } from "core/utils";
|
||||
|
||||
export function getExtension(filename: string): string | undefined {
|
||||
if (!filename.includes(".")) return;
|
||||
|
||||
const parts = filename.split(".");
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
export function getRandomizedFilename(file: File, length?: number): string;
|
||||
export function getRandomizedFilename(file: string, length?: number): string;
|
||||
export function getRandomizedFilename(file: File | string, length = 16): string {
|
||||
const filename = file instanceof File ? file.name : file;
|
||||
|
||||
if (typeof filename !== "string") {
|
||||
console.error("Couldn't extract filename from", file);
|
||||
throw new Error("Invalid file name");
|
||||
}
|
||||
|
||||
return [randomString(length), getExtension(filename)].filter(Boolean).join(".");
|
||||
}
|
||||
Reference in New Issue
Block a user