Refactor: Block user from sending files that are not images extensions
This commit is contained in:
parent
44fbc4ebdc
commit
5e28f2d005
2 changed files with 40 additions and 0 deletions
|
|
@ -1,13 +1,30 @@
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
import useFileUpload from "./use-file-upload";
|
import useFileUpload from "./use-file-upload";
|
||||||
|
import useAlertStore from "../../../../../../stores/alertStore";
|
||||||
|
|
||||||
|
const fsErrorText =
|
||||||
|
"Please ensure your file has one of the following extensions:";
|
||||||
|
const snErrorTxt = "png, jpg, jpeg, gif, bmp, webp";
|
||||||
|
|
||||||
export const useHandleFileChange = (setFiles, currentFlowId) => {
|
export const useHandleFileChange = (setFiles, currentFlowId) => {
|
||||||
|
const setErrorData = useAlertStore((state) => state.setErrorData);
|
||||||
const handleFileChange = async (
|
const handleFileChange = async (
|
||||||
event: React.ChangeEvent<HTMLInputElement>,
|
event: React.ChangeEvent<HTMLInputElement>,
|
||||||
) => {
|
) => {
|
||||||
const fileInput = event.target;
|
const fileInput = event.target;
|
||||||
const file = fileInput.files?.[0];
|
const file = fileInput.files?.[0];
|
||||||
if (file) {
|
if (file) {
|
||||||
|
const allowedExtensions = ["png", "jpg", "jpeg", "gif", "bmp", "webp"];
|
||||||
|
const fileExtension = file.name.split(".").pop()?.toLowerCase();
|
||||||
|
|
||||||
|
if (!fileExtension || !allowedExtensions.includes(fileExtension)) {
|
||||||
|
setErrorData({
|
||||||
|
title: "Error uploading file",
|
||||||
|
list: [fsErrorText, snErrorTxt],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const uid = new ShortUniqueId({ length: 10 }); // Increase the length to ensure uniqueness
|
const uid = new ShortUniqueId({ length: 10 }); // Increase the length to ensure uniqueness
|
||||||
const id = uid();
|
const id = uid();
|
||||||
const type = file.type.split("/")[0];
|
const type = file.type.split("/")[0];
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
import useFileUpload from "./use-file-upload";
|
import useFileUpload from "./use-file-upload";
|
||||||
|
import useAlertStore from "../../../../../../stores/alertStore";
|
||||||
|
|
||||||
|
const fsErrorText =
|
||||||
|
"Please ensure your file has one of the following extensions:";
|
||||||
|
const snErrorTxt = "png, jpg, jpeg, gif, bmp, webp";
|
||||||
|
|
||||||
const useUpload = (uploadFile, currentFlowId, setFiles, lockChat) => {
|
const useUpload = (uploadFile, currentFlowId, setFiles, lockChat) => {
|
||||||
|
const setErrorData = useAlertStore((state) => state.setErrorData);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handlePaste = (event: ClipboardEvent): void => {
|
const handlePaste = (event: ClipboardEvent): void => {
|
||||||
if (lockChat) {
|
if (lockChat) {
|
||||||
|
|
@ -15,6 +21,23 @@ const useUpload = (uploadFile, currentFlowId, setFiles, lockChat) => {
|
||||||
const uid = new ShortUniqueId({ length: 3 });
|
const uid = new ShortUniqueId({ length: 3 });
|
||||||
const blob = items[i].getAsFile();
|
const blob = items[i].getAsFile();
|
||||||
if (blob) {
|
if (blob) {
|
||||||
|
const allowedExtensions = [
|
||||||
|
"png",
|
||||||
|
"jpg",
|
||||||
|
"jpeg",
|
||||||
|
"gif",
|
||||||
|
"bmp",
|
||||||
|
"webp",
|
||||||
|
];
|
||||||
|
const fileExtension = blob.name.split(".").pop()?.toLowerCase();
|
||||||
|
|
||||||
|
if (!fileExtension || !allowedExtensions.includes(fileExtension)) {
|
||||||
|
setErrorData({
|
||||||
|
title: "Error uploading file",
|
||||||
|
list: [fsErrorText, snErrorTxt],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
const id = uid();
|
const id = uid();
|
||||||
setFiles((prevFiles) => [
|
setFiles((prevFiles) => [
|
||||||
...prevFiles,
|
...prevFiles,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue