Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 503x 5x 5x 852x 5x 5x 5x 5x 5x 5x 5x 852x 4x 4x 4x 4x 4x 4x 4x 852x | import { AcceptAllowedImagesTypes, MAX_UPLOAD_SIZE } from "@constants/constants";
import { useUploadProgress } from "@redux/slices/uploadProgressSlice";
import { IFileWithMeta, StatusValue } from "react-dropzone-uploader";
export const getFileExtension = (filename: string) => {
const dotIndex = filename.lastIndexOf(".");
return dotIndex !== -1 ? filename.slice(dotIndex) : "";
};
export function useIsValidFile() {
const { setUploadProgress } = useUploadProgress();
function isValidFile({
file,
allowedTypes,
maxSizeInBytes = MAX_UPLOAD_SIZE.value,
actualSize,
allowedExtensions = Object.values(AcceptAllowedImagesTypes).flat(),
}: {
file: File;
allowedTypes: string[];
allowedExtensions?: string[];
maxSizeInBytes?: number;
actualSize?: any;
}) {
Iif (actualSize && actualSize >= maxSizeInBytes) {
setUploadProgress({
key: Math.random().toString(36).substring(7),
data: {
fileName: file.name,
size: actualSize,
tooLarge: true,
},
});
return false;
} else Iif (!actualSize && file.size >= maxSizeInBytes) {
setUploadProgress({
key: Math.random().toString(36).substring(7),
data: {
fileName: file.name,
size: file.size,
tooLarge: true,
},
});
return false;
}
// check type
Eif (file && file instanceof Blob) {
const fileType = file.type;
const fileNameExtension = getFileExtension(file.name);
if (
allowedTypes.includes(fileType) ||
(!fileType && allowedExtensions.includes(fileNameExtension))
) {
return true;
} else E{
setUploadProgress({
key: Math.random().toString(36).substring(7),
data: {
fileName: file.name,
size: actualSize ? actualSize : file.size,
unsuported: true,
},
});
return false;
}
}
}
const isDropzoneImageFileValid = (
fileWithMeta: IFileWithMeta,
status: StatusValue,
fileAllowedTypes = AcceptAllowedImagesTypes,
) => {
const { file, remove } = fileWithMeta;
Iif (["preparing", "removed"].includes(status)) return;
const allowedTypes = Object.keys(fileAllowedTypes);
const allowedExtensions = Object.values(fileAllowedTypes).flat();
const isValid = isValidFile({
file,
allowedTypes,
maxSizeInBytes: MAX_UPLOAD_SIZE.value,
allowedExtensions,
});
Iif (!isValid) {
remove && remove();
return false;
}
return true;
};
return { isValidFile, isDropzoneImageFileValid };
}
|