44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import type { ImportFileTypeAndExtensions } from '@/core/file.ts';
|
|
|
|
import { isString } from './common.ts';
|
|
|
|
export function getFileExtension(filename: string): string {
|
|
if (!filename || !isString(filename)) {
|
|
return '';
|
|
}
|
|
|
|
const parts = filename.split('.');
|
|
return parts[parts.length - 1] as string;
|
|
}
|
|
|
|
export function findExtensionByType(items: ImportFileTypeAndExtensions[] | undefined, type: string): string | undefined {
|
|
if (!items || items.length < 1) {
|
|
return undefined;
|
|
}
|
|
|
|
for (const item of items) {
|
|
if (item.type === type) {
|
|
return item.extensions;
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
export function isFileExtensionSupported(filename: string, supportedExtensions: string): boolean {
|
|
if (!supportedExtensions) {
|
|
return false;
|
|
}
|
|
|
|
const supportedExtensionsArray = supportedExtensions.split(',');
|
|
const fileExtension = getFileExtension(filename).toLowerCase();
|
|
|
|
for (const supportedExtension of supportedExtensionsArray) {
|
|
if (getFileExtension(supportedExtension).toLowerCase() === fileExtension) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|