Files
ontime/apps/client/src/common/utils/uploadUtils.ts
T

76 lines
1.8 KiB
TypeScript

/**
* Collection of rules for pre-validating a spreadsheet
* @param file
*/
export function validateExcelImport(file: File) {
if (!isExcelFile(file)) {
throw new Error('Unknown file type');
}
// Check if file is empty
if (file.size === 0) {
throw new Error('File is empty');
}
// Limit file size of an Excel file to around 10MB
if (file.size > 10_000_000) {
throw new Error('File size limit (10MB) exceeded');
}
}
/**
* Collection of rules for pre-validating a project file
* @param file
*/
export function validateProjectFile(file: File) {
if (!isOntimeFile(file)) {
throw new Error('Unknown file type');
}
// Check if file is empty
if (file.size === 0) {
throw new Error('File is empty');
}
// Limit file size of a project file to around 1MB
if (file.size > 2_000_000) {
throw new Error('File size limit (2MB) exceeded');
}
}
/**
* Removes a trailing file extension from a file name (e.g. "show.xlsx" -> "show")
* A leading dot (dotfiles like ".gitignore") is not treated as an extension
*/
export function removeFileExtension(fileName: string): string {
const lastDot = fileName.lastIndexOf('.');
if (lastDot <= 0) {
return fileName;
}
return fileName.slice(0, lastDot);
}
export function isExcelFile(file: File | null) {
return file?.name.endsWith('.xlsx');
}
export function isOntimeFile(file: File | null) {
return file?.name.endsWith('.json');
}
/**
* Collection of rules for pre-validating a project file
* @param file
*/
export function validateLogo(file: File) {
// Check if file is empty
if (file.size === 0) {
throw new Error('File is empty');
}
// Limit file size of a project file to around 1.5MB
if (file.size > 1_500_000) {
throw new Error('File size limit (1.5MB) exceeded');
}
}