mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
Merge branch 'release/0.19' into feat/advanced-permissions
This commit is contained in:
@@ -6,3 +6,7 @@ export interface Serializable<Class, Json extends object = object> {
|
||||
export type MaybePromise<T> = T | Promise<T>;
|
||||
|
||||
export type PartialRec<T> = { [P in keyof T]?: PartialRec<T[P]> };
|
||||
|
||||
export type Merge<T> = {
|
||||
[K in keyof T]: T[K];
|
||||
};
|
||||
|
||||
@@ -240,3 +240,46 @@ export async function blobToFile(
|
||||
lastModified: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
export function isFileAccepted(file: File | unknown, _accept: string | string[]): boolean {
|
||||
const accept = Array.isArray(_accept) ? _accept.join(",") : _accept;
|
||||
if (!accept || !accept.trim()) return true; // no restrictions
|
||||
if (!isFile(file)) {
|
||||
throw new Error("Given file is not a File instance");
|
||||
}
|
||||
|
||||
const name = file.name.toLowerCase();
|
||||
const type = (file.type || "").trim().toLowerCase();
|
||||
|
||||
// split on commas, trim whitespace
|
||||
const tokens = accept
|
||||
.split(",")
|
||||
.map((t) => t.trim().toLowerCase())
|
||||
.filter(Boolean);
|
||||
|
||||
// try each token until one matches
|
||||
return tokens.some((token) => {
|
||||
if (token.startsWith(".")) {
|
||||
// extension match, e.g. ".png" or ".tar.gz"
|
||||
return name.endsWith(token);
|
||||
}
|
||||
|
||||
const slashIdx = token.indexOf("/");
|
||||
if (slashIdx !== -1) {
|
||||
const [major, minor] = token.split("/");
|
||||
if (minor === "*") {
|
||||
// wildcard like "image/*"
|
||||
if (!type) return false;
|
||||
const [fMajor] = type.split("/");
|
||||
return fMajor === major;
|
||||
} else {
|
||||
// exact MIME like "image/svg+xml" or "application/pdf"
|
||||
// because of "text/plain;charset=utf-8"
|
||||
return type.startsWith(token);
|
||||
}
|
||||
}
|
||||
|
||||
// unknown token shape, ignore
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user